出典:Wiktionary
出典:『Wiktionary』 (2026/02/20 20:06 UTC 版)
An allusion to the somewhat grammatically reversed speech style of Yoda, a character in the Star Wars franchise—for example, "Truly wonderful, the mind of a child is." The term was possibly coined by Félix Cloutier (username “zneak”) in 2010, based on Yoda notation which is claimed to have been coined by Thomas M. Tuerke and published online in 2006.
Yoda condition (plural Yoda conditions)
| Example |
|---|
The condition |
= (assignment) instead of == (equality), an error that is harder to spot when using the normal order of operands. [from early 21st c.]
if(isHungry = true) by accident will set isHungry to true every time the if statement is hit! To avoid this possibility, we can just write if(isHungry). Alternatively, some (wise) people use what are called Yoda conditions to check an if statement: if(true == isHungry). The reason we write the if statement in this way is that, if we accidentally write if(true = isHungry), this will generate a compiler error, catching the mistake.
x == 7 with the variable name (an lvalue) on the left and the literal (an rvalue) on the right. Placing the literal on the left, as in 7 == x (which is syntactically correct and is sometimes called a "Yoda condition"), enables the compiler to issue an error if you accidentally replace the == operator with =. The compiler treats this as a compilation error because you can't change a literal's value.