5

I was browsing these PHP Coding Guidelines (a bit outdated but very interesting), and found this:

Condition Format

Always put the constant on the left hand side of an equality/inequality comparison. For example: if ( 6 == $errorNum ) ...

One reason is that if you leave out one of the = signs, the parser will find the error for you. A second reason is that it puts the value you are looking for right up front where you can find it instead of buried at the end of your expression. It takes a little time to get used to this format, but then it really gets useful.

I have been using ($var == 6) for years now and the idea of putting them the other way around is horrifying. But as mentioned, this takes a bit time to get used to and is supposed to have clear benefits. We are just writing up standard at our company so if we want to change this, this is the moment to do that. But I'd like to hear if other have experience with this particular format. Any opinions?

EDIT

I am interested in people's experience with making this switch. To me, and most likely to others, this looks like a big change that you need to get used to. But it looks interesting. So the question to those who have switched: can you recommend this change?

user852091
  • 3,070
  • 5
  • 23
  • 21
  • 6
    If you often make mistake like `=` instead of `==` - then you have much bigger troubles with your code, than just this. – zerkms Jul 21 '11 at 03:25
  • 1
    I prefer to let the IDE warn me instead of writing convoluted code like this. – rid Jul 21 '11 at 03:27
  • possible duplicate of [Variable before or after value in IF statement](http://stackoverflow.com/questions/6635712/variable-before-or-after-value-in-if-statement) – netcoder Jul 21 '11 at 03:28
  • Why is this downvoted? I do not see how this is a duplicate, if that is the problem. I specifically ask about experience with this, not if this is the same, as in the referenced duplicate. – user852091 Jul 21 '11 at 03:35
  • It takes some getting used to. It's a much better idea as it makes simple errors in your code obvious at run time. – Mark Tomlin Jul 21 '11 at 21:30

1 Answers1

6

6 == $var has a real advantage that $var == 6 does not. There is nothing horrifying about it at all, it just looks different. You should get used to it and use it. In fact, I haven't gotten used to it yet and I forget about it frequently. You really have to think about it and use it since the opposite is so common.

The advantage, in case you didn't know, is to prevent simple syntactical mistakes, thus:

if ($var = 6) {} //weird semantic error
if (6 = $var) {} //parse error
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
  • 2
    Modern IDEs highlight this as warning though. – zerkms Jul 21 '11 at 03:26
  • @netcoder very wise .. vim has syntax highlighting though. I think that zerkms is the one misusing the term here. – Explosion Pills Jul 21 '11 at 03:48
  • @tandu: you can think everything you want. But my editor (let's call that sort of software "an editor") helps me to write correct code, while yours doesn't. Looks like someone made better choice and another one ought to change order of operands to get the same result, don't you think so? – zerkms Jul 21 '11 at 04:13
  • I would be angry if my editor always highlighted if($var = 6){} as a warning... Doesn't that mean it will highlight while($row = mysql_fetch_array($result)){} as well? – Paul Jul 21 '11 at 04:38
  • @zerkms want to start another editor war? At any rate, there is absolutely no benefit to using `$var == 6` over `6 == $var`. Even if the technology is so down pat, why wouldn't you want to take as many free precautions as possible? – Explosion Pills Jul 21 '11 at 12:43
  • @Paulpro, netbeans will not highlight that as an error if you double wrap it, so u "let the ide know that ia your intent", so `if (($x=6))` shows no warning for me – chiliNUT Dec 05 '15 at 21:55