Home » Java » Is it appropiate the statement. if (0 != expression or variable) {} in java? [closed]

Is it appropiate the statement. if (0 != expression or variable) {} in java? [closed]

Posted by: admin December 28, 2021 Leave a comment

Questions:

I’m working on a java source code that has the style:

if (0 == var) {}
and 
if (null == someObj) {}

or

if (0 != var) {}
and 
if (null != someObj) {}

Should I rewrite it to:

if (var == 0) {}
and 
if (someObj == null) {}

?

Thanks in advance!.

Answers:

It’s perfectly valid to have:

if (0 == var) {}
and 
if (null == someObj) {}

It’s famously called yoda condition to prevent accidental use single = in place of ==.

I personally never used it. But some people like it. It’s really a matter of taste.


However, it’s of no use in Java. Because, such an accidental typo is caught at compile time.

The following would give a compile time error in Java.

if( var = 0 ) 
{
//Some code
}

Because assignment operation doesn’t yield Boolean value in Java.
However, in languages like C/C++, the above is valid and compiler would give no errors if warnings are not enabled. The above if condition will always evaluate to false (0) in C/C++. So it may go unnoticed and give unexpected results at run time.

In GCC, with all warnings enabled, it would give the warning for the above in C or C++:

warning: suggest parentheses around assignment used as truth value

So this may not of much use in C/C++ too as one is expected to compile with all warnings and fix all warnings. As I said before it’s a personal choice and makes no difference.

So if ( 0 == var){} is a valid syntax and is same as if (var == 0) {} and if you prefer Yoda conditions it, go for it!

###

In general to write 0 == n is called a yoda condition. Because if you say it loud you say if zero equals my var.

It is better to write n == 0. But it is exactly the same. But it is read better.

Some of the pros of the condition does not apply to java.

if (value = 42)

yields a compile error. value = 42 yields an int which is not a valid input in an if in java. But perfectly legal in C.

To compare using the constant on the left side is a common best practice. But only if using Object equals method.

if ("CONSTANT".equals(myString))

if myString is null or you are comparing primitives since there is no .equals() access to the variable theres no point in checking null.

if ( "CONSTANT" == null )

Although is only my opinion, the benefit is legibility only.

###

This type of terms is known as “Yoda expressions“, because they are “backwards” compared to English natural language.

I don’t think it pays of with assignments – good code audit tools catch these types of errors, and most should already be caught by the more strict Java compiler (using = instead of == was a common typo in C!)

However, when you are using compareTo methods, you need to consider null values.

 if ("example".equals(input))

cannot run into a NullPointerException, while the more “natural”

 if (input.equals("example"))

can, and needs extra checking for null values.

###

The first style is preferred by some because saying var = null instead of var == null by accident would be a bug that would be hard to catch in languages like C and C++, but null = var would be easy to catch, as it wouldn’t compile. As noted in the comments, this doesn’t apply for Java because null = var isn’t a boolean expression.