Is there any reason to use null == value condition in java?
java
Solution
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 `if`s and `while`s. Writing
if (var = NULL) // No compile-time error in C/C++
instead of
if (var == NULL)
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 `NULL`
if (NULL = var) // Compile-time error
but "reversed" `NULL` checks are OK:
if (NULL == var)
Since Java considers non-boolean expressions inside control blocks of `if`, `while`, and `for` to be errors, it would trap `var = null` in place of `var == null`, triggering an error. Therefore, there is no reason to give up readability by "Yodifying" your expressions.
Problem
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?