Search code examples
c++cprogramming-languages

When using if(...) , why is this considered a good programming practice?


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.


Solution

  • 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.