I may be an old greenscreener, but...

Feb 17, 2011 12:57

Could somebody please tell me why

if 2 == x
    something

is somehow preferable or more readable than the usual

if x == 2
    something

To me the former is asking "Has the value of 2 been set to match x?"

Reminds me of the old saying, "2 can never equal 3, even for very large values of 2."

geek

Leave a comment

Comments 5

crosstables February 17 2011, 18:07:55 UTC
I still always write "if x == 2", but if you make a typo, then:

if (2 = x) won't compile

and

if (x = 2) will probably cause your program to die in horrible ways

That's the only non-stylistic reason I've seen.

Reply

jigsawn February 18 2011, 04:00:09 UTC
Exactly. I don't make a habit of following this convention, but I think I ought to. The single equal sign is an easy error to make, and giving yourself an extra chance to catch that error is a Very Good Thing.

Reply


Also important with object-oriented String comparisons jlmoran February 28 2011, 12:59:59 UTC
There's another reason to put the constant value as the first part of the comparison. In some object-oriented languages, such as Java, Strings are objects and use an equals() method to compare the actual value of the Strings, as using == just compares the memory addresses of the two String objects. In this case, if you were to use if foo.equals("2"), you run the risk of a null pointer exception in your code, which means you now have to check for null before you call the equals() method.

On the other hand, if you use if "2".equals(foo), there's no risk of a null pointer exception (all OO languages safely handle null values passed as the argument to equals()), so your code is cleaner and automatically eliminates the exception risk.

Because of that, it's just considered good practice to be consistent and make all boolean comparisons involving a constant with the constant on the left side of the equation.

Reply

Re: Also important with object-oriented String comparisons edhorch February 28 2011, 14:09:31 UTC
Interesting. I guess I'll just have to get used to it like I had to get used to the control key being replaced by the caps lock key. :)

Hope you're feeling OK. I heard about the fall, and cringed in sympathy.

Reply

Re: Also important with object-oriented String comparisons jlmoran February 28 2011, 16:27:33 UTC
The tale of the fall sounds worse than it really was. I was standing up from the couch and lost my balance before I could get the crutches under me. I knew I just had to fall back onto the couch, but somehow I didn't get my foot elevated in time and partially planted it. But my arm took the brunt of the fall (on purpose) from hitting the couch's arm rest. The initial flare of pain in the foot died down pretty quickly, and apart from the initial loud "OWWWWWW!" I was surprised how "easy" I got off. But I got nervous when it started flaring and ebbing regularly, so I decided to play it safe and call the doc.

I probably could have just taken an extra painkiller and been good, given that I'd done similar things years ago with a prior foot surgery (and that was a FULL plant on the operated foot, without any damage). But given the effort the surgeon had to put into getting the foot properly aligned, I didn't want to find out a few days later that I'd hosed all his work. ;-)

Reply


Leave a comment

Up