Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is more readable? [closed]

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);
    
like image 717
Mariano Avatar asked Oct 14 '08 12:10

Mariano


People also ask

What is better English or English CC?

Basically, the difference between English and English [CC] is that the closed-captions setting provides descriptions of sounds, such as gasps, and prompts as to who is speaking. They're often autogenerated and, in Squid Game's case according to one viewer, a closer match to the English dub than the English subtitles.

What does CC mean in captions?

Closed Captions are the most common kind of captions, used by major broadcasters and video streaming services like Facebook Live, YouTube and Vimeo. Usually identified by a [CC] symbol in the corner of the screen, closed captions exist as a separate file, allowing the viewer to switch them on or off whilst watching.

What does closed caption mean on TV?

Closed captioning displays the audio portion of a television program as text on the TV screen, providing a critical link to news, entertainment and information for individuals who are deaf or hard-of-hearing.

What is an example of closed captioning?

Closed captions are a text version of the audio content of a video. This includes spoken words, but also information about who is speaking and any sounds relevant to understanding context and meaning. For example: [laughter], [applause], [ominous music], the lyrics to a song playing in the background, etc.


3 Answers

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

like image 161
Ólafur Waage Avatar answered Oct 13 '22 21:10

Ólafur Waage


Use whichever you prefer but hide it in a method:

return risks.SumTechnicalPremium();
like image 38
Cristian Libardo Avatar answered Oct 13 '22 19:10

Cristian Libardo


Neither. The first one is more verbose and likely to be understood by everyone. The second one is more concise and easily understood by everyone with even a passing knowledge of linq.

I'd say you can base your choice on the environment you are in.

like image 36
Goran Avatar answered Oct 13 '22 21:10

Goran