Meaning of Yoda condition | Babel Free
ˌjəʊdə kənˈdɪʃn̩Definitions
A logical condition with the usual order of operands reversed for various reasons, such as avoiding the accidental misuse of = (assignment) instead of == (equality), an error that is harder to spot when using the normal order of operands.
Equivalents
Español
condición Yoda
Français
condition Yoda
Examples
“A great convention to stick⟳ to, the Yoda condition requires you to keep⟳ the comparison variable on the right side of the conditional statement.”
“A computer programmer is trying to explain⟳ a recent problem to a journalist friend, and she says, "This jimmy had put⟳ in Yoda conditions and had been refactoring my previous work⟳ so that in the end⟳ it was just hooker code." The journalist friend looks baffled.”
“// what happens if the debug parameter is removed // from the request⟳ and becomes a null? // should really be using a Yoda Condition here ;)”
“Writing 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⟳.”
“Leo grimaces at Caleb. "I never asked—how did you figure⟳ it out?" / "Yoda conditions. You may as well have⟳ signed your name⟳." At my blank look⟳, Caleb says, "Your boyfriend writes his code backwards."”
“Programmers normally write⟳ conditions such as 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.”
CEFR level
B2
Upper Intermediate
This word is part of the CEFR B2 vocabulary — upper intermediate level.
This word is part of the CEFR B2 vocabulary — upper intermediate level.
See also
Know this word better than we do? Language is a living thing — help us keep it growing. Collaborate with Babel Free