-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
Introduction
Wiredtiger's current C Style guide doesn't require curly braces for multiline if/for/while statements:
Single statement blocks in conditions and loops do not use braces unless required to avoid ambiguity.
Proposed style change
- If an if, while, or for statement spans more than one line, the body must be enclosed in braces.
- For single-line statements the preferred style is not to use braces but they can be used if it improves readability or expressiveness.
Exceptions
- If for any reason braces can't be used, there must be a comment explaining it.
Rationale
- Helps prevent common mistakes like the dangling else problem.
- Enhances code clarity by making block boundaries explicit.
- Makes code easier to maintain: less chance to introduce an error by adding or removing an adjacent line.
- With auto-formatting, an introduced error can be harder to spot when there are no braces.
Examples
Simple statement
Instead of
if (condition) myFunction();
use
if (condition) myFunction();
or
if (condition) { myFunction(); }
Multiline comment
Instead of
if (condition) /* * Comment */ myFunction();
use
if (condition) { /* * Comment */ myFunction(); }
If-Else
Instead of
if (condition)
myFunction();
else
myFunction();
use
if (condition) {
myFunction();
} else {
myFunction();
}
Nested statements
Instead of
if (condition1) if (condition2) myFunction1(); else /* What is this else even for? */ myFunction2(); for (init; condition1; incr) if (condition2) myFunction1(); if (condition1) while (condition2) myFunction2();
use
if (condition1) { if (condition2) { myFunction1(); } } else { myFunction2(); } for (init; condition1; incr) { if (condition2) { myFunction1(); } } if (condition1) { while (condition2) { myFunction2(); } }