Yoda Conditions
Yoda Conditions (also: Yoda notation ) denote the programming jargon a form of source code formatting, with the condition within a two parts of the expression are reversed. The name for this programming style is derived from the character Yoda from the Star Wars universe from which tended to exchange a few words within a sentence.
Example
Usually, programmers write a condition in this case, a conditional statement like this:
If ( value == 52) {/ * ... * /} / / Reads like: If the value is equal to 52 .. Yoda Conditions now denote the reverse position of the expression of content:
If ( value == 52 ) {/ * ... * /} / / Reads like: If 52 equal value ... Thus, the constants in this programming style listed first, then the variable comparison value. Analogous to the pronunciation of the name giver Yoda ( " Forget what you have learned earlier you " ) this leads to a strange-looking discussion of the condition: "If zero is equal worth ... ".
Advantage
The interchanging the two conditional values does not change the behavior of the program. Although read worse for the programmer this condition can be considered the first example, this application has in those programming languages that allow assigning a value using " =" in an expression, the advantage of the careless, to make the condition a variable assignment excluded:
If ( value = 52 ) {/ * ... * /} / / If ( unintentionally by the programmer ) always true if ( value = 52 ) {/ * ... * /} / / Creates a syntax error The example below prevents careless mistakes, as is output or generate an error at run time or at compile time: 52 is a constant; this value can not be assigned.
In some languages can thus also the invalid dereference a null value to be avoided:
String value = null; if ( wert.equals ( " foobar " )) {/ * ... * /} / / In Java here a NullPointerException is thrown if ( " foobar ". equals ( value) ) {/ * ... * /} / / If (as expected) always wrong criticism
Critics of the notation style to see the lack of readability as a predominant disadvantage of not outweigh the supposed problem of Flüchtigkeitsfehlers described above. It is argued that modern development environments and mark this line as a possible error. Modern programming languages such as C # also does not allow a variable assignment within a condition anyway. In the D programming assignments have no Boolean data type and can not therefore be used as an if - criterion.