Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for null - what order? [duplicate]

Tags:

java

When checking for nulls I use this:

String str;

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

but I've seen this as well:

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

Is there any advantage of using one over the other? Or is it just to improve readability?

like image 830
blue-sky Avatar asked Jun 11 '12 15:06

blue-sky


People also ask

How do you do a null check?

Use "==" to check a variable's value. If you set a variable to null with "=" then checking that the variable is equal to null would return true. variableName == null; You can also use "!= " to check that a value is NOT equal.

Why is checking for null a good practice?

It is a good idea to check for null explicitly because: You can catch the error earlier. You can provide a more descriptive error message.

Can a double value be null?

Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.


4 Answers

The second version ( null == str ) is called a yoda condition.

They both result in the same behavior, but the second one has one advantage: It prevents you from accidentally changing a variable, when you forget one =. In that case the compiler returns an error at that row and you're not left with some weird behavior of your code and the resulting debugging.

like image 197
Sirko Avatar answered Oct 19 '22 10:10

Sirko


The null == x convention is usually found in code written by people familiar with C, where an assignment can also be an expression. Some C programmers write code like this so that if they miss an = in

if (NULL == ptr)...

the code will not compile, since NULL = ptr is not a valid assignment. This prevents a rather sneaky error from being introduced in the code-base, although modern C compilers make make such conventions obsolete, as long as one takes care to enable and read the generated warnings...

This coding style has never had any usefulness in Java, where reference assignments cannot be used as boolean expressions. It could even be considered counter-intuitive; in their natural language most people will say "if X is null...", or "if X is equal to 17...", rather than "if null is equal to X...".

like image 34
thkala Avatar answered Oct 19 '22 12:10

thkala


There's no difference between the two other than readability. Use whichever makes more sense to you.

like image 27
Jeffrey Avatar answered Oct 19 '22 11:10

Jeffrey


As you stated readability is the most important reason. Reading it out loud, the (null == str) does not read well. It's almost like reading right to left. (str == null) reads much better.

In addition, I think the following needs to be taken into consideration:

if (str != null)  
if (str == null)

vs.

if (null != str)
if (null == str)

I would expect the positive (str == null) and the negative to be written in the same manner, which is another reason I would favor the top set.

like image 27
tjg184 Avatar answered Oct 19 '22 12:10

tjg184