File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 8787
8888## Miscellaneous examples
8989
90- ### Boolean expression
90+ ### Boolean expressions
9191
92- Use explicit checks with added parenthesis like below.
92+ - Non-boolean values (pointers, counts, status/enum codes) should use explicit
93+ comparisons.
94+ - Boolean flags (integers that hold only 0/1, e.g. flags like ` is_* ` /` has_* ` ,
95+ the return value of a predicate function, etc.) should be tested directly.
96+ - Add parentheses around every comparison in compound expressions (excluding
97+ direct boolean tests) to ensure correct operator precedence.
98+ - Negation with ` ! ` takes no parentheses when applied to a single flag or
99+ predicate call (` !is_enabled ` ).
93100
94101Good
95102``` C
96103 if (ptr == NULL ) {
97104
98105 if (a == 0) {
99106
100- if ((ret == UCS_KH_PUT_BUCKET_EMPTY) ||
101- (ret == UCS_KH_PUT_BUCKET_CLEAR)) {
107+ if (is_enabled) {
108+
109+ if (!uct_iface_is_reachable(iface)) {
110+
111+ if ((ret == UCS_KH_PUT_BUCKET_EMPTY) || (ret == UCS_KH_PUT_BUCKET_CLEAR)) {
112+
113+ if (is_enabled || ((a == 2) && (b > 0))) {
114+ ```
115+
116+ Bad
117+ ```C
118+ if (!ptr) {
119+
120+ if (!a) {
121+
122+ if (is_enabled == 1) {
123+
124+ if (uct_iface_is_reachable(iface) == 0) {
125+
126+ if (!(uct_iface_is_reachable(iface))) {
127+
128+ if (ret == UCS_KH_PUT_BUCKET_EMPTY || ret == UCS_KH_PUT_BUCKET_CLEAR) {
129+
130+ if (is_enabled || a == 2 && b > 0) {
102131```
103132
104133### Variable definition
You can’t perform that action at this time.
0 commit comments