File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null test

 
ravi janap
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been programming in Java for a while.
I have seen the practise of ( null != someVariable ) being used in my previous project.
Recently, There was a debate when I tried to do the same in my current project and it was called as bad programming practise.
I know that ( someVariable != null ) is correct.
My only argument is that it is a boolean test and it would not make any difference whether I do it either way.
Any comments ?
-- Ravindra
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
A quick search on JavaRanch's beginner's section for "null" in the topic heading results in a link to this conversation from last month.
Good Luck,
-Dirk Schreckmann
 
Junilu Lacar
Bartender
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct to argue that it doesn't matter either way and I would even question the statement that it is a bad programming practice (how so?). That said though, your form does seem a little awkward to me. Personally, I prefer to have null on the RHS. Maybe your new colleagues just want you to be consistent with their style.
Junilu
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the 'bad programming practice' comes from the days of C/C++ where it's completely possible for a condition to always evaluate true, because whoops, assignment always equals true. I seem to remember an instructor giving this to us as a 'tip' to avoid the very common newbie mistake of coding:
Which is completely unexpected, because doesn't i equal 5?

Whereas.. if I had coded the constant on the LHS, the compiler would catch it. So for C/C++ programmers, it's best to keep in this habit, of putting constants on the LHS and letting compilers catch this bug.

Having said that... I rarely remembered that particular 'trick'... and I think it's just easier to remember to use == for comparisons and = for assignment.
 
ravi janap
Ranch Hand
Posts: 389
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you guys.
Anyway , I am changing the experession to
( someVariable != null ) inorder to be consistent with my other team members.
Thanks
-- Ravindra
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic