my dog learned polymorphism
The moose likes Java in General and the fly likes Question about coding standards Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Question about coding standards" Watch "Question about coding standards" New topic

Question about coding standards

Francisco Moroyoqui

Joined: Nov 24, 2004
Posts: 6

I have a question about coding standards. Is it defined somewhere (i.e by Sun) the order in which variables must come in a condition when comparing them to null?

For example, which option is better?
a) if (myObj != null)
b) if (null != myObj)
c) really doesn't matter

I couldn't find anything about this, the closest thing I found were the code conventions at

Sorry if this is the wrong forum for this question, couldn't find any other more suitable.

Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
I think this may be more a coding "style" than a coding "standard".

Using "if (null != obj)" is just akward in my opinion, but I suppose it doesn't matter much as long as your style is used consistently.

Jessica Sant

Joined: Oct 17, 2001
Posts: 4313

I'm used to seeing (object != null) rather than the other way around... But I don't think I've ever seen this in a coding standard before.
Francisco Moroyoqui

Joined: Nov 24, 2004
Posts: 6
OK, I see what you mean.

Thanks a lot for your help.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Realize that this style came from C and C++ where if you made a typothe compiler would merrily turn that into, effectivelyIn words, c would get assigned the value 'A' (instead of compared to it) and the if-test would pass since the value of an assignment is the assigned value itself.

In Java, that would be illegal, so that typo is caught by the compiler.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Same thing with if (0==arg). I still like this because most of my career was with langauges that used = for assign and compare and I can forget the == now & then.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Question about coding standards
It's not a secret anymore!