• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about coding standards

 
Francisco Moroyoqui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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 http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

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

Thanks.
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I see what you mean.

Thanks a lot for your help.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic