3

Every now and then when I have a complex lambda, I do something like this:

Func<SomeObject, bool> equals = o =>
    o.ID == someID && o.Name == someName && IsAdd || ...
var myList = MyThings.Where(equals).ToList();

Let's assume that the logic in equals can not be simplified. Does extracting the lambda from the linq query improve, or reduce readability?

The alternative being:

var myList = MyThings.Where(o => o.ID == someID && o.Name == someName && IsAdd || ...).ToList();

2 Answers 2

3

When you format a complex Linq query well enough (don't let it run on too many lines) it shouldn't be more difficult to read than a complex condition in a while loop or if statement.

The follow reads just fine to me:

var myList = MyThings.Where(o => o.ID == someID
                              && o.Name == someName
                              && IsAdd
                              || /* other conditions */).ToList();

So does the version using an extracted equals Func<T, bool>.

However, usually when I see a Func object like that, I make the assumption that it may not always be the same logic. I use a Func when the action being performed needs to be passed in externally, or determined by some condition. In this case, your Func is right next to the Linq expression itself, so it's easy to see that it's not the case, but it may be confusing if they were separated further.

Let's pretend this logic was being used in an if condition. If the logic is complex enough that you would consider splitting it out into its own named Boolean variable, it's probably justifiable to do something similar and make a Func. If you would write the condition directly inside the body of your if, it's probably simple enough to understand inside the body of your Where function.

5

There is another alternative. If the lambdas are big, I would suggest that you extract one or more methods. Something like this:

var myList = myThings.Where(MatchesTheCondition).ToList();

private bool MatchesTheCondition(SomeObject o) {
  return o.ID == someID && o.Name == someName && IsAdd || ... ;
}

Of course, your method shouldn´t be named "MatchesTheCondition" ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.