Yoda conditions


In programming jargon, Yoda conditions is a programming style where the two parts of an expression are reversed from the typical order in a conditional statement. A Yoda condition places the constant portion of the expression on the left side of the conditional statement. The name for this programming style is derived from the Star Wars character named Yoda, who speaks English with a non-standard syntax.
Yoda conditions are part of the Symfony, and the WordPress coding standards.

Example

Usually a conditional statement would be written as:

if
// Reads like: "If the value equals 42..."

Yoda conditions describe the same expression, but reversed:

if
// Reads like: "If 42 equals the value..."

The constant is written to the left of the comparison operator, and the variable whose value is being checked against the constant is written to the right. This order is comparable to the non-standard English speaking style of Yoda, which is roughly object–subject–verb.

Advantage

Error detections

Placing the constant value in the expression does not change the behavior of the program. In programming languages that use a single equals sign for assignment and not for comparison, a possible mistake is to assign a value unintentionally instead of writing a conditional statement.

if
// This assigns 42 to myNumber instead of evaluating the desired condition

Using Yoda conditions:

if
// This is a syntax error and will not compile

Since 42 is a constant and can not be changed, this error will be caught by the compiler.

Boolean myBoolean = true;
if
// This causes a NullPointerException in Java Runtime, but legal in compilation.

Avoiding some types of unsafe null behavior

Yoda conditions help with unsafe behavior in some situations.

String myString = null;
if
// This causes a NullPointerException in Java

With Yoda conditions:

String myString = null;
if
// This is false, as expected

Expressing mathematics

Yoda conditions can be easier to understand when the expression represents the real number line.

if -1 <= x <= 1:

Without yoda conditions:

if && )

With yoda conditions:

if && )

Criticism

Yoda conditions are widely criticized for compromising readability by increasing the cognitive load of reading the code.
Some programming languages do not allow variable assignments within conditionalsfor example by requiring that assignments do not return a value, or by defining as part of their grammar the invariant that conditions cannot contain assignment statementsin which case this error is impossible to encounter. Many compilers produce a warning for code such as if , which alerts the programmer to the likely mistake. In dynamic languages like JavaScript, linters such as ESLint can warn on assignment inside a conditional.
The advantage of avoiding null behavior can also be considered a disadvantage, as null pointer errors can be hidden and only appear much later in the program.
Another disadvantage appears in C++ when comparing non-basic types as the is an operator and there may not be a suitable overloaded operator function defined. Example: a Microsoft's CComBSTR compare against a string literal, written as if , does not map to an overload function.