Skip to content

Commit 1275252

Browse files
DOCS: Update boolean expressions section in CodeStyle.md (#11555)
Signed-off-by: Guy Ealey Morag <gealeymorag@nvidia.com>
1 parent f933a65 commit 1275252

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

docs/CodeStyle.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,47 @@
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

94101
Good
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

0 commit comments

Comments
 (0)