• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about coding standards

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

Thanks a lot for your help.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic