0

What is the difference between the two? Which one is better? I was asked this in an Interview.

The only difference I can think of is that if you wanted to do if(null == object) and forgot to add == and added =, it will not have any side-effect to your program. But if you do this mistake while doing if(object = null) then your program is toast.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
v0ld3m0rt
  • 866
  • 3
  • 12
  • 47
  • 2
    Some coders prefer "yoda" checks `null == value` where others prefer "natural language" form `value == null` - it's personal preference, in Java at least. I'd say your conclusion is also not bad – MadProgrammer May 08 '18 at 05:27
  • Oh, they really find one of the two better. You should ask them, which is better: `if(a==b)` or `if(b==a)`. – bipll May 08 '18 at 05:28
  • 5
    Unless you are assigning to a Boolean `if (o = null)` is a compile time error, and if it is a Boolean it is an NPE, so I think the correct answer is that `null = o` is an outdated Cism. – tgdavies May 08 '18 at 05:28

3 Answers3

1

Adding to Nayuki's answer, if the programmer mistypes = for ==, like if (object = null), there are still few cases where compilation error won't come and bugs would creep in the code.

For example: if (boolean1 = boolean2), is an assignment expression first and then is evaluated for the if condition. Another problematic code is if (wrapperBool = null), where wrapperBool is a Boolean wrapper class variable. It will throw a NullPointerException (NPE) at runtime.

But as Benny mentioned, mistyping should be caught upfront with unit test case(s).

gargkshitiz
  • 2,130
  • 17
  • 19
0

In the Java programming language, the two expressions if (object == null) versus if (null == object) do exactly the same thing, because the == operator is commutative. The former notation is considered more natural when spoken in English, whereas the latter notation has some proponents.

If the programmer mistypes = for ==, like if (object = null), this is a compile-time error in Java because the condition expression must be a boolean value, not Object. Hence, if (object == null) and if (null == object) are equally secure against typos in Java.

But in C and C++, integer, floating-point, and pointer types are implicitly coerced to bool in a condition expression. Writing if (object = null) will compile but do the wrong thing, whereas writing if (null = object) should trigger a compile-time error.

See also: https://coderanch.com/t/489740/java/difference-null-object-object-null

Nayuki
  • 17,911
  • 6
  • 53
  • 80
0

Because of its commutative property, the only difference between object == null and null == object (the Yoda version) is of cognitive nature: how the code is read and digested by the reader. I don't know the definitive answer though, but I do know I personally prefer comparing the object I'm inspecting to something else, rather than comparing something else to the object I'm inspecting, if that makes any sense. Start with the subject, then the value to compare it to.

In some other languages this comparison style is more useful.

To safe guard against a missing "=" sign in general though, I think writing null == object is a misguided act of defensive programming. The better way around this particular code is by guaranteeing the behavior with a junit test. Remember, the possible mistake of missing an "=" is not dependant on the method's input arguments - you are not dependent on the right use of this API by other people - so a junit test is perfect to safe guard against that instead. Anyway you will want to write junit tests to verify the behavior; a missing "=" naturally falls within scope.

Or maybe, could it have been a trick question to get you on the topic of discussing the uses of Optional to avoid raw null-checks altogether?

Benny Bottema
  • 11,111
  • 10
  • 71
  • 96