|Synopsis:||All flow control primitives (if, else, while, for, do, switch) shall be followed by a block, even if it is empty|
bResult = true;
// Count number of elements in array.
for (unsigned nIndex = 0;
anArray[nIndex] != 0;
There are three exceptions to this rule:
- an "else" statement may directly followed by another "if"
- statements following a case label do not have to be enclosed in a block, unless they define a variable.
- An if clause, followed by a single statement, does not have to enclose that single statement in a block, provided that the entire statement is written on a single line.
Of course the exception is intended for those cases where it improves readability. Please note that the entire statement must be a one-liner (of reasonable length), so it is not applicable to complex conditions. Also note that the exception is only made for if (without else), not for while etc.
Rationale for the exception: code readability can be improved because the one-liner saves vertical space (by a factor of 4). The lurking danger in later maintenance, where someone might add a statement intending it to be subject to the condition, is absent in the one-liner.
if (getToken() == ASSIGN) v = expr(true);
if (failure) throw VerySorry(errno);