Saving lines - the usage of ternary and null-coalescing operators

DZone 's Guide to

Saving lines - the usage of ternary and null-coalescing operators

· ·
Free Resource

Think about the situations when you had to write multiple IF statements to verify the validity of a statement or a value. More than that, think about the situations when you had to basically repeat almost the same IF statements in several cases – the code volume becomes huge. Take a look at this sample piece of code:

int i = 0;
int b = 10;
int c = 120;
int d = 12;

if (i == 0)
Debug.Print("Not zero");

if (b == 10)
Debug.Print("Not 10");

if (c == 120)
Debug.Print("Not 120");

if (d == 12)
Debug.Print("Not 12");
Mind, that this is generic code and many other situations can be referenced to this specific example. However, it shows the idea behind multiple  verifications on different values. The good news is – this code can be significantly shortened. And the key here is the ternary IF operator (also known as the conditional operator). The ternary IF operator is a specific feature of the C-type languages (like C++ or C#), however it was implemented in other languages as well (like VB.NET or Perl). It allows to put the above mentioned IF statements in a single line, sometimes saving significant amounts of code.

I am going to show the implementation of the code above using ternary IF operators and later I am going to explain its structure.

int i = 0;
int b = 10;
int c = 120;
int d = 12;

Debug.Print(i== 0 ? i.ToString() : "Not zero");
Debug.Print(b == 10 ? b.ToString() : "Not 10");
Debug.Print(c == 120 ? c.ToString() : "Not 120");
Debug.Print(d == 12 ? d.ToString() : "Not 12");

Pretty concise, isn’t it? Although it might seem a bit confusing, the overall structure is pretty simple. The general idea behind it is this:

Statement [IF] ? Action 1 [THEN – CONDITION VERIFIED] : Action 2 [ELSE – CONDITION FAILED]

The important part about ternary operators is that those cannot be used as statements. I can only use them in assignments or method calls.

Another element that is worth mentioning on the same topic is the null-coalescing operator. It works similar to the ternary operator, but it has a different purpose. It checks whether the passed instance (either a variable or class instance) is null. Personally, I found it extremely useful when reading XML that is obtained via different web APIs. Since XML nodes can be null, it is good to check whether those can be used further in the application.

An example of using the null-coalescing operator is below:

string a = null;
Debug.Print(a ?? "Null");

Notice the fact that it doesn’t have an ELSE part. It only triggers the specified action if the object is null.

Both ternary operators and the null-coalescing operator can be easily used to minimize the volume taken by IF statements. Their structure is easy enough to understand, making the code readable even with a short representation.

In many cases when elements like these are used (shorthand versions of full-sized statements), I advise not to over-use them. This is not the case. I advise the developers who used two-way IF-statements before to consider ternary operators and the null-coalescing operator.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}