Search code examples
c#linqlambdareadability

What is more readable?


I have these two pieces of code, wich one is more readable?

  1. foreach

    decimal technicalPremium = 0;
    foreach (Risk risk in risks)
    {
         technicalPremium = technicalPremium + risk.TechnicalPremium;
    }
    return technicalPremium;
    
  2. linq

    return risks.Sum(risk => risk.TechnicalPremium);
    

Solution

  • If the team that works on the code knows what the Linq version does and knows its inner workings, then it is more readable.