Usefulness of Yoda expressions in programming

yodaIn 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 spoke English in a non-standard syntax.

The first time you see a Yoda expression it actually looks really awkward. It looks something like this

To a new Developer, but maybe even to a seasoned one, this simple line of code will be reversed to a keep a normal looking if statement. It’s not so common to see a statement like this. But when are they useful? How come they exist? Who thought of them?

Prevent Assignment

Newcomers to the software development world might not have ever developed in languages like C or other languages that do not have boolean types.

In C a boolean type does not exist, thus an if statement is evaluated to 0 for false and to any other number for true.

When writing code in a language that treats boolean types as first class citizens (e.g. Java or C#) if you have this line of code

compiler will fail on this line of code and tell you that it is not of a Boolean type and you can easily spot this error and add the missing equals sign.

But in a language like C the above if statement will always evaluate to true. Why?
Because simply x will be assigned the value of 5 and since Boolean values do not exist in C and C uses numerical values to identify if a statement is true or false, any non zero value is evaluated to true, thus x=5 will evaluate to true. This will lead the developer to countless hours of debugging (especially a novice developer) to find out why the statement is always executed even thought the value of x is not always 5. All of that for just a missing equals sign.

Now Yoda expressions come in to play. In a Yoda expression we will reverse the items in the if statement.

and if we make a mistake it will look like this

In any language you can think of you cannot assign a value to a constant literal. Thus when compiling the above statement will throw a syntax error. In this way, a developer will not have to spend any time at all for debugging.

This is not useful only in languages with no boolean types though. In Javascript, Boolean types do exist, but given that Javascript is not a strong typed language, in an if condition you can place any type of condition, or variable, or value and the the check will be performed. So any value that is “truthy” will evaluate to true (e.g. any not empty string, including “false”, any not zero value etc) and any value that is “falsey” (e.g. undefined, null, empty string, number zero) will evaluate to false. So this is also helpful in languages like Javascript, that do have Boolean types, but still allow anything to be placed in an if statement.

Null Checks

There are many occasions in which you need to check if one “thing” is equal to another. In many of those case one of them is nullable. You could write code like the following

but what happens if s is null? You would get a null reference exception at runtime. Same thing goes for objects, nullable integers etc. So to avoid this conflict with nullable items you would need to write something like

Overly verbose don’t you think? As programmers we have the need to write the least amount of code to serve our needs.
Well we can reduce the verbosity of our code with Yoda expressions simply by writing

We don’t care if s is null, no exception will be thrown and the condition will evaluate to false if s is null.

Once again Yoda expressions to the rescue.

Please follow and like us:

Leave a Reply