2

I am seeing this a lot in someone's code: null-check conditions are written like this:

if (null == value)

rather than

if (value == null)

I don't think there is any reason to have the null precede the operator in Java. Is there any benefit to doing it this way? Is this simply a case of a C++ programmer applying his skill to Java incorrectly, or am I missing something important?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
bharal
  • 15,461
  • 36
  • 117
  • 195
  • 2
    do you have example code? i dont really understand the question... sorry – melli-182 Dec 05 '12 at 17:17
  • 1
    @MatiasCaamaño `if (null == value)` rather than `if (value == null)` – FThompson Dec 05 '12 at 17:19
  • This sounds like a duplicate of http://stackoverflow.com/questions/3021195/which-is-more-effective-if-null-variable-or-if-variable-null – NPE Dec 05 '12 at 17:19
  • Just a note for SO, please re-ask the question in the body of the post, and not just as the title. – Matt Clark Dec 05 '12 at 17:19
  • @Vulcan Thanks for the aclaration! – melli-182 Dec 05 '12 at 17:20
  • People we do need 8 answers answering the wrong thing. The reason is to make sure we we do not write `value = null` instead of `value == null` since `null = value` would catch the error. – jn1kk Dec 05 '12 at 17:24
  • @skynorth - in fact, it isn't the case at all. I'm aware of the difference (hence the mention of c++ programming in the original question) – bharal Dec 06 '12 at 13:24

7 Answers7

11

This is a case of so-called "Yoda Conditions" (item #1). Although it is possible to rationalize them in C/C++, there is no reason to use them in Java.

In C/C++, expressions of any type can go into ifs and whiles. Writing

if (var = NULL) // No compile-time error in C/C++

instead of

if (var == NULL)

is a common error among novices. Yoda conditions were supposedly a remedy to address this problem, at the cost of "Yodifying" your code: C/C++ would trap assignments to NULL

if (NULL = var) // Compile-time error

but "reversed" NULL checks are OK:

if (NULL == var)

Since Java considers non-boolean expressions inside control blocks of if, while, and for to be errors, it would trap var = null in place of var == null, triggering an error. Therefore, there is no reason to give up readability by "Yodifying" your expressions.

thegrinner
  • 11,546
  • 5
  • 41
  • 64
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Lets say you only want to print the contents of a String if it's longer than 20. You'll do this:

if (str != null && str.length() > 20) {
    System.out.println(str);
}

leaving out this str != null && would throw a NPE if str actually were null.

If you are referring to the difference of

if (str == null) { ... }

and

if (null == str) { ... }

there is none.

jlordo
  • 37,490
  • 6
  • 58
  • 83
1

The answer is NO!
There is absolutley not any need to prefer if (null == value) instead if (value == null) in java!

The check of if (null == value) , is a bit strange, at least in java, coming from code guidelines in C and C++, where that make sense.

I like more if (value == null) which is better readable.

The reason is to avoid the C/C++ error if (value = null) instead of correct if (value == null)

If you use if (null = value) the C/C++ compiler will throw an error.

However in java, even if (value = null) would be a syntax error.
So dont use that (null == value) in java

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • i don't think this is true... in an `if` block you'd have a compiler error for value = null -- that is, if (value = null) won't compile – bharal Dec 05 '12 at 17:24
1

It may read better some times

if( null == someFunction(someParameter, anotherParameter) )

vs

if( someFunction(someParameter, anotherParameter) == null )
irreputable
  • 44,725
  • 9
  • 65
  • 93
0

It's typically an artifact of the developer's native language. Some languages have rules make null == value more natural than value == null. Syntactically, they are the same to Java.

Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • No, it is not the reason that there exitsts languages where "null == value" is more natural. It is simply a coding gudeline, to avoid accidently omitting one "=" instead of correct the "==" – AlexWien Dec 05 '12 at 17:32
0

Not checking could mean a null pointer exception.

Placing it in this order helps you not confuse = (assignment) with == (equality).

both

if (a = null)

and

if (a == null)

will compile; however, you probably want the second and not the first.

If you flip them,

if (null = a)

will not compile, while

if (null == a) 

will compile.

Thus you can catch a small, hard to find error at compiler time.

rajah9
  • 11,645
  • 5
  • 44
  • 57
0

Just to make sure that you are not assigning a value for eg like value = null if you ommit == so this code will compile fine and it can be identified only during runtime, but if you try like this null = value this even won't compile.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107