Yoda conditions

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 [1] and Symfony coding standards.[2]

Example

Usually a conditional statement would be written as:

if ( $value == 42 ) { /* ... */ }
// Reads like: "If the value is equal to 42..."

Yoda conditions describe the same expression, but reversed:

if ( 42 == $value ) { /* ... */ }
// 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 speaking style of Yoda, which is roughly object–subject–verb[3] (e.g., “When nine hundred years old you reach, look as good you will not."[4][5]).

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 = 42) { /* ... */ }
// This assigns 42 to myNumber instead of evaluating the desired condition

Using Yoda conditions:

if (42 = myNumber) { /* ... */ }
// 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 (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 outweighs the benefits described above. Some programming languages as Python and Swift do not allow variable assignments within conditionals, by defining assignments to not return a value, in which case this error is impossible to make.[6] Many compilers produce a warning for code such as if (myNumber = 42) (e.g., the GCC -Wall option warns suggest parentheses around assignment used as truth value), which alerts the programmer to the likely mistake.

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 CComBSTR compare against a string literal, written as if (L"Hello" == cbstrMessage), does not map to an overload function [7]

See also

References

  1. https://make.wordpress.org/core/handbook/coding-standards/php/#yoda-conditions
  2. http://symfony.com/doc/current/contributing/code/standards.html#structure
  3. 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. External link in |website= (help)
  4. "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.
  5. "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?
  6. https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/BasicOperators.html
  7. https://msdn.microsoft.com/en-us/library/cfy54764.aspx

External links

This article is issued from Wikipedia - version of the Monday, March 21, 2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.