Why this matters
Omitting curly braces in control structures can lead to errors when modifying the code. Always use `{}` for clarity.
Use curly braces `{}` around all control structures (`if`, `for`, `while`, `do`-`while`) to improve readability and prevent logic errors.
Omitting curly braces in control structures can lead to errors when modifying the code. Always use `{}` for clarity.
Side-by-side examples engineers can pattern-match during review.
if (condition) // Noncompliant
executeSomething();
checkSomething();if (condition) {
executeSomething();
checkSomething();
}if (condition) // Noncompliant
executeSomething();
checkSomething();if (condition) {
executeSomething();
checkSomething();
}From the same buckets as this rule.
Check if loops use equality operators (== or !=) in termination conditions. These can lead to infinite loops if the condition is never met exactly. Instead, use relational operators like < or > for safer loop termination.