Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ or foreach - style/readability and speed

I have a piece of code for some validation logic, which in generalized for goes like this:

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

This works, is pretty easy to understand, and has early-out optimization. It is, however, pretty verbose. The main purpose of this question is what is considered readable and good style. I'm also interested in the performance; I'm a firm believer that premature {optimization, pessimization} is the root of all evil, and try to avoid micro-optimizing as well as introducing bottlenecks.

I'm pretty new to LINQ, so I'd like some comments on the two alternative versions I've come up with, as well as any other suggestions wrt. 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 believe that V2 offers much over V1 in terms of readability, even if shorter. I find V3 to be clear & concise, but I'm not too fond of the Method().Property part; of course I could turn the lambda into a full delegate, but then it loses it's one-line elegance.

What I'd like comments on are:

  1. Style - ever so subjective, but what do you feel is readable?
  2. Performance - are any of these a definite no-no? As far as I understand, all three methods should early-out.
  3. Debuggability - anything to consider?
  4. Alternatives - anything goes.

Thanks in advance :)

like image 514
snemarch Avatar asked Sep 07 '10 22:09

snemarch


People also ask

Is LINQ version faster than foreach one?

Is the LINQ version faster than the foreach one? No. Yes. Maybe. LINQ isn’t magic. It doesn’t make code faster in some weird way that we can’t understand. , they’re going to be really close to the same runtime. If you don’t do the break in that second code, though, they might be very different. The real gain with LINQ is readability.

Why should LINQ be faster?

Why should LINQ be faster? It also uses loops internally. Most of the times, LINQ will be a bit slower because it introduces overhead. Do not use LINQ if you care much about performance. Use LINQ because you want shorter better readable and maintainable code. Show activity on this post.

Why do we use LINQ instead of for loop?

More importantly though, LINQ is just much easier to read. That should be enough reason. It should probably be noted that the for loop is faster than the foreach. So for the original post, if you are worried about performance on a critical component like a renderer, use a for loop.

Is LINQ a bottleneck?

It would be painful to have to replace many usages of LINQ with other constructs in addition to time spent finding that to be an actual bottleneck. Let’s do a basic benchmark to see what using LINQ might cost us compared to more traditional constructs such as for and foreach loops, and see if this worry is valid. I used the BenchmarkDotNet library.


1 Answers

I think All would be clearer:

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

I think it's unlikely using linq here would cause a performance problem over a regular foreach loop, although it would be straightforward to change if it did.

like image 160
Lee Avatar answered Oct 19 '22 22:10

Lee