Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does order of operands in equality matter? (e.g. 1 == x vs x == 1)

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?

like image 966
Nathan Avatar asked Nov 16 '12 01:11

Nathan


People also ask

Does the order of operands matter when using the operator?

The precedence and associativity of C operators affect the grouping and evaluation of operands in expressions. An operator's precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first.

Do all comparison operators have the same precedence?

Precedence RulesAll comparison operators have equal precedence, and all have greater precedence than the logical and bitwise operators, but lower precedence than the arithmetic and concatenation operators.

What is the order of precedence of operators?

In the United States and in France, the acronym PEMDAS is common. It stands for Parentheses, Exponents, Multiplication/Division, Addition/Subtraction. PEMDAS is often expanded to the mnemonic "Please Excuse My Dear Aunt Sally" in schools.

Which operator has the highest order of precedence ()?

The operator precedence is responsible for evaluating the expressions. In Java, parentheses() and Array subscript[] have the highest precedence in Java. For example, Addition and Subtraction have higher precedence than the Left shift and Right shift operators.


2 Answers

Commonly done in languages like C / C++ so you can't accidently do

if (a = null) {
    // sets a to null and everyone is happy.
    // but probably meant to check if a is null or not.
    // via (a == null)
}

if (null = a) {
    // won't compile
}
like image 95
John3136 Avatar answered Oct 01 '22 22:10

John3136


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.

like image 38
John Avatar answered Oct 01 '22 21:10

John