Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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();
share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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" ;)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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