5

I've come across some code that flips how a condition is checked and was wondering why this would be done aside from a weird personal quirk. I've never seen any text books use it nor have I seen any sample code done this way.

// why do it this way?
if (5 == myValue) 
{
    // do something
}

// instead of:
if (myValue == 5) 
{
    // do something
}

I've only seen this way for == operand but not for any other operands.

Praetorian
  • 106,671
  • 19
  • 240
  • 328
Willam Hill
  • 1,572
  • 1
  • 17
  • 28
  • why not? The reason is: because it expresses intent and works. Exactly like the opposite – sehe Nov 26 '13 at 23:01
  • I have herd that they write constant at LHS for safety, because if you write `5 = val` instead of `5 == val` by mistake, then you get compiler error instead of surprise that you would get if you write `val = 5` by mistake. But I don't believe that.. because if you are able to remember to write constant at LHS then you must also be able to remember to write `==` correctly instead of `=`. And in my case `5 = val` does Not improve readability... I don't like it.. – Sam Nov 27 '13 at 06:57
  • @SAM except for typos. The yoda conditional form is more resistant to typos. As another example, in English, I know the difference among "to", "too", and "two", but it is still easy to type the wrong one by mistake. – Tim Seguine Nov 27 '13 at 13:21
  • @Tim Compiler warnings should catch typos like that. – hyde Dec 04 '13 at 06:50
  • @hyde many people consider errors better than warnings. Warnings are usually a mistake. Errors are always a mistake. Some people think that is worth something. But your point is valid, and everyone should compile with warnings on. I feel it is a decent way to express intent. I do not use it personally. In PHP, for example, I would argue stronger for it, since there are valid uses of assignment in a conditional. – Tim Seguine Dec 04 '13 at 08:51
  • @Tim At least with *gcc*, to get a warning free assignment in a conditional, the assignment should be put in extra parenthesis: `if ((a=b))`... but to make intent extra clear, I personally would prefer `if((a=b) != 0)`. And it's simple to convert warnings to errors for those who so prefer. – hyde Dec 04 '13 at 11:18

2 Answers2

14

Some people like doing that because if you mess up and type a single-equals instead of a double-equals, "if (val = 5)" is only a compiler warning, while "if (5 = val)" is an error (you can't assign to a constant).

I think it's sort of ugly, personally, and that you should be checking your warnings just as much as your errors, but that is the reason for that convention (which is thankfully not universal, but is at least moderately widespread. It is also true that it might not universally have been treated as even a warning by much older compilers, and this convention has been around for a long time.)

Fun fact: I was just reading Code Complete (Second Edition), and Steve McConnell agrees with me, stating his personal preference is "to use number-line ordering and let the compiler warn me about unintended assignments". Steve McConnell by definition knows what he's talking about.

neminem
  • 2,658
  • 5
  • 27
  • 36
  • 5
    I like converting warnings to errors. – ChrisInEdmonton Nov 26 '13 at 17:07
  • 8
    This may be helpful, too: http://en.wikipedia.org/wiki/Yoda_conditions – wjmolina Nov 26 '13 at 17:08
  • This is just the half story. If you have set val in the example to null this makes a big difference. In this case the val == 5 will crash in some languages at runtime without warning or error while compiling. If you want an almost identical, safe expression you would have to use if (val != null && val == 5) – Wienczny Nov 26 '13 at 23:44
  • Yes it is seriously ugly... :( I don't like it. I believe that if you are able to remember to write constant at LHS then you must also be able to remember to correctly write `==` instead of `=`... – Sam Nov 27 '13 at 07:02
  • 1
    Moreover, compilers have option to turn individual warning to errors. For example MSVC: `/we4706` – Evgeny Panasyuk Nov 27 '13 at 09:22
4

Some folks do it for readability when myValue is deemed less interesting than 5; it puts the constant value in a more prominent place. There is no practical reason for it - purely a judgment call on the part of the coder.

Mike Woolf
  • 1,210
  • 7
  • 11
  • 4
    Completely agreed here. I used to do this back when compilers didn't warn/I didn't always have the flags configured. Nowadays I use it for the reason stated only: when it's more readable (e.g. when the 'rhs' becomes a larger expression: `if (0 != some_strange_api_call(with, quite, a, lot, of, parameters))`. – sehe Nov 26 '13 at 23:03