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.