I like referring to the core guidelines for good vs bad practice and it seems from looking at a few examples in that document, they may favour the one line if statements. For example in F.56: Avoid unnecessary condition nesting they have the following example…
// Bad: Deep nesting
void foo() {
...
if (x) {
computeImportantThings(x);
}
}
// Bad: Still a redundant else.
void foo() {
...
if (!x) {
return;
}
else {
computeImportantThings(x);
}
}
// Good: Early return, no redundant else
void foo() {
...
if (!x)
return;
computeImportantThings(x);
}
It’s not that the rule is saying don’t use braces, you could use braces here, but an early return is exactly the kind of time I would personally reach for this style of if statement.
For me (and this is purely personal) I prefer this style for an early return regardless of the bracing style, (IMO) having the braces adds unnecessary visual clutter.
// Very cluttered
if (!x)
{
return;
}
computeImportantThings(x);
// Still cluttered
if (!x) {
return;
}
computeImportantThings(x);
// This is my favourite
if (!x)
return;
computeImportantThings(x);
// Uncluttered, but I like to see the return statement standout
if (!x) return;
computeImportantThings(x);
I should also add that the google style guide shared earlier also allows for this style of if statement (although by the sounds of it for legacy reasons).
That being said MIRSA says this
If you use single statements as bodies of if or if…else constructs, then attempting to change the body into a compound statement might result in logic errors and unexpected results. This rule ensures that braces are not missed or forgotten when attempting to turn a single statement body into a compound statement body. Failure to use compound statements might provide unexpected results and cause developer confusion. Use { }
braces to create compound statements. Use braces even when the if statement is simple and contains only a single statement.
I have to say from 12 years of developing C++ in this style, it’s not an issue I ever recall encountering.
IME overall I find people normally tend to have very positive comments about the JUCE style.
Personally I would urge any team that doesn’t have coding standard yet to work on one together using a number of sources to inform your choices, it can be a good team building exercise. Once you’ve gone with something though try to stick to it, no need to keep making changes back and forth. Occasional updates particularly when there is ambiguity is fine.