8.6.a. When evaluating the equality of a variable against a constant, the constant shall always be placed to the left of the equal-to operator (==).


if (NULL == p_object)
    return (ERR_NULL_PTR);

Reasoning: It is always desirable to detect possible typos and as many other coding defects as possible at compile-time. Defect discovery in later phases is not guaranteed and often also more costly. By following this rule, any compiler will reliably detect erroneous attempts to assign (i.e., = instead of ==) a new value to a constant.

Enforcement: Many compilers can be configured to warn about suspicious assignments (i.e., located where comparisons are more typical). However, ultimate responsibility for enforcement of this rule falls to code reviewers.


Does rule 8.6.a apply to the relational operators (i.e., "greater than" and "less than") too, even though the danger of confusing "=" and "==" doesn't exist for these? That is, does this rule require if (x < 5) be written as if (5 > x)?

No. That is not required by this rule. When it comes to other types of comparisons, programmers or teams should do what they believe will be easiest to read and maintain.