Skip to content

Commit

Permalink
CODING_STYLE: Section about conditional statement
Browse files Browse the repository at this point in the history
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 <arei.gonglei@huawei.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
  • Loading branch information
gongleiarei authored and Michael Tokarev committed Aug 15, 2014
1 parent 30dc600 commit 2bb0020
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions CODING_STYLE
Expand Up @@ -91,3 +91,17 @@ Mixed declarations (interleaving statements and declarations within blocks)
are not allowed; declarations should be at the beginning of blocks. In other
words, the code should not generate warnings if using GCC's
-Wdeclaration-after-statement option.

6. Conditional statements

When comparing a variable for (in)equality with a constant, list the
constant on the right, as in:

if (a == 1) {
/* Reads like: "If a equals 1" */
do_something();
}

Rationale: Yoda conditions (as in 'if (1 == a)') are awkward to read.
Besides, good compilers already warn users when '==' is mis-typed as '=',
even when the constant is on the right.

0 comments on commit 2bb0020

Please sign in to comment.