For the most-part this is personal preference, however there are some things to consider.
Possible Bugs
While it can be argued that bugs caused by forgetting to add-in braces are rare, from what I've seen that they do happen occasionally (not to forget the famous IOS goto fail bug). So I think this should be a factor when considering your code style (some tools warn about misleading-indentation, so it depends on your tool chain too).
Valid Code (that reads like it might be a bug)
Even assuming your project doesn't suffer from such bugs, when reading code you may see some blocks of code that look like they could be bugs - but aren't, taking some of your mental cycles.
We start with:
if (foo)
bar();
A developer adds a useful comment.
if (foo)
// At this point we know foo is valid.
bar();
Later on a developer expands on it.
if (foo)
// At this point we know foo is valid.
// This never fails but is too slow even for debug, so keep disabled.
// assert(is_valid(foo));
bar();
Or adds a nested block:
if (foo)
while (i--) {
bar(i);
baz(i);
}
Or uses a macro:
if (foo)
SOME_MACRO();
"... Since macros may define multiple lines of code, does the macro use do {...} while (0)
for multiple lines? It should because its in our style-guide but I better check just in case!"
The examples above are all valid code, however the more content in the code-block, the more you need to read to ensure there aren't any mistakes.
Maybe your code-style defines that multi-line blocks require a brace (no matter what, even if they're not code), but I've seen these kinds of comments being added in production code. When you read it, there is some small doubt that whoever last edited those lines forgot to add a brace, sometimes I feel the need to double-check is working as intended (especially when investigating a bug in this area of the code).
Diff Noise
One practical reason to use braces for single lines is to reduce diff noise.
That is, changing:
if (foo)
bar();
To:
if (foo) {
bar();
baz();
}
... causes the conditional line to show up in a diff as being changed, this adds some small but unnecessary overhead.
- the lines show up as being changed in code-reviews, if your diffing tools are word-based you can easily see that only the brace changed, but that takes more time to check then if the line didn't change at all.
Having said that, not all tools support word-based diffing, diff (svn, git, hg... etc) will show as if the entire line changed, even with fancy tools, sometimes you may need to quickly look over a plain line-based diff to see what changed.
- annotation tools (such as
git blame
) will show the line as being changed, making tracking the origin of a line more step to find the real change.
These are both small, and depend on how much time you spend in code-review or tracking-down which commit changed lines of code.
A more tangible inconvenience of having extra lines changes in a diff, theirs higher likely-hood that changes in the code will cause conflicts which merging and need to be manually resolved.
There is an exception to this, for code-bases that have {
on its own line - it's not a problem.
The diff noise argument doesn't hold if you write in this style:
if (foo)
{
bar();
baz();
}
However this isn't such a common convention, so mainly adding to the answer for completeness (not suggesting projects should use this style).
if
block is only ever one line, it doesn't matter. If you need to add more lines, it should be another function which reduces theif
block back to one line (the function call). If you adhere to that, then braces simply do not matter -- at all.return (page==null) ? "" : includePage(mode,page);
if he's that much into getting terse... I've thought no-brace style is cool until I started developing apps professionally. Diff noise, possible typo bugs etc. The braces, being there all the time, save you the time & overhead you'd need to introduce them later on.