views:

136

answers:

2

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

In C/C++ one might prefer the former because of hard to find bugs when misspelling the equality operator as assignment. But in other languages, what are the pros and cons of one vs the other?

A: 

in java for example if ("expected".equals( var )) doesn't throw a nullPointerException, but if (var.equals("expected")) does if var is null

Nikolaus Gradwohl
A: 

I think that the latter is easier to read for a human, because you're saying:

Is the result of doing Foo what I wanted?

Whereas the former is:

Is what I wanted the same as the result of doing Foo?

Which sounds more like the expected result is liable to change.
When you're using literals, it seems stupid, and probably gives strange results in some languages, to test:
if(6 == resultOfSomething())
I've never had the issue of mistyping == as =, also [running through ActionScript, C#, and C++].

The Communist Duck