0

Possible Duplicate:
How to check for equals? (0 == i) or (i == 0)
Is there a difference between i==0 and 0==i?

I have seen many a times that people use the if(condition) as if(0==x) instead of if(x==0). It is said to be a good practice but can someone explain why so? What difference does it make ? Rather it decreases the readability in my opinion.

Community
  • 1
  • 1
h4ck3d
  • 6,134
  • 15
  • 51
  • 74
  • Try imagining you make the mistake of using = instead of == (and also think of cases where you compare against something else than 0). – PlasmaHH Sep 24 '12 at 09:03
  • 1
    It is not good practice, it is dinosaur practice. People who programmed before 1990 used to write code like that, thinking they were smart. In 1990 Borland released a compiler raising a warning against "possible incorrect assignment" and since then every compiler has a warning against this. Writing obfuscated code is a bad solution, using proper static checking tools is a better, modern idea. – Lundin Sep 24 '12 at 09:32
  • Although it's also worth bearing in mind that people who have more difficulty reading code in which the equality is the other way around (including me), don't properly understand what "equals" means. So the `0==x` code is only obfuscated to the extent to which the reader is interpreting the `x==0` code on guesswork. Which, given how humans read quickly, is a considerable extent, but you'd hope less for programmers reading code than for average text. – Steve Jessop Sep 24 '12 at 09:33

5 Answers5

10

The fact that it decreases readability is purely subjective. (I feel the same way though, but that's because I've been dealing with x==0 more than the other way around, so I got used to it).

It's done to prevent accidental assignment:

if(0=x)

will yield a compiler error,

if(x=0)

won't.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
2

Personally, i don't prefer this practice. but it became popular because of following reason:

To differentiate between assignment operator and boolean condition.

if(x = 0)

This lines serves two purpsose:

  1. Assigns 0 to x.
  2. makes the condition false cause it is equivalent to if(0)

To avoid these mistakes, some people prefer if(0 = x) which will result in compile time error.

Azodious
  • 13,752
  • 1
  • 36
  • 71
1

It simply avoids a typing mistake where you would type if (a = 0) instead of if (a == 0). Using the Yoda style you'd get a compile error if you wrote if (0 = a) instead of if (0 == a).

On the other hand, it doesn't prevent cases of if (b = a) and b is another variable. No silver bullet.

Better is to use -Wall -Wextra when compiling. Add -Werror if you want to be paranoid and treat all warnings as errors (it is better to do this)

Mihai Maruseac
  • 20,967
  • 7
  • 57
  • 109
0

Yes, it less readable (imho), but this allow to avoid common mistake with assign instead of checking its ==

if( 0 = variable ) { // Fail 0 is constant

Fun fact: some calling this "Yoda conditions"

Galimov Albert
  • 7,269
  • 1
  • 24
  • 50
0

No, It's just your habit to read the things like variable name in left and constants in right. But actually it will give you exact code that if you leave

if (0=x)

instead of

if(0==x) by mistake then it will throw you an error which you can easily modify but if in reverse case its very difficult to debug

Omkant
  • 9,018
  • 8
  • 39
  • 59