C# Ternaries

C# Ternaries

A ternary operator is a conditional statement. It's easiest to think of it as an alternate if else statement. The syntax is fairly easy, and it can be a useful tool in certain cases.

It can be used to make short compact code when doing operations.  They allow you to assign variables quickly in a single line. Ternaries are quick to type with only a few keystrokes, often written in a single line. But they can come with some drawbacks.

Here is the basic formatting of a ternary:

bool r = condition ? true : false;

In this example, if the condition is true, the r would be set to true, if it is false the result would be false.

bool r = x > 6 ? true : false;

Here if x is greater than six the r would be true. If it's equal to or less than r would be false.

The issue with ternaries is code readability. Ternaries can be nested, and often when I see them in code I am working on, they are.

Nested ternaries become nearly impossible to read quickly. This is an issue when someone is reading through 10,000 lines of code to find out how an application is working (or not).

Another issue is that ternaries are not perfect replacements for if-else. If calculation and methods need to be run perform the if-else then a ternary is not a great choice. Which is fine, we can switch from a ternary to an if-else when needed.

But if we constantly switch back and forth between if-else and ternary we are increasing the possibility of the code being read incorrectly by someone maintaining that code. This may not seem to matter when writing code, but if you want to keep writing new code, then you need to make sure someone else can maintain what you wrote.  

Let's look at some code comparing a ternary to an if-else statement. Additionally, let's look at another short-handed method that is also available.

Decent Ternary

These lines are fairly easy to follow and read, they are a decent use of Ternaries. Ternaries are very compact, but if you are reading 2,00 lines of code the lack of white space makes them extremely hard to read. They often get overlooked and missed when application maintenance takes place.

Bad Ternary

This example is a nested ternary. It's a  single line for the assignment, but is confusing to read. Even though it does the same thing as the other examples it can take some time to figure out exactly what it's doing.

If-Else

The If-Else is good, it's useful if you are doing something besides a simple variable assignment here. The issue is that a lot of times you are only doing one thing, like calling a method or assigning a variable.

If-Else Compact

If you only have to do one thing, then I prefer the If-Else Compact shorthand method. This method uses the fact that a single thing can be done after an if or else statement without the need to wrap it in brackets. This leaves the code extremely readable.

When it comes to the If-Else vs. If-Else Compact, I have had some pushback from other developers. Their main complaint has been that the Compact method isn't available in some languages. That argument didn't really hold water for me, as there are lots of other differences amongst languages to worry about as well.  The other is that some people consider it slightly harder to spot in code. I have worked on hundreds of legacy applications and never really had this issue. I think the spacing in the code makes them easier to spot and the code easy to read.

Ternaries can be really useful, but some care and thought need to be taken when incorporating them into code. Nesting and sporadic use can both lead to mistakes and difficulty when maintaining code bases.