LINQ or foreach - style / readability and speed

I have a piece of code for some verification logic, which in a generalized form looks like this:

private bool AllItemsAreSatisfactoryV1(IEnumerable<Source> collection) { foreach(var foo in collection) { Target target = SomeFancyLookup(foo); if (!target.Satisfactory) { return false; } } return true; } 

It works, is pretty easy to understand, and it has optimization. This, however, is rather verbose. The main purpose of this question is what is considered readable and good style. I am also interested in performance; I strongly believe that premature (optimization, pessimization) is the root of all evil and is trying to avoid microoptimization, as well as introduce bottlenecks.

I am new to LINQ, so I would like to get some comments on the two alternative versions that I came up with, as well as any other suggestions. readability.

 private bool AllItemsAreSatisfactoryV2(IEnumerable<Source> collection) { return null == (from foo in collection where !(SomeFancyLookup(foo).Satisfactory) select foo).First(); } private bool AllItemsAreSatisfactoryV3(IEnumerable<Source> collection) { return !collection.Any(foo => !SomeFancyLookup(foo).Satisfactory); } 

I don't think V2 offers much more than V1 in terms of readability, even if it is shorter. I find V3 clear and concise, but I don't like the Method().Property part too much; Of course, I could turn the lambda into a full delegate, but then it loses its one-line elegance.

I would like the comments to be:

  • Style is so subjective, but what do you think is readable?
  • Is a performance a definite no-no? As far as I understand, all three methods should be early.
  • Is debugging all you need to consider?
  • Alternatives - everything goes.

Thanks in advance:)

+6
performance c # coding-style foreach linq-to-objects
source share
2 answers

I think All will be more clear:

 private bool AllItemsAreSatisfactoryV1(IEnumerable<Source> collection) { return collection.Select(f => SomeFancyLookup(f)).All(t => t.Satisfactory); } 

I think it is unlikely that using linq here will cause a performance problem over the regular foreach loop, although that would be easy to change if that happened.

+12
source share

I personally have no problem with the V3 style, and this will be my first choice. You are essentially browsing the list for those whose search is not satisfactory.

V2 is hard to understand, and an exception will be thrown in its current form (First () requires that the IEnumerable source is not empty, I think you're looking for FirstOrDefault ()). Why not just stick Any () on the end instead of comparing the result from the list with zero?

V1 is good if the bit is a bit booby, and probably the easiest to debug, as I found lambdas debugging to be a little ubiquitous from time to time. You can remove the curly braces to lose some spaces without sacrificing readability.

Indeed, all three will condense on very similar opcodes; iterate over the collection, call SomeFancyLookup () and check the return value property; go to the first failure. Any () hides a very similar foreach algorithm. The difference between V1 and all others is the use of a named variable, which MAY be a little less efficient, but you have a reference to the target in all three cases, so I doubt its significance if the difference even exists.

+1
source share

All Articles