I am seeing this a lot in someone’s code: null-check conditions are written like this:
if (null == value)
rather than
if (value == null)
I don’t think there is any reason to have the null precede the operator in Java. Is there any benefit to doing it this way? Is this simply a case of a C++ programmer applying his skill to Java incorrectly, or am I missing something important?
This is a case of so-called “Yoda Conditions” (item #1). Although it is possible to rationalize them in C/C++, there is no reason to use them in Java.
In C/C++, expressions of any type can go into
ifs andwhiles. Writinginstead of
is a common error among novices. Yoda conditions were supposedly a remedy to address this problem, at the cost of “Yodifying” your code: C/C++ would trap assignments to
NULLbut “reversed”
NULLchecks are OK:Since Java considers non-boolean expressions inside control blocks of
if,while, andforto be errors, it would trapvar = nullin place ofvar == null, triggering an error. Therefore, there is no reason to give up readability by “Yodifying” your expressions.