Does order of operands in equality matter? (e.g. 1 == x vs x == 1)
google-closure-compiler, javascript
Solution
The compiler switches the order for a very simple reason: it compresses better with gzip. The compiler doesn't care a wit about improving comprehension or making it easier to edit. By switching the order common comparisons such as "if (x == null) ... if (y == null) ..." become "if (null == x) ... if (null == y) ..." Gzip finds "if (null ==" and is able to replace it with a single token. It isn't a big improvement, but it adds up in a large code base.
Problem
So I'm trying out the Google Closure Compiler and I've noticed that it switches all my equality parameters so that the variables are always on the right side of the comparison. So now instead of `typeof XMLHttpRequest=="undefined"` I have `"undefined"==typeof XMLHttpRequest` and I have `if(null!==a)` instead of `if(a!==null)`, just as some examples. I know they accomplish the same thing, but it's just not the style I'm used to. Is there some sort of benefit that you get for having these switched? I can't see how there would be. Can someone explain to me why the Closure Compiler decides to do this? Is it just a preference of whoever wrote that part of Closure? Edit: To clarify, people are telling me why it might be considered good coding practice. That's fine, but this is after compilation. Is there a performance benefit or is the Closure Compiler just trying to prove a point?