I’ve noticed a subtle trend among some developers who consistently place the literal value of a boolean equality expression on the left hand side of the expression.
Example:
//Style #1, the style in question
if (null == object.Value || 0 == object.Value) doSomething();
//Style #2, the way I've always written it
if (object.Value == null || object.Value == 0) doSomething();
Is there any measurable advantage gained by writing the boolean expressions like #1? I can’t see why it would evaluate any faster at runtime, but I wonder if perhaps some compilers can optimize #1 more readily than #2? If so, please explain which compilers and why.
I’ve always considered this simply a matter of personal taste (perhaps there is a readability argument to be made), but I’ve seen enough code written like #1 that I wonder if there is more to it than I’m aware of.
Some developers follow and advocate (1) so that if they accidentally mis-type the comparison (
==) as assignment (=), then the compiler will issue an error during compilation. In (2) the same mis-typing will not issue any compilation error, but it will result in flawed program logic because theifwill always resolve totruedue to the assignment.