Yoda conditions
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
|
In programming jargon, Yoda conditions (also called Yoda notation) 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 spoke English with non-standard syntax.
Yoda conditions are part of the WordPress coding standards.[1]
Example
Usually a conditional statement would be written as:
if ( $value == 52 ) { /* ... */ }
// Reads like: "If the value is equal to 52..."
Yoda conditions describe the same expression, but reversed:
if ( 52 == $value ) { /* ... */ }
// Reads like: "If 52 equals the value"
The constant is listed first, then the variable being compared to, analogous to the speaking style of Yoda (“When nine hundred years old you reach, look as good you will not."[2][3]).
Advantage
Placing the constant value in the expression does not change the behavior of the program (unless the values evaluate to false – see below). With programming languages that use = for assignment rather than for comparison, a possible mistake is to accidentally assign a value instead of writing a conditional statement.
if (myNumber = 52) { /* ... */ }
// This equals the new value of myNumber and not the desired condition, and overwrites the value of myNumber
Using Yoda conditions:
if (52 = myNumber) { /* ... */ }
// This is a syntax error and will not compile
Since 52 is a constant and can not be changed, this error will be caught by the compiler.
Boolean myBoolean = true;
if (myBoolean = null) { /* ... */ }
// This causes a NullPointerException in Java Runtime, but legal in compilation.
It can also solve some types of unsafe null behavior.
String myString = null;
if (myString.equals("foobar")) { /* ... */ }
// This causes a NullPointerException in Java
With Yoda conditions:
String myString = null;
if ("foobar".equals(myString)) { /* ... */ }
// This is false, as expected
Criticism
Critics of Yoda conditions see the lack of readability as a disadvantage that does not outweigh the benefits described above. Some programming languages do not allow variable assignments within conditionals, so this error is impossible to make.
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.
See also
References
- ^ https://make.wordpress.org/core/handbook/coding-standards/php/#yoda-conditions
- ^ "The StarWars.com 10: Best Yoda Quotes". starwars.com. Lucasfilm, Ltd. 2013-11-26. Retrieved 2014-12-22.
When nine hundred years old you reach, look as good you will not.
- ^ "Quotes for Yoda (Character)". imdb.com. Amazon. Retrieved 2014-12-22.
When nine hundred years old *you* reach, look as good *you* will not, hmm?
External links
- united-coders.com: What are Yoda Conditions? Examples in Java
- New programmier jargon Mentions Yoda Conditions in a list of new programming jargon
- Coding in Style Probable origin of the term
- Yoda Conditions in Java Potential pitfalls of the technique