tags:

views:

357

answers:

8

For all of my programming life, I've always used the following notation style in my code:

n < 10
"".length == 0
ptr != NULL

etc.

In other words, I put the variable on the left of the comparison, and the constant on the right.

Recently, I've noticed - here on stackoverflow and it some third party code - people doing the opposite:

10 > n
0 == "".length
NULL != ptr

So my question is, is this simply a completely subjective alternative style that I've just not noticed before, or is there some objective beneficial reason for putting the variable on the left and constant on the right?

+10  A: 

In C/C++, it's legal to write:

if (x = 0) // Typo; should really be ==

which doesn't do what you want. Most compilers will warn you about it if you have sensible warning levels, but some developers prefer the safer form:

if (0 = x) // Error!

In languages such as C# and Java, conditions have to be truly Boolean expressions anyway - your preference is generally accepted to be more human-readable, and there's no point in catering for a deficiency in C/C++ when coding in other languages.

Jon Skeet
+2  A: 

Whether it has an objective benefit depends on the language. In C/C++ since many expressions implicitly convert to boolean expressions, there's a common defect that occurs when a comparison is intended but an assignment expression is used:

if (x = 1) ...  // probably a bug

if (1 = x) ...  // a syntax error

Personally, I find the 2nd form somewhat jarring to read, but I still try to use it because it has caught out bugs for me.

Michael Burr
+1  A: 

In C you can help protect from typos by reversing them:

if 0 == counter

because the common mistake of using a single = instead of == will be caught by the compiler.

idontwanttortfm
A: 

One historical reason may be that if you do (wrongly) in C:

if (0=variable)
{
...
}

(assignment instead of comparison) you'd get an error - you wouldn't get it if you had wrote:

if (variable=0)
{
...
}

Apart from this, it's just a matter of taste.

Edit: modern compilers should show at least a warning for the latter expression.

friol
A: 

This style is subjective for any language where == is used for equality. Most books and almost all code style guides I have seen published by businesses suggest that programmers use the format you use.

From a readability perspective, it is much better to say:

if (n > 5) => If n is greater than 5

than:

if (5 < n) => If 5 is less than n

Even if you just think about that for a moment, the first option seems much easier to understand, even though they're the same thing.

Compilers will evaluate your condition regardless of the order, so there isn't really a compelling objective reason to use the second option.

Ed Altorfer
+5  A: 

As stated before, there are some compiler error considerations for turning it around, as constants can't be LHS of an assignment.

Also, it becomes useful when the variable being tested could potentially be null, and the comparison is a function on the object. In Java:

if ( str.equals("Stuff") ) // dangerous comparison

if ( "Stuff".equals(str) ) // this works even if str is null

Bill James
+1  A: 

Here is a related discussion on the subject.

itsmatt
Is it close enough to justify me closing this one do you feel?
David Arno
Probably, David. The only reason I knew about the original post was because I wrote an answer to it. Sometimes it is hard to find the dupes on this system.
itsmatt
A: 

x relop constant

is much easier on the mind. Maybe that's because I(we) speak a Left->Right language, not Hebrew or another R->L language.

Paul Nathan