views:

125

answers:

3

Possible Duplicates:
What's the comparison difference?
Null check in Java

Most of the developers have the habit of writing the null checking with null in the left hand side.like,

if(null == someVariable)

Does this help any way? According to me this is affecting the readability of the code.

+3  A: 

It used to help in 'the olden days' when C compilers would not complain about missing an =, when wanting ==:

// OOps forgot an equals, and got assignment
if (someVariable = null) 
{
}

Any modern C#/Java/C++/C compiler should raise a warning (and hopefully an error).

Personally, I find

if (someVariable == null) 
{
}

more readable than starting with the null.

Mitch Wheat
(Speaking of C and C++ compilers): an error, not a warning?
mlvljr
updated.........
Mitch Wheat
+11  A: 
T.J. Crowder
A: 

In your case, I don't see any merit in doing that way. But I prefer the following...

if("a string".equals(strVariable))
{
}

over this..

if(strVariable != null && strVariable.equals("a string"))
{
}
Rosdi
@T.J. Crowder - yes, it is defined in the contract. From the very link you provided: "For any non-null reference value x, x.equals(null) should return false."
Cowan
Rosdi
T.J. Crowder