Inprogrammingjargon,Yoda conditions (also calledYoda notation) is aprogramming style where the two parts of an expression are reversed from the typical order in aconditional 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 forSymfony[1] andWordPress.[2]
This programming style is named after theStar Wars characterYoda, whospeaks English with a non-standard syntax[3] (e.g., "When 900 years old you reach, look as good you will not."[4][5]). Thomas M. Tuerke claims to have coined the termYoda notation and first published it online in 2006.[6] According to him, the termYoda condition was later popularized by Félix Cloutier in 2010.
Usually a conditional statement would be written as:
if(value==42){/* ... */}// Reads like: "If the value equals 42..."
Yoda conditions express identical logic, but are visually reversed:
if(42==value){/* ... */}// Reads like: "If 42 equals the value..."
Some languages, such as Python, support "chained" comparison operators ("comparators") in their syntax.[7] Thus, the following lines are logically equivalent:
# Using chained comparators:if3.14<y<=42:...# Logically equivalent to:if(3.14<y)and(y<=42):...
Notice that the second form naturally uses Yoda syntax in the left-hand comparison (3.14 < y). Consider the same linewithout Yoda syntax:
if(y>3.14)and(y<=42):...
When handwriting math, many authors prefer the "chained" notation (example,example). 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.
Forsymmetric comparisons, such as equality, swapping the left and right operands does not change the behavior of theprogram. In programming languages that use a singleequals sign (=) forassignment expressions, one mightmistakenly write an assignment expression where an equality comparison was intended.
if(myNumber=42){/* ... */}// This assigns 42 to myNumber instead of evaluating the desired condition
Using Yoda conditions:
if(42=myNumber){/* ... */}// An error this is, and compile it will not
Since literal expressions such as42 are not assignable (they are not "lvalues"), assignment-equality confusion in Yoda conditions often manifests as acompile-time semantic error.
In mostobject-oriented programming languages, the receiver of amethod 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 ordinarilydynamically dispatched on the object being compared, which is not always desirable.
StringmyString=null;if(myString.equals("foobar")){/* ... */}// This causes a NullPointerException in Java
With Yoda conditions, the call can be dispatched on a constant object instead.
StringmyString=null;if("foobar".equals(myString)){/* ... */}// This resolves to false without throwing a NullPointerException
Yoda conditions are criticized for compromising readability by increasing thecognitive load of reading the code.[8][9][10]
Some programming languages (such asSwift,Kotlin and versions ofPython below 3.8) do not allow variable assignments within conditionals—for example by requiring that assignments do not return a value, or by defining as part of theirgrammar the invariant that conditions cannot contain assignment statements—in which case this error is impossible to encounter (that is, it would be detected as asyntax error by the parser prior to a program ever being allowed to enter intoruntime).[11] Many compilers produce a warning for code such asif (myNumber = 42) (e.g., theGCC-Wall option warnssuggest parentheses around assignment used as truth value), which alerts the programmer to the likely mistake. In dynamic languages likeJavaScript,linters such as ESLint can warn on assignment inside a conditional.[12] Python 3.8 introduced assignment expressions, but uses the walrus operator:= instead of a regular equal sign (=) to avoid bugs which simply confuse== with=.[13]
Another disadvantage appears in C++ when comparing non-basic types as the == is an operator and there may not be a suitableoverloaded operator function defined. Example: a Microsoft'sCComBSTR compare against astring literal, written asif (L"Hello" == cbstrMessage), does not map to an overload function.[14]
One way to look at Yoda's syntax is that it shows signs of favoring OSV syntax (Object-Subject-Verb) as the basic order in the simple clause.
When nine hundred years old you reach, look as good you will not.
When nine hundred years old *you* reach, look as good *you* will not, hmm?