+1  A: 

I personally think "value == 0" is more readable (English: "value is equal to 0" rather than "0 is equal to value").

I also think that putting in a single "=" rather than two is a mistake that you'll catch if you have any decent testing environment.

Matthew Iselin
Not only that, any decent compiler will warn about an untested assignment in a conditional statement.
Greg Hewgill
+2  A: 

It's also useful in Java like languages when doing something like

private static final String MY_CONSTANT = "some_value";

//...
if (MY_CONSTANT.equals(valueFromSomeWhere)) {
  // do something...
}

If you have it the other way around, you have to first check if valueFromSomeWhere is null or not and not do the .equals() call (otherwise you'll get a NullPointerException). If you put the constant first, you don't have to.

madlep
A: 

I side with the more readable value == 0; you are asking if value is 0, not if 0 is value.

Many compilation environments already warn you if you do if (value = 0), and so our coding standard says to do it GCC's way if you are going to do this at all: if ((value=0)). Actually, our coding standard wouldn't allow that statement anyway, but we do allow things like if ( (status=do_command()) == OK )

Since the whole "put constants on the left" is just a coding standard rule, we figure that asking for the extra parenthesis is just as good, and gets enforced by our compiler.

Chris Arguin
+1  A: 

You can't assign a value to a constant. So if you write

  • if (a = 0)

when you meant

  • if (a == 0)

you've just assigned the value 0 to A.

But if you write

  • if (0 = a)

The compiler will picket you and refuse this outrageous request until you fix it. While some testing tools and compilers will flag you if you use the construct "if (a = 0)" to warn you that you might be making a mistake, by using the (0 == a) format it doesn't matter, you can't ever even get into trouble because the misuse (0 = a) won't be tolerated by the compiler in the first place, it prevents you from making the error at all, whether or not you have the additional potential error check feature.

That's one reason some people like Pascal, because = means only one thing: comparison; to assign a value you have to use := so you can't do it by accident.

Paul Robinson

A: 

I generally prefer to write the variable on the left, like that :

if ($value == 0) {}

But I'm using the other way around more and more :

if (0 == $value) {}

This, just to avoid the case of forgetting an = sign, and spend load of time searching for a stupid bug : as you can't assign a value to a constant, forgetting an = in the second case causes an error at compile time (or the equivalent of compile-time, as I work with PHP).

The second solution is recommended by several frameworks, in PHP, and I see it used more and more -- I wouldn't say it's "best practice", but it's definitly a good one : doesn't take more time to write or understand, and can sometimes really help you not lose loads of time !

(It takes some time to get used to writing conditions this way, but once you do it, it's not any harder than the first solution)

Pascal MARTIN
As a side note after ready comment from Greg Hewgill : in PHP, I don't have a compiler that gives such kind of warnings ; so, this syntax is a plus. (and, *yes*, PHP is a real language... )
Pascal MARTIN
PHP is a real language... It's a PITA to work with most of the time. Also, PHP has a compile-time, it's just that it's directly before runtime.
Matthew Scharley