1

Possible Duplicate:
What is the difference between these (bCondition == NULL) and (NULL==bCondition)?

please see the below code:-

if(myVariable==5)
   {
     //some logic
   }

one of my friends says that this is not a good way to write the code as its not per guidelines, however he dont have any reason to it. Is there is a chance of exception with above code or accidental modification?? According to him the better way would have been

if(5==myVariable)
       {
         //some logic
       }

please let me know which is a better way and why??? Do provide links if u have any.

Community
  • 1
  • 1
amod
  • 4,190
  • 10
  • 49
  • 75
  • Either way is valid, but I have seen the former used 99% of the time. – ssell Aug 08 '11 at 14:51
  • 1
    Basically, all answers in this post are saying one thing: both statements are the same, just stick with the one which is more readable. The most common and used one is if(variable == 5), which also happens to be more readable (you can see what are you considering, and what are you checking it against). Btw, both ways are correct. – Samuele Mattiuzzo Aug 08 '11 at 14:54
  • 2
    They are called [Yoda conditions](http://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined/2430307#2430307) and are considered silly. – Bo Persson Aug 08 '11 at 15:12
  • @Bo Persson: First time I have heard that. But I like it. – Martin York Aug 08 '11 at 15:29

10 Answers10

10

The only reason to write:

5 == variable

instead of

variable == 5

is that in the former case if you incorrectly put an assignment (single =) in place you will get a compile time error because you are trying to overwrite a constant.

However any decent compiler will give you a warning if you do:

if (variable = 5)

so IMHO it's not worth worrying about. I always use the latter if (var == num) form.


However in Java there is a common pattern that is worth using. When testing a string for equality, one should use:

if ("constant".equals(variable)) { ... }

instead of:

if (variable.equals("constant")) { ... }

since the latter can trigger a null pointer exception, and the former cannot.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
4

The reversal is preferable in some languages like C, where

if (x = 5) {
   ...
}

would accidentally assign x to the value 5 if you mistype = instead of ==. By reversing the two arguments the compiler would rightfully object to you reassigning the value 5.

Unlike C/C++, for languages such as Java it's not such an issue since

if (x = 5) {
   ...
}

isn't a valid statement. I still follow the above practise however. I don't have to rethink when swapping between Java and C/C++.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • It is not less of an issue in Java for boolean expressions. If I follow the Java Spec correctly, then something like `boolean b = ...; if (b=true)` is valid code (same for C#, btw). – Sebastian Mach Aug 08 '11 at 15:34
2

Both are same. select which ever you find more readable.. I would go with first

jmj
  • 237,923
  • 42
  • 401
  • 438
2

For that specific case, it's technically safer to do 5 == variable because then if you accidentally say 5 = variable the compiler will complain. On the other hand, variable = 5 is perfectly legal.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • It doesn't stop me from writing `5 == variable` when I meant to write `5 != variable`. – Bo Persson Aug 08 '11 at 15:24
  • That's why I said *technically* because it's one less use-case you have to deal with. Your argument is true for `variable != 5` a well. Either way, worrying about this kind of stuff seems a bit paranoid, in my opinion. – Jeremy Aug 08 '11 at 15:30
2

This convention is to prevent you from accidentally writing if (myVariable=5) when you mean if (myVariable==5).

1

For ==, it doesn't matter a bit what order you do it in.

Your friend mentioned "guidelines"-- perhaps it's a business rule? Albeit an arbitrary and semi-pointless one...

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
1

It's immaterial. I prefer the first one, because it's more readable.

I hope you're aware that using == is not always correct for reference types. In those cases you should prefer equals. THEN it matters, because you want to avoid null pointer exceptions. It's best to dereference the instance that cannot be null in that case.

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

There is no such guildline I found. First approach should be followed because it is more readable.

Kamahire
  • 2,149
  • 3
  • 21
  • 50
0

i think if you what to know if they are equals it's fine then. but if you want to know about string, then i more appropriated use '.equals'.

e.g:

object.equals("something");
Valter Silva
  • 16,446
  • 52
  • 137
  • 218
  • or preferably, `"something".equals(object)`, since this is one of those cases where it's better to put a constant on the right-hand side of the `.equals()`. – Alnitak Aug 08 '11 at 14:56
0

The only thing I can think of is to avoid possible syntax errors of the kind constant = expression, or to avoid mangled operator overload in C++. In Java both expressions are syntatically valid and picking one over the other is a matter of choice.

Perception
  • 79,279
  • 19
  • 185
  • 195