DEV Community

Discussion on: Which do you prefer, and why?

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.