Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equality query!

 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While studying some codes(from many different projects)I have noticed that some programmers prefer to use
if(null != object )
instead of
if(object != null)
Is there is any advantage of using the first as compared to the second.
 
Jeremy Botha
Ranch Hand
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a habit you pick up from seeing too many NullPointerExceptions.

Consider the following snippet:



Now consider the following code:



Example one can generate a NullPointerException unless you explicitly check for a null parameter. When querying databases this can be time consuming.

Example 2 will never raise a NullPointerException, since "bar" is never null, and (any non-null string).equals(null) will always return false.

I always use method 2.

Jeremy
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have mentioned is absolutely true, but I have mentioned a different senario.
In my case even if object is null , it would not throw a NPE.
if(object != null)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it's because of symmetry to the well known C(++) idiom to use

if (null == object)

instead of

if (object == null)

The latter can lead to nasty bugs in some languages, because it's so easy to write

if (object = null)

instead.

Of course in Java that last line wouldn't compile at all, so it's an idiom that doesn't apply to Java.

With other words, while that idiom is good to use in some other languages, the only thing it does for Java code is making it harder to read...
 
Jeremy Botha
Ranch Hand
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rahul Bhattacharjee:
What you have mentioned is absolutely true, but I have mentioned a different senario.
In my case even if object is null , it would not throw a NPE.
if(object != null)


Yes, your situation is different, but the idiom is the same. Put an operand of the left hand side of the equality operator that cannot cause some form of RuntimeException or other illegal operation

whether it's "string".equals(arg) or null == arg or result == complicatedVoodooFunction(arg...), it restricts the possible 'error space' and eliminates several classes of simple yet exceedingly vexing bugs. I'd personally rather use the above idiom than spend even 5 minutes bughunting.

Jeremy
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeremy Botha:

whether it's "string".equals(arg) or null == arg or result == complicatedVoodooFunction(arg...), it restricts the possible 'error space' and eliminates several classes of simple yet exceedingly vexing bugs. I'd personally rather use the above idiom than spend even 5 minutes bughunting.


Only that the "null ==" idiom doesn't reduce the error space in Java at all (although it does in other languages).
 
Jeremy Botha
Ranch Hand
Posts: 125
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe as of the later compilers. I seem to recall 1.4 compilers happilly allowing you to go if(variable = null) { }

Jeremy
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you.
So I conclude that it doesnot add any value to java and its matches with what I thought initially about this.
 
Eitan Levi
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeremy Botha:
maybe as of the later compilers. I seem to recall 1.4 compilers happilly allowing you to go if(variable = null) { }

Jeremy


That doesn't sound right ... the value of an assignment expression is of the type you're assigning to, and if statements expect booleans - so you'd probably get a "Type mismatch: cannot convert from <whatever type variable is> to boolean".

Personally though, it freaks me out when I see



I always have to do a double-take and re-read the expression.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic