1

I have a doubt regarding coding standard of a null check. I want to know the difference between

if(a!=null)

and

if(null!=a)

which one is better,which one to use and why?

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
Abx
  • 2,852
  • 4
  • 30
  • 50

6 Answers6

8

There is no difference. But the first is more common. The second is also called "Yoda Conditions" because of its unnatural "grammar".

Once I was working in a project where the coding guideline was to use if (null != a) because they thought it is easier for the developer to understand that the constant value has to come first always (as in CONSTANT_VALUE.equals(variable). That was pretty annoying to me.

Kai
  • 38,985
  • 14
  • 88
  • 103
  • 3
    Though Yoda usually states the constant known not to be `null` first. E.g. `"bar".equals(foo)` – Tim Bender Mar 20 '13 at 08:56
  • Yepp, for reference: [Coding Horror: New Programming Jargon](http://www.codinghorror.com/blog/2012/07/new-programming-jargon.html) – Kai Mar 20 '13 at 09:02
8

Both are same in Java, as only boolean expressions can be inside an if. This is just a coding style preference by programmer and most of them use null != a.

The null != a is an old practice in programming languages like Java,C++ (called as Yoda Conditions).
As it is valid to write if (a = null) and accidentally assign null to the a so writing null first is a guard to stop this accident from happening.

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82
  • 2
    Thanks.I got it now,the last part really helped – Abx Mar 20 '13 at 09:51
  • 4
    Consider dropping the "most of them use null != a". Up to date developers who stay educated on how their programming language works instead of blindly applying previous language paradigms will instead favor (object != null) format in Java. This is because it reads more logically and the opposite pattern has absolutely no benefit in Java. Thus, the "null first" pattern that should be discouraged in Java because there is absolutely no benefit and sacrifices readability. – Russ Dec 14 '15 at 13:42
  • Down voter. Can you please put a comment. – Shreyos Adikari Mar 07 '16 at 18:17
2

They're both the same. It depends on your coding style.

xlecoustillier
  • 16,183
  • 14
  • 60
  • 85
1

From the compiler's point of view, they're exactly the same. But the first form is more readable, so I'd advise you to use that one.

Ivan Vrtarić
  • 385
  • 3
  • 11
1

No difference betwwen them if statement works based on result of expression so u write either if(a!=null) or if(null!=a) will produce true or false then result is evaluated.

So it doesnt matter you write which you like

Manish Sahu
  • 315
  • 1
  • 3
  • 12
1

They both are same. Although the first variant is common the second variant is useful if you know the first variable is not null

Example "some value".equals(your_variable) , some value can be any value you know is not null. This will avoid NPE when your_variable is null.

String str = "somevalue"; 

if(str != null && str.equals("somevalue")) { }

if("somevalue".equals(str)) { }

Both the conditions will be same if str is null or not.

Dhanuka
  • 2,826
  • 5
  • 27
  • 38
Abdullah Shaikh
  • 2,567
  • 6
  • 30
  • 43