Was going through some legacy code recently and found a big for loop that had multiple switch statement inside. So being that I hate something that is fairly large, every method should be small and concise, I went along to refactor it.

This code is generic so you might see something like this in your code base also.

As you will notice one can go on and keep adding types to it (so did the legacy code).
But if you think about it what this code is basically doing is that its just filtering things out to seperate collections.

Let see how we can answer things with LINQ on this one

First lets remove the if statement for customer IGNORE.

Ok one step done thats better, now lets see that the loop is basically a filter, so lets just take the payingCustomer example and break it down first. Its is going through a loop and finding the A_TYPE customer. Therefore a linq query like

collection.Where(x => x.Type == A_TYPE).toDictionary(t => t.Key, t.Name);

can answer that one.

So lets create a function/method that does this.

So here we have a single responsibility function, that just does one thing and does it well.
Its more of a functional style of programming (data in and data out).
No side effects of any sort. No saving of data to database or file etc.

Now lets go back to our original code, we can now remove the loop and switch statement all together.

Note: we now have a filterCustomer collection, which filters out the ignore customers first.

By doing so we can write a unit test to test the method/function FilterCustomers and we can be 100% sure that all it does is a single responsibility task.
We now have removed complexity in our codebase and the test-ability goes up.