Yoda Condition

"Zero, this variable equals."

A YodaCondition is a binary comparison expression, typically found in conditional statements such as if and while statements, where the value on the left of the operator is the expectation and the value on the right is the actual. Simple examples include:

 if(0==variable) ...

while(false == endingCondition) ...

These statements sound odd when read left to right; "while false equals the ending condition" sounds much better as "while the ending condition equals false" which would be the natural way of reading the statement with the operands reversed:

 while(endingCondition == false) ...

A YodaCondition can reduce readability of code, however it can also be a safety feature in languages where the assignment operator is "=" while the equality operator is "=="; see CompareConstantsFromTheLeft.


EditText of this page (last edited August 24, 2012) or FindPage with title or text search