LukeWilliams
LukeWilliams

Reputation: 13

What is the preferred order for operands in boolean expressions?

Is there any benefit to structuring boolean expressions like:

if (0 < x) { ... }

instead of

if (x > 0) { ... }

I have always used the second way, always putting the variable as the first operand and using whatever boolean operator makes sense, but lately I have read code that uses the first method, and after getting over the initial weirdness I am starting to like it a lot more.

Now I have started to write all my boolean expressions to only use < or <= even if this means the variable isn't the first operand, like the above example. To me it seems to increase readability, but that might just be me :)

What do other people think about this?

Upvotes: 1

Views: 233

Answers (5)

John Kugelman
John Kugelman

Reputation: 361615

To be honest it's unusual to write expressions with the variable on the right-side, and as a direct consequence of that unusualness readability suffers. Coding conventions have intrinsic value merely by virtue of being conventions; people are used to code being written in particular standard ways, x >= 0 being one example. Unnecessarily deviating from simple norms like these should be avoided without good cause.

The fact that you had to "get over the initial weirdness" should perhaps be a red flag.

I would not write 0 < x just as I would not use Hungarian notation in Java. When in Rome, do as the Romans do. The Romans write x >= 0. No, it's not a huge deal, it just seems like an unnecessary little quirk.

Upvotes: 0

Yada
Yada

Reputation: 31225

An advantage for putting the number first is that it can prevent bug of using = when == is wanted.

if ( 0 == x ) // ok
if ( 0 = x )  //is a compiler error

compare to the subtle bug:

if ( x = 0 )  // assignment and not comparison. most likely a typo

Upvotes: 0

Michael Burr
Michael Burr

Reputation: 340218

Use whatever 'reads' best. One thing I'd point out is that if I'm testing to see if a value is within bounds, I try to write it so the bounds are on the 'outside' just like they might be in a mathematical expression:

So, to test that (0 < x <= 10):

 if ((0 < x) && (x <= 10)) { ... }

instead of

 if ((0 < x) && (10 >= x)) { ... }

or

 if ((x > 0) && (10 >= x)) { ... }

I find this pattern make is somewhat easier to follow the logic.

Upvotes: 0

Naveen
Naveen

Reputation: 73443

It is mostly done to avoid the problem of using = instead of == in if conditions. To keep the consistency many people use the same for with other operators also. I do not see any problem in doing it.

Upvotes: 0

Noon Silk
Noon Silk

Reputation: 55082

Do whatever is most natural for whatever expression you are trying to compare.

If you're wondering about other operations (like ==) there are previous topics comparing the orderings of operands for those comparisons (and the reasons why).

Upvotes: 3

Related Questions