Today I received the feedback from a code review one of my peers did of my code and they asked a question about a particular coding style technique I use. Specifically they asked why when comparing a variable to a literal value I always put the literal value on the left side of the operator? Put another way, why do I do this:
if(null == someVariable)
instead of this:
if(someVariable == null)
After all, this person pointed out, the second method is more easily read as "someVariable equals null" as opposed to "null is equal to the value of someVariable." I think that's a fair point and while I'm a big proponent of increasing the readability of code this is an instance where I'll take the hit.
if(someVariable = 0)
But it's not going to do what I want it to do.
Because I've been burned by this in the past I got in the habit of putting the literal on the left side of the operator. Because while I still won't get any compile-time love I will get a run-time error when the code says:
if(0 = someVariable)