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