What are the benefits and drawbacks of the ?: operator as opposed to the standard if-else statement. The obvious ones being:
Conditional ?: Operator
- Shorter and more concise when dealing with direct value comparisons and assignments
- Doesn't seem to be as flexible as the if/else construct
Standard If/Else
- Can be applied to more situations (such as function calls)
- Often are unnecessarily long
Readability seems to vary for each depending on the statement. For a little while after first being exposed to the ?: operator, it took me some time to digest exactly how it worked. Would you recommend using it wherever possible, or sticking to if/else given that I work with many non-programmers?
Sometimes it can make the assignment of a bool value easier to read at first glance:
The advantage of the conditional operator is that it is an operator. In other words, it returns a value. Since
if
is a statement, it cannot return a value.This is pretty much covered by the other answers, but "it's an expression" doesn't really explain why that is so useful...
In languages like C++ and C#, you can define local readonly fields (within a method body) using them. This is not possible with a conventional if/then statement because the value of a readonly field has to be assigned within that single statement:
is not the same as:
In a similar way you can embed a tertiary expression in other code. As well as making the source code more compact (and in some cases more readable as a result) it can also make the generated machine code more compact and efficient:
...may generate less code than having to call the same method twice:
Of course, it's also a more convenient and concise form (less typing, less repetition, and can reduce the chance of errors if you have to duplicate chunks of code in an if/else). In clean "common pattern" cases like this:
... it is simply faster to read/parse/understand (once you're used to it) than the long-winded if/else equivalent, so it can help you to 'grok' code faster.
Of course, just because it is useful does not mean it is the best thing to use in every case. I'd advise only using it for short bits of code where the meaning is clear (or made more clear) by using
?:
- if you use it in more complex code, or nest ternary operators within each other it can make code horribly difficult to read.The scenario I most find myself using it is for defaulting values and especially in returns
Those are really the only places I find it nice, but for them I do.
Though if you're looking for a boolean this might sometimes look like an appropriate thing to do:
Because it's so easy to read and understand, but that idea should always be tossed for the more obvious:
I usually choose a ternary operator when I'd have a lot of duplicate code otherwise.
With a ternary operator, this could be accomplished with the following.
While the above answers are valid, and I agree with readability being important, there are 2 further points to consider:
This makes it particularly concise to use the ternary:
If you have types
T1
andT2
that can both be implicitly converted toT
, then the below does not work:(because the compiler tries to determine the type of the ternary expression, and there is no conversion between
T1
andT2
.)On the other hand, the
if/else
version below does work:because
T1
is converted toT
and so isT2