Alex Rivera | Logout

How to check for equals? (0 == i) or (i == 0)

Asked 2008-09-29T11:20:44.983
22

Okay, we know that the following two lines are equivalent -

  1. (0 == i)
  2. (i == 0)

Also, the first method was encouraged in the past because that would have allowed the compiler to give an error message if you accidentally used '=' instead of '=='.

My question is - in today's generation of pretty slick IDE's and intelligent compilers, do you still recommend the first method?

In particular, this question popped into my mind when I saw the following code -

if(DialogResult.OK == MessageBox.Show("Message")) ... 

In my opinion, I would never recommend the above. Any second opinions?

Edit
Report

3 Answers

2

Third option - disallow assignment inside conditionals entirely:

In high reliability situations, you are not allowed (without good explanation in the comments preceeding) to assign a variable in a conditional statement - it eliminates this question entirely because you either turn it off at the compiler or with LINT and only under very controlled situations are you allowed to use it.

Keep in mind that generally the same code is generated whether the assignment occurs inside the conditional or outside - it's simply a shortcut to reduce the number of lines of code. There are always exceptions to the rule, but it never has to be in the conditional - you can always write your way out of that if you need to.

So another option is merely to disallow such statements, and where needed use the comments to turn off the LINT checking for this common error.

-Adam

answered 2008-09-29T12:38:30.100
1

Rule 0 for all coding standards should be "write code that can be read easily by another human." For that reason I go with (most-rapidly-changing value) test-against (less-rapidly-changing-value, or constant), i.e "i == 0" in this case.

Even where this technique is useful, the rule should be "avoid putting an lvalue on the left of the comparison", rather than the "always put any constant on the left", which is how it's usually interpreted - for example, there is nothing to be gained from writing

if (DateClass.SATURDAY == dateObject.getDayOfWeek())

if getDayOfWeek() is returning a constant (and therefore not an lvalue) anyway!

I'm lucky (in this respect, at least) in that these days in that I'm mostly coding in Java and, as has been mentioned, if (someInt = 0) won't compile.

The caveat about comparing two booleans is a bit of a red-herring, as most of the time you're either comparing two boolean variables (in which case swapping them round doesn't help) or testing whether a flag is set, and woe-betide-you if I catch you comparing anything explicitly with true or false in your conditionals! Grrrr!

answered 2008-09-29T13:09:42.350
0

I believe the only factor to ever force one over the other is if the tool chain does not provide warnings to catch assignments in expressions. My preference as a developer is irrelevant. An expression is better served by presenting business logic clearly. If (0 == i) is more suitable than (i == 0) I will choose it. If not I will choose the other.

Many constants in expressions are represented by symbolic names. Some style guides also limit the parts of speech that can be used for identifiers. I use these as a guide to help shape how the expression reads. If the resulting expression reads loosely like pseudo code then I'm usually satisfied. I just let the expression express itself and If I'm wrong it'll usually get caught in a peer review.

answered 2012-06-26T21:40:52.910

Your Answer