7

Possible Duplicate:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)?
Javascript minification of comparison statements

Ive been writing my if statements like this:

if(variable1 === 1){}
if(variable2 > 10){}
if(variable3 == "a"){}

But I remember reading somewhere (unfortunately I cant find that page anymore), that if statements are better off written like this:

if(1 === variable1){}
if(10 < variable2){}
if("a" == variable3){}

Where you put the variable on the right hand side of the expression.

Is this correct? And, if so, can anyone shed any light on why this is correct? Also, does this apply to all programming languages, or just javascript?

TIA

Salman A
  • 262,204
  • 82
  • 430
  • 521
Jimmery
  • 9,783
  • 25
  • 83
  • 157
  • 3
    Both are correct but the second is ugly. – lukas.pukenis Jan 09 '13 at 11:41
  • 5
    http://i.stack.imgur.com/xXkvA.jpg, from [this deleted answer](http://stackoverflow.com/a/2430307/508666). For people who cannot see deleted stuff: `"Yoda Conditions"— using if(constant == variable) instead of if(variable == constant), like if(4 == foo). Because it's like saying "if blue is the sky" or "if tall is the man"`. A.k.a. fugly as hell :) – PeeHaa Jan 09 '13 at 11:42
  • Both work, but apparently the second, uglier, option is better? Runs faster? – Jimmery Jan 09 '13 at 11:43
  • 1
    It does not run faster. It is uglier thus you slow down debugging and reading code – lukas.pukenis Jan 09 '13 at 11:44
  • I think the second one is good for cases when you're comparing the result of a very long expression to a fixed value. E.g. `false === myObject.myOtherObject.veryLongMethodName(way, too, much, parameters)` – Yoshi Jan 09 '13 at 11:44
  • 2
    Yoda Conditions -- http://stackoverflow.com/questions/5854317/what-is-the-difference-between-these-bcondition-null-and-null-bcondition – Salman A Jan 09 '13 at 11:44
  • 1
    @Yoshi In this specific case please simply use `!(my...` – Denys Séguret Jan 09 '13 at 11:49
  • why second one is faster ? – rags Jan 09 '13 at 11:49
  • @destroy Really? That's what you took from the example? – Yoshi Jan 09 '13 at 12:05

4 Answers4

5

1 === variable1 is same as the expression variable1 === 1 written in Yoda notation**: constant listed on left hand side, variable on the right hand side.

The main reason why some programmers choose to use it is to avoid the common mistake of writing if (a = 1) where the programmer actually meant if (a == 1) or if (a === 1). The following line of code will work but not as expected (a is assigned a value and if block will always get executed):

if (a = 1) {}

The same expression written the other way round will generate a syntax (or compile) error:

if (1 = a) {}

The programmer can immediately spot the error and fix it.

I do not like or use the Yoda notation. I try to keep my eyes open while coding.

** I am unable to find out the origin of this term.

Salman A
  • 262,204
  • 82
  • 430
  • 521
2

Some people may prefer to reverse the order of values in the if cause the second form is more protective. In fact if you miss to type an equal sign:

if (42 = myVar) { }

throws a syntax error at compile time, while

if (myVar = 42) { } 

evaluates the completion value of the assignment expression, 42 in this case, that is a truthy value in JavaScript.

Anyway a similar error today can be easily spotted with tools such as eslint... So there's no a real point for using first form.

Bruno
  • 5,961
  • 6
  • 33
  • 52
  • 1
    This is hardly a good reason to use the less readable form. – Denys Séguret Jan 09 '13 at 11:47
  • the accepted answer to this question suggests otherwise: http://stackoverflow.com/questions/5854317/what-is-the-difference-between-these-bcondition-null-and-null-bcondition – Jimmery Jan 09 '13 at 11:53
1

Both are correct but the second one is ugly and I haven't really seen much of it. It's the same as say

"If blue is sky"

instead of

"if sky is blue"

. Can't recal where I have read it :).

lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
  • 1
    [Closure Compiler](http://closure-compiler.appspot.com/home) sets values at the first place in `if` clauses. – VisioN Jan 09 '13 at 11:44
  • I tried. I noticed. Still looks like reading from right to left – lukas.pukenis Jan 09 '13 at 11:46
  • I wonder why Closure Compiler uses "Yoda conditions"? – Jimmery Jan 09 '13 at 11:47
  • 2
    http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html defines this as "Yoda Conditions" and uses the "if blue is sky" example. – pete Jan 09 '13 at 11:52
  • 1
    @Jimmery: I read somewhere (I think on Closure compiler's discussion forum) that `1 === somevar` compress better than `somevar === 1` and that is the _only_ reason. – Salman A Jan 09 '13 at 12:14
0

=== :

By definition, it's commutative, as it checks both objects are the same (same type, same value).

== :

== is more complex as there are conversions involved but the specification makes it clear :

A == B is equivalent to B == A, except in the order of evaluation of A and B

In other words, == is commutative.

This means you should simply use the less ugly and the most familiar to readers. That's the first one.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758