Reputation: 5502
I use Resharper, and when I make a few lines of code like this:
foreach (var posCombination in possibleCombinations)
{
if (posCombination .Count == combo.Count && posCombination .Select((l, i) => combo.Contains(l)).All(b => b))
{
return true;
}
}
return false;
It will ask me if I want to convert it into a LINQ-expression:
return possibleCombinations.Any(possibleCombination =>
possibleCombination.Count == combo.Count
&& possibleCombination.Select((l, i) => combo.Contains(l)).All(b => b));
I've had a lot of people tell me that they have a hard time reading whats going on in a LINQ statement... So why would I want to convert it to a LINQ-expression, if it makes my code less readable?
Upvotes: 3
Views: 2228
Reputation: 2531
You're already using LINQ expressions, namely this part of your code:
posCombination.Select((l, i) => combo.Contains(l)).All(b => b)
So you might be able to answer your own question. Why did you decide to use a LINQ expression there?
I believe this is more of an issue with what ReSharper suggests than LINQ expressions themselves. Like others have said, they are only suggestions, and it's up to you to decide what to do with them. I believe this particular suggestion just needs to be cleaned up, and then it will make your code more readable. First, the lamda expression's parameter name should be shortened because you can infer from the calling collection what it is. Second, your original LINQ expression can be simplified into this:
posCombination.All(x => combo.Contains(x))
Here's resulting LINQ expression:
return possibleCombinations.Any(p => p.Count == combo.Count &&
p.All(x => combo.Contains(x)));
Now it's a terse yet descriptive line of code that doesn't require you to examine the innards of a loop. Of course this is still just a suggestion. If you want you can use more descriptive parameter names, extract a less readable part into a method, add comments, or stick with your original code.
Upvotes: 3
Reputation: 30708
I've had a lot of people tell me that they have a hard time reading whats going on in a LINQ statement... So why would I want to convert it to a LINQ-expression, if it makes my code less readable?
The issue is that existing code looks readable, as developers are used to it for very long. LINQ syntax is relatively new (except DB queries).
Initially i had struggled to understand even simple LINQ expressions, but with practice i Just love it. Not denying the fact some of LINQ expressions are simply obscure (ex Aggregate) Now i can write complex queries with ease.
Why LINQ expressions should be used? It simplifies code (reduces LOC), and provides powerful constructs (like sorting, filtering, grouping, lazy loading)
Upvotes: 0
Reputation: 10995
You can set Resharper to give you hints instead. You don't have to follow every suggestion it makes, and to be honest it can be quite annoying.
return possibleCombinations.Any(possibleCombination => possibleCombination.Count == combo.Count && possibleCombination.Select((l, i) => combo.Contains(l)).All(b => b));
Is an example as to when you shouldn't take Resharper's advice. Not everyone is a Linq guru, and I know this when working with some interns. They kept asking me what my Linq code does, and even my mentor didn't like it. Better to keep the code simple and readable thus easy to maintain.
Use Linq when it is very obvious with what you are trying to accomplish.
How to configure Resharper, if you are getting fed up.
Upvotes: 0
Reputation: 726639
This is entirely up to you: if readers of your code, including yourself, prefer a more verbose style, by all means keep it: hard-to-read clever code is much more expensive in programmer's time than in CPU time. After all, it's only ReSharper's hint: paying attention or disregarding it is entirely up to you.
Reading LINQ code would become easier with time (I know it did become much easier for me, but it took considerable amount of writing LINQ code and looking at LINQ code written by other team members). One thing that we found particularly useful was commenting: a LINQ expression can fit a surprising amount of information in a short line of code, so spelling out the intent in plain English helps figuring out the meaning once I stumble upon a line that I wrote a few months ago.
Upvotes: 6