Why is this an issue?
While not technically incorrect, the omission of curly braces can be misleading, and may lead to the introduction of errors during maintenance.
Noncompliant code example
if (condition) // Noncompliant
executeSomething();
Compliant solution
if (condition) {
executeSomething();
}
Exceptions
When the body of an if
statement is a single return
, break
or continue
and is on the same
line.
Resources
- CERT, EXP19-C. - Use braces for the body of an if, for, or while statement
- CERT, EXP52-J. - Use braces for the body of an if, for, or while statement