Skip to content

Commit 2bb0020

Browse files
gongleiareiMichael Tokarev
authored and
Michael Tokarev
committed
CODING_STYLE: Section about conditional statement
Yoda conditions lack readability, and QEMU has a strict compiler configuration for checking a common mistake like "if (dev = NULL)". Make it a written rule. Signed-off-by: Gonglei <[email protected]> Reviewed-by: Eric Blake <[email protected]> Signed-off-by: Michael Tokarev <[email protected]>
1 parent 30dc600 commit 2bb0020

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

CODING_STYLE

+14
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,17 @@ Mixed declarations (interleaving statements and declarations within blocks)
9191
are not allowed; declarations should be at the beginning of blocks. In other
9292
words, the code should not generate warnings if using GCC's
9393
-Wdeclaration-after-statement option.
94+
95+
6. Conditional statements
96+
97+
When comparing a variable for (in)equality with a constant, list the
98+
constant on the right, as in:
99+
100+
if (a == 1) {
101+
/* Reads like: "If a equals 1" */
102+
do_something();
103+
}
104+
105+
Rationale: Yoda conditions (as in 'if (1 == a)') are awkward to read.
106+
Besides, good compilers already warn users when '==' is mis-typed as '=',
107+
even when the constant is on the right.

0 commit comments

Comments
 (0)