DEV Community

Zohar Peled
Zohar Peled

Posted on

3 1

Which do you prefer, and why?

Yes, I'm here again with a c# coding style question - and today - between the following different coding styles - which do you prefer and why?

1:

if(condition) single-line-of-code;
Enter fullscreen mode Exit fullscreen mode

2:

if(condition) 
    single-line-of-code;
Enter fullscreen mode Exit fullscreen mode

3:

if(condition) 
{
    single-line-of-code;
}
Enter fullscreen mode Exit fullscreen mode

4:

if(condition) 
    {single-line-of-code;}
Enter fullscreen mode Exit fullscreen mode

5:

if(condition) {single-line-of-code;}
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
jayjeckel profile image
Jay Jeckel

Never 1 or 2, as the braces should always be included. These are just asking for some lazy, distracted, or inexperienced maintenance coder to come along and break the whole thing.

With 4, the opening brace should be aligned with the if, not indented passed it.

In general, 3 and 5 are my preferences, though the lack of proper spacing is bugging me. if is a keyword, not a function, so there should be a space between it and the opening parenthesis. Likewise, there should be a space after the opening brace and before the closing brace.
if (condition) { single-line-of-code; }

Collapse
 
peledzohar profile image
Zohar Peled

Thanks for your answer.
I totally agree with your comment on 1 and two. Personally I like option 3 the best.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay