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

obj == null OR null == obj, which is better?

 
Mohit Jain
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While comparing a reference for null value, out of following two ways which way is better??

obj == null OR null == obj

They say it doesnt make difference when this condition is used all alone, but its better to put null on left side when two conditions are concatenated using && or || like -

null == obj && <some condition>


Please suggest.
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be patient.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For C/C++, if you accidentally used "=" (assignment) instead of "==" (comparison), it will assign the value, and still have a valie that can be used in the condition. Thia made for some really obscure bugs. Reversing it fixed the problem, as you can assign a variable to a literal (null).

For Java, it doesn't matter as you can't use null as a boolean (implicitedly).

Henry
[ April 25, 2008: Message edited by: Henry Wong ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic