In a previous Hidden Gems post, I covered the artistic side of programming as expressed through the use of the ternary operator. Here is the original post if you’re interested, otherwise here’s a quick summary of the concept: programming relies on the use of conditional statements, where a certain path is taken depending on the value of a variable or expression, and a ternary operator is an elegant, shorthand way of writing these conditionals. In the example below, the function isItSnowing()
will return a true or false value telling us if it is snowing. If it is snowing, then we’ll stay inside, and if it is not, then we’ll go outside. The ternary operator has unlimited potential, with the ability to string multiple conditions together, but must be used carefully; just because it improves readability and style in this scenario does not mean it always will.
The null coalescence operator is a slightly more nuanced version of the ternary operator, but even more interesting due to it’s use cases. Oftentimes in programming, you want to assign a value to a variable, but you don’t know for sure that the value exists. The value might come from another source and you can’t guarantee that it was received correctly or as expected. In most programming languages, trying to use a value that was never created causes a disaster (one can easily see why), and will usually crash the program. However, programmers would usually prefer to provide some default value instead, to prevent the program from crashing. Here is that exact situation presented in relatively simple, but verbose code:
This is a fairly straightforward conditional statement, which says in plain English, “if the value of temperature is known, the message will be that value, but if for some reason we don’t have the temperature, let the message be a default warning that we don’t have the temperature”. If you’ve been paying close attention, you might have noticed that this conditional is very similar to the one we explored when discussing ternary operators, and as a matter of fact, this conditional can be expressed using a ternary operator, as demonstrated here:
Notice again the power of the ternary operator in simplifying the conditional. It compacts the same logic into one statement, which reduces the number of lines and is more efficient to run. Now behold the same statement written using the null coalescence operator:
The differences may be subtle at first, but they are extremely important. Notice that there is no expression to test whether or not “temperature” exists, as it is built into the operator. Also see that it automatically assigns the value of “temperature” if it exists. Finally notice that the default message comes after the null coalescence operator, and is assigned when the value of “temperature” does not exist. A common way to read the operator in English is “reading from left to right, give me back the first thing that exists”. This use case is extremely common in programming, which is what ultimately led to the null coalescence operator being created. Personally, I was thrilled when I discovered this: after writing hundreds of the equivalent ternary versions, I had finally discovered a way to write less code while also improving the style of my code. Once a programmer is used to using this operator, it reads much more naturally than the ternary operator and saves valuable time when trying to understand the code. This operator can also be chained, similar to the ternary operator, as shown in this example, where the default string is used if both the “temperature” and “backupMessage” variables are null:
At the end of the day, the null coalescence operator may not seem like a revolutionary idea, and it isn’t. However, it is a testament to the ingenuity and artistry of programmers, motivated by a unique combination of laziness and empathy for the programmers who follow. It is the accumulation of these small improvements that evolve into the modern programming languages we have today, which are more powerful and easier to read than ever before.