In C/C++/Java/JavaScript, an if
/for
/while
statement may be used with
exactly one regular statement in its block, no more, no less. If you want the
block to have no statements, you must use a ;
to indicate an empty statement.
If you want more than one statement, you must use {}
to create a compound
statement. This results in great controversy regarding the use of braces.
It is said that programmers spend more time reading code than writing it. This article will only consider issues of readability. We will ignore the effects on writing or editing code.
There are two questions to consider: whether to omit braces in one-statement blocks, and whether to put each brace on a line by itself. We must consider these questions in conjunction with each other. We must decide which question to answer first, and how the first decision will influence the second.
Do we use braces where they’re not required?
Let’s consider the omission of braces first.
Omitting braces for one-statement blocks
This may improve readability by saving us an occasional closing brace which would take up a line by itself. Getting those lines back would fit more lines on the screen, which makes it easier to follow what the code is doing.
Some may point out that putting braces around one statement is a no-op. No-op
code wastes the reader’s time, because he must look at the code, decide that it
does nothing, then mentally throw it out. Braces, however, are only a no-op when
we have exactly one statement. They’re not even a no-op with zero statements,
because they replace the ;
. Due to braces being a no-op only in one special
case, I don’t believe this is a good argument for omitting braces. I bring it up
only to show I’ve thought of it.
Now, do we put the braces we do need each on their own line, or do we cuddle them?
Putting each brace on its own line
This is the GNU style. It emphasizes the choice to treat single statements differently from compound statements, which fits in nicely with how C code is actually parsed. The significant downside to this is that if we have more compound statements than one-statement blocks, we can easily use up more lines than we hoped to gain by omitting unneeded braces.
Cuddling braces
This is the K&R style. This saves as many lines as possible.
Always using braces
This gives us readability in the form of consistancy. Every control block is formatted the same way, whether it has one statement or two. We know that when we see an outdent without a closing brace, it must be a line break in the middle of a statement. It also minimizes diff noise. When adding or removing a statement, the head of the block doesn’t change, nor does the indentation of the block.
Putting each brace on its own line
I don’t see any clear advantage to this.
Cuddling braces
Since we’re using braces as a matter of course, there’s no reason to call attention to them by putting them on their own line. We can save many lines by cuddling braces.
Let’s see what happens when we tackle this issue from the other side:
Do we cuddle braces or put them on their own line?
Putting each brace on its own line
As I said when discussing the GNU style, this may help by closely reflecting how C syntax works.
Omitting braces for one-statement blocks
Avoids wasting many lines.
Always using braces
May be useful for consistancy.
Cuddling braces
Saves lines.
Omitting braces for one-statement blocks
As mentioned above, saves as many lines as possible.
Always using braces
Adds consistancy and makes diffs more readable.
Conclusion
Personally, I recommend always using braces and cuddling them.