views:

164

answers:

8

Where is the best place to put the constant in a condition? Left side or Right side?

I personally use in the right side:

if($value > 23)
{
}
+4  A: 

The right side. The left side is a tradition in C/C++ because people sometimes forget and uses "=" instead of "==" and putting the const on the left side causes a compilation error in this case.

Otávio Décio
Isn't that an argument for putting the constant on the left?
Piskvor
The question was tagged "language agnostic" and not all languages suffer the same issue as C/C++
Otávio Décio
+4  A: 

A lot of people will say the LHS because it prevents you doing subtle and damaging things like if (foo = KBAR) (note the lack of '==') but I always find that jarring for readability.

annakata
Right. You might also want to mention this: http://okasaki.blogspot.com/2008/09/less-than-vs-greater-than.html
ShreevatsaR
A: 

I prefer left side, as it prevents accidental assignments like so:

// when using = instead of == this can result in accidental assignment
if ($value == null) {}

// $value cannot be accidentally assigned this way 
if (null === $value) {}

NOTE: from reading other answer I understand when using compiled languages, this could get you in trouble. I still prefer using this, as my main language is PHP. For compiled languages, please refer to the answers others have already given.

Aron Rotteveel
Please clarify the downvote; the question did not explicitly state this is a question related to compiled languages and the answer is not incorrect (perhaps subjective, but that's a whole other thing)
Aron Rotteveel
+1  A: 

Put the condition on the right side, since that's the "natural" place, and rely on your compiler to generate a warning if you accidentally use = instead of ==.

Alnitak
+1  A: 

Does it really matter? It may help you if you keep a convention, but whether it is to keep the constants on one side, or to always use the <, <= operators and avoid the >, >=; that really depends on you.

It surely doesn't matter to the compiler/interpreter, and modern compilers should give a clear warning when you accidentally write "set to" (=) instead of "does it equal" (==), as pointed out in ocdecio's post.

David Hanak
+2  A: 

depends:

if (23 <= i and i <= 40)

but i would prefer right side, it reads more naturallly

Peter Miehle
A: 

Always use < (and <=), never use > (or >=), and avoid languages which can't distinguish between assignment and equality.

First part of the rule means that numbers in your conditions occur in their usual order, smallest on the left, largest on the right. This is a great help when your conditions contain multiple terms (eg 3<x && x<=14)

Second part of the rule means letting a compiler sweat about things a compiler is good at (such as spotting how many ===== signs you've typed).

And I make these assertions forcefully and positively sure and certain in the knowledge that this is only my opinion and that there isn't a right or wrong answer.

Regards

High Performance Mark
Why this is down-voted?
Sunny
I agree with the argument about left-right ordering of values, and the resulting consistent use of < and <=. Those can be applied regardless of whether your employer, client, professor, significant other, etc. mandates the use of e.g. C.
joel.neely
+1  A: 

The answer, as always, is "it depends". In most cases, it reads more naturally to put it on the right, as in the OP's example. In other cases, particularly compound statements that check to see if something is in a range (see Peter Miehle's example), it can go either way. I think you should use whichever makes the statement clearer to any future programmers who happen across your code. If there is no clear difference in readability, I recommend defaulting to putting it on the right, since that is what most people expect (principle of least surprise). As many have mentioned already, any decent compiler nowadays will warn you if you attempt to perform an assignment inside an if statement (you can usually silence this warning by putting an extra set of parentheses around the assignment). Also, it has been mentioned that some JIT or interpreted languages might make it hard to find this problem without the constant-on-the-left trick, but IIRC, many of them will also emit a warning in this case, so if you run them with warnings treated as errors, it will help you catch that problem.

rmeador