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 speaks English with a non-standard syntax.

Yoda conditions are part of the Symfony[1], and the WordPress[2] coding standards.

Example

Usually a conditional statement would be written as:

if ($value == 42) { /* ... */ }
// Reads like: "If the value equals 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 English 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

Error detections

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.

Avoiding some types of unsafe null behavior

Yoda conditions help with unsafe behavior in some situations.

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

Expressing mathematics

Yoda conditions can be easier to understand when the expression represents the real number line.

if -1 <= x <= 1:

Without yoda conditions:

if ((x >= -1) && (x <= 1)) { /* ... */ }

With yoda conditions:

if ((-1 <= x) && (x <= 1)) { /* ... */ }

Criticism

Yoda conditions are widely criticized for compromising readability by increasing the cognitive load of reading the code.[6][7][8]

Some programming languages (such as Swift and past versions of Python) 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 (that is, it would be detected as a syntax error by the parser prior to a program ever being allowed to enter into runtime).[9] 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. In dynamic languages like JavaScript, linters such as ESLint can warn on assignment inside a conditional.[10]

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

gollark: https://github.com/osmarks/autobotrobot/blob/master/src/irc_link.py#L75
gollark: Ah, but you actually did need to, because there was obviously truncation logic in the code.
gollark: Discord ones can, however, be bigger than IRC ones.
gollark: No.
gollark: Did you *read* the code?

References

  1. "Coding Standards (Contributing to Symfony)". Symfony.com. Retrieved 2016-11-12.
  2. "PHP Coding Standards - Make WordPress Core". make.wordpress.com. Retrieved 2019-08-15.
  3. Pullum, Geoffrey K. (2005-05-18). "Yoda's Syntax the Tribune Analyzes; Supply More Details I Will!". Itre.cis.upenn.edu. 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.
  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. "Quick tips for reducing the cognitive load of your code". March 21, 2017.
  7. "Why using Yoda conditions you should probably not be". August 2, 2017.
  8. "Yoda Conditions: Why You Shouldn't Use Them". August 16, 2017.
  9. "The Swift Programming Language (Swift 3.0.1): Basic Operators". Developer.apple.com. 2016-10-27. Retrieved 2016-11-12.
  10. "disallow assignment operators in conditional statements". eslint.org. Retrieved 2017-02-17.
  11. "CComBSTR::operator". Msdn.microsoft.com. Retrieved 2016-11-12.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.