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.