aspose file tools*
The moose likes Beginning Java and the fly likes null test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null test" Watch "null test" New topic
Author

null test

ravi janap
Ranch Hand

Joined: Nov 04, 2000
Posts: 389
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


SCJP, SCJD, SCWCD, SCBCD, SCEA
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4997
    
    8

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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Nov 04, 2000
Posts: 389
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null test