简体   繁体   中英

Which is better way of having a null check?

I came across these two ways of having a null check for a string object.

Given a string object String str = "example";

  1. If(str.someMethod() != null ) or
  2. If (null != str.someMethod())

Why do we prefer the 2nd one ? What is the exact reason behind this, is it related to performance ?

In your example, it makes absolutely no difference which you do (other than style), because the reason for Yoda checks is to avoid accidentally doing an assignment (but keep reading for why this doesn't matter in Java), and you can't assign to the result of calling a method.

One of the nice things about Java is that even if you were testing str , eg:

if (str == null)

vs.

if (null == str)

there would still be no difference, whereas in some of the languages with syntax derived from B (such as C, C++, D, JavaScript, etc.), people do the second (a "Yoda test") to minimize the odds of this bug:

if (str = null)          // Not an issue in Java

In C or JavaScript, for instance, that would assign null to str , then evaluate the result, coerce it to boolean, and not branch. But in Java, that's a syntax error the compiler tells you about.

Java doesn't do that kind of boolean conversion, so the only reason for using Yoda checks in Java is if you're testing booleans, ec

boolean flag;
// ...
if (flag == false)

There , you might conceivably do this by accident:

if (flag = false)

But since using == and != with booleans is completely unnecessary (you'd just use if (flag) or if (!flag) ), in the real world you don't need Yoda checks with Java at all.

That doesn't mean people don't still use them, as a matter of their own personal style. There's just no objective reason to, in Java.

It makes no difference performance-wise, however the Yoda programming pattern have some advantages when it comes to the world of programming skills.

In your example it would not matter as both cases would throw a NullPointerException (since you're invoking someMethod` of a null instance reference).

However, say that you wanted to check if str is null. In the first case, you'd write if (str == null) and in the second if (null == str) . Both are the same. Now say that you have accidently used = instead of == . In Java, it would not matter as the compiler wouldn't let you as the expression doesn't evalute to a boolean value. But other languages let you do that, more specifically languages that are compiler-free and only use an interperter. In that case, if you write if (str = null) you'll be assigning null to string and overriding its' current value, which would result in buggy behavior and you chasing after your tail for quite some time. However, if you'd write if (null = str) you'll get an error saying you cannot assign a value to null and thus save yourself a lot of time and effort. Again, this is not relevant to JAVA.

An example which might be relevant for Java, is the use of method invocation on constant values. For example, if (str.equals("constantString") . If str is null you'll get a NullPointerException. However, if you use a Yoad pattern and write if ("constantString".equals(str)) you'll get false as ConstantString does not equal null . This of course is only relevant for comparison, and not say contains etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM