WordPress mistery solved!

Whenever I looked at source code from WordPress there was something that always irked me, and it was their recurrent use of this style of comparisons:

if(2 == $x)

I have been using the totally opposite style from the very first time I typed a comparison into a computer… and that was eons ago, so I can safely assume and proclaim that I have assimilated it as an instinctive act. I mean, when I think of comparison, this is what I think of:

if($x == 2)

And so I couldn’t understand WP’s style. It didn’t make sense… until I found this little paragraph semi-hidden at the very end of their Coding Standards document:

Another important point in the above example, when doing logical comparisons always put the variable on the right side, like above. If you forget an equal sign it’ll throw a parse error instead of just evaluating true and executing the statement. It really takes no extra time to do, so if this saves one bug it’s worth it.

So that was it!

But although that now makes sense — I have lost count of the amount of times I have been asked why does my code not work? it looks fine just to quickly find the reason was they were using an assignment instead of a comparison, I am not going to follow that guideline. It just doesn’t feel right, it looks awkward to me, unnatural and overengineered. Besides, I never make those mistakes, of course ;)

Coincidentally, I was testing Netbeans with PHP yesterday. It has a feature which warns you if your comparisons have unintentional side effects, which is the very same problem WP developers are trying to prevent with that coding rule. Maybe the solution is not to change the code style but to use tools like Netbeans.

Iceland EP

Comments

  • uau muy interesante forma para evitar un tipico error.. aunque es cierto que un IDE podria darnos el warning y listo ;)

    saludos

  • This is a habit I forced myself into years ago when i was programming mainly in C.

    I’m glad that there are better tools these days that warn one of issues like this, but in the end, if you make the l-value the constant, you’re pretty much guaranteed not to make this mistake in languages that dumbfoundingly use the equals sign ‘=’ as the /assignment/ operator.

  • Definitely! Although I think it’s a question of habit: I am used to write it the other way. But of course I recognize it’s way better the WP way. I mean – it’s IMPOSSIBLE to introduce a bug with that, because the interpreter is going to want to have some serious talk with you if you do it :)

  • At some point, painful as it is, you have to clean out bad habits, even habits you thought were good. I made this change a few years ago, and it has definitely helped me. Just making the chnage itself makes you less prone to the errors, because you immediately notice that 2 can’t = something, only ==.

  • I think what confuses/bothers/annoys me is that when you reach a line with that comparison style, you then have to temporarily ignore the constant on the left of the comparison, look for the variable on the right, and then jump back to the constant and mentally swap their places so that the classic “if $x equals blah…” takes shape again.