6

Possible Duplicates:
What's the comparison difference?
Null check in Java

Most of the developers have the habit of writing the null checking with null in the left hand side.like,

if(null == someVariable)

Does this help any way? According to me this is affecting the readability of the code.

asked Oct 29, 2010 at 7:13
5
  • Possible duplicate: stackoverflow.com/questions/3021195/… Commented Oct 29, 2010 at 7:27
  • It's called "Yoda conditions" ! :D Commented Oct 29, 2010 at 7:28
  • 3
    You mean, "Yoda Conditions, its called." Commented Oct 29, 2010 at 7:29
  • 4
    'Most of the developers'. Really? Evidence? Commented Oct 29, 2010 at 7:47
  • @EJP: Yeah, I don't see it very often at all. Perhaps it's most of the developers in his organization. Commented Oct 29, 2010 at 7:48

3 Answers 3

15

No, it has no purpose whatsoever in Java.

In C and some of its related languages, it was sometimes used to avoid making this mistake:

if (someVariable = null)

Note the = rather than ==, the author has inadvertently assigned null to someVariable rather than checking for null. But that will result in a compiler error in Java.

Even in C, any modern compiler will have an option to treat the if (someVariable = null) as a warning (or even an error).

Stylistically, I agree with you — I wouldn't say "if 21 you are, I will serve you a drink" (unless I'd already had (削除) a couple (削除ここまで) several and was doing my Yoda impersonation). Mind you, that's English; for all I know it would make perfect sense in other languages, in which case it would be perfectly reasonable style for speakers of those languages.

answered Oct 29, 2010 at 7:17

4 Comments

@TJCrowder, to say it has no purpose whatsoever in java would be slightly wrong....If the someVariable is a Boolean, it doesn't throw a compile error...:) You'll need to Yoda that out...but otherwise java cleans up for you. :)
@st0le: Wow, I'm astonished to find that you're right about that. I did not know that (and have checked it). Thank you! But I'd counter with: Don't test booleans that way, there's no purpose whatsoever to if (someVariable == true), simply use if (someVariable) (and of course the !someVariable instead of comparing to false). (And the fact you can have an inadvertent assignment supports using that style.) The only time I'd use = or != with booleans would be if I were comparing two variables (not comparing to a constant), and the construct wouldn't help me there. :-)
@TJCrowder, well ofcourse, no decent java programmer will actually use that...I was merely throwing an exception, pun intended ;)
@st0le: Yeah, thanks again. I was so surprised when I wrote a test case for it and it passed. :-)
5

It used to help in 'the olden days' when C compilers would not complain about missing an =, when wanting ==:

// OOps forgot an equals, and got assignment
if (someVariable = null) 
{
}

Any modern C#/Java/C++/C compiler should raise a warning (and hopefully an error).

Personally, I find

if (someVariable == null) 
{
}

more readable than starting with the null.

answered Oct 29, 2010 at 7:16

1 Comment

(Speaking of C and C++ compilers): an error, not a warning?
1

In your case, I don't see any merit in doing that way. But I prefer the following...

if("a string".equals(strVariable))
{
}

over this..

if(strVariable != null && strVariable.equals("a string"))
{
}
answered Oct 29, 2010 at 7:35

3 Comments

@T.J. Crowder - yes, it is defined in the contract. From the very link you provided: "For any non-null reference value x, x.equals(null) should return false."
@T.J. Crowder & @Cowan, yes I went back as far as JDK 1.1.1 and it is defined in the contract. tns.lcs.mit.edu/manuals/java-1.1.1/api/…
@Cowan & @Rosdi: I can't believe I've missed that all these years. Thanks. I've removed the earlier comment. Proof that you never stop learning. Man do those docs need rewording, all of the unnecessary reiteration of "for any non-null x" totally obscures the remaining points. And sadly, I've met quite a number of contract-breaking implementations of equals over the years. :-(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.