Yoda conditions: Difference between revisions
m →External links: Correct spelling. |
Remove hat note for no citations. |
||
Line 1: | Line 1: | ||
{{no footnotes|date=September 2013}} |
|||
In [[Computer programming|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 (computer programming)|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 in a non-standard syntax. |
In [[Computer programming|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 (computer programming)|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 in a non-standard syntax. |
Revision as of 21:44, 22 December 2014
This article includes a list of references, related reading, or external links, but its sources remain unclear because it lacks inline citations. (September 2013) |
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 in a 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 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 speaking style of Yoda, which is roughly object–subject–verb[2] (e.g., “When nine hundred years old you reach, look as good you will not."[3][4]).
Advantage
Placing the constant value in the expression does not change the behavior of the program (unless the values evaluate to false—see below). 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 (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, in which case 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
- ^ Pullum, Geoffrey K. (2005-05-18). "Yoda's Syntax the Tribune Analyzes; Supply More Details I Will!". http://itre.cis.upenn.edu/~myl/languagelog/. Language Log. Retrieved 2014-12-22.
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.
{{cite web}}
: External link in
(help)|website=
- ^ "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 programming 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