Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Which is the more correct way?

 
Sivaraman Lakshmanan
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I could see many programmers code in different way they check for null conditions. The 2 common ways of checking are

and other way is


Which is the correct ways of checking. Can some please clarify this.

Regards,
Sivaraman.L
 
Steve McCann
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's down to personal preference - they will both work equally well.

Steve
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is in whether you want to catch that you have mistakenly typed "=" instead of "==".

if (null = varName)

doesn't compile, while

if (varName = null)

does.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
The difference is in whether you want to catch that you have mistakenly typed "=" instead of "==".

if (null = varName)

doesn't compile, while

if (varName = null)

does.


That's true for C++, but not for Java. "null" is not a boolean expression in Java. Try it!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, then, it's a habit consistent with coding

1 == var

instead of

var == 1

It's also consistent with

"literal".equals(var)

which avoids a test for (var != null) first.

I put literals and constants on the left almost all the time. Looks a little odd at first, but keeps me out of trouble now & then.
[ August 04, 2005: Message edited by: Stan James ]
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:

I put literals and constants on the left almost all the time. Looks a little odd at first, but keeps me out of trouble now & then.

[ August 04, 2005: Message edited by: Stan James ]


and can be avoided, if your programming environment is able to warn on probably unmeant assignments (like eclipse).
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The correct way is not to use Java. The very existence of null itself is a language flaw. You can workaround its existence optimally by failing as early as possible upon its detection i.e. entry into contract implementations (concrete methods). This optimal approach works surprisingly well, but is only optimal within the context of Java. The correct approach, and moreso for a statically typed language, would have failure at compile-time (no such thing as null).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic