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.
Yoda conditions are part of the coding standards for Symfony and WordPress.

Origin

This programming style is named after the Star Wars character Yoda, who speaks English with a non-standard syntax. Thomas M. Tuerke claims to have coined the term Yoda notation and first published it online in 2006. According to him, the term Yoda condition was later popularized by Félix Cloutier in 2010.

Example

Usually a conditional statement would be written as:

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

Yoda conditions express identical logic, but are visually reversed:

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

Advantage

Readability of logically-chained comparisons

Some languages, such as Python, support "chained" comparison operators in their syntax. Thus, the following lines are logically equivalent:

  1. Using chained comparators:
if 3.14 < y <= 42:...
  1. Logically equivalent to:
if and :...

Notice that the second form naturally uses Yoda syntax in the left-hand comparison. Consider the same line without Yoda syntax:

if and :...

When handwriting math, many authors prefer the "chained" notation . When programming in a language that does not literally support the chained notation, the author may prefer the Yoda syntax, as it at least visually evokes the familiar chained notation.

Detecting programmer mistakes

For symmetric comparisons, such as equality, swapping the left and right operands does not change the behavior of the program. In programming languages that use a single equals sign for assignment expressions, one might mistakenly write an assignment expression where an equality comparison was intended.

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

Using Yoda conditions:

if
// An error this is, and compile it will not

Since literal expressions such as 42 are not assignable, assignment-equality confusion in Yoda conditions often manifests as a compile-time semantic error.

Changing the target of dynamic dispatch

In most object-oriented programming languages, the receiver of a method call is written to the left of the call's other arguments. At the same time, in non-Yoda comparisons, the variable that is the subject of comparison is written on the left-hand side. Comparison method calls are thus ordinarily dynamically dispatched on the object being compared, which is not always desirable.

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

With Yoda conditions, the call can be dispatched on a constant object instead.

String myString = null;
if
// This resolves to false without throwing a NullPointerException

Criticism

Yoda conditions are 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. Python 3.8 introduced assignment expressions, but uses the walrus operator := instead of a regular equal sign to avoid bugs which simply confuse with =.
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.