Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1044829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:53:25+00:00 2026-05-16T15:53:25+00:00

I have a piece of code for some validation logic, which in generalized for

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:53:26+00:00Added an answer on May 16, 2026 at 3:53 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.