2

From Wikipedia, the following sections of code are equivalent:

if (a > b) {
    result = x;
} else {
    result = y;
}

And:

result = a > b ? x : y;

Obviously the use of the ternary if is much more concise, though not as clear.

What I'm wondering is: Is it bad (In terms of readability of code, execution time etc.) to use the ternary if instead of a traditional if statement?

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
Dylan
  • 159
  • 5
  • 2
    It all depends on context. If it's a tiny, trivial comparison then no. Normally, probably, yes. – Joe Jan 10 '14 at 12:23
  • 1
    In that example, the use of the ternary is both more concise and *more* clear. – Sneftel Jan 10 '14 at 12:26
  • In contrast, see nested ternaries, ternaries used as rvalues, and side-effect-ful ternary operands. – Sneftel Jan 10 '14 at 12:27
  • because purrrrformmanncceeeee – Tony The Lion Jan 10 '14 at 12:28
  • 1
    To me this ternary is as much clear as `if...else`. It is very personal perception. There was kind of similar question somewhere, "Should I write code easier to understand for less skilled people." To me, if it looks good it's good. If you use 100+ character long ternary just to avoid `if..else`, then it starts to look stupid. – luk32 Jan 10 '14 at 12:30
  • A few considerations that come to mind: 1. It is easier to debug (step into) the conventional `if/else` statements. 2. It will not be very readable once the operands are more than just "simple" variables. 3. In some cases, you'd be taking the risk of "unknown" operator-precedence, although I think that in most cases, the ternary operator has the lowest priority (not sure about bit-wise operators). – barak manos Jan 10 '14 at 12:34
  • Duplicate question, too (cf link bottom of my answer [or click here](http://stackoverflow.com/questions/758849/the-ternary-conditional-operator-in-c)) – Elias Van Ootegem Jan 10 '14 at 12:34
  • An embedded programmer must always say no to ternary operator no matter how smooth the bit*h feels !Hmmm CONST hmpf – Gil Jan 10 '14 at 12:34
  • @Geekasaur: actually, `const` are an argument in favour of the ternary operator, as I've explained at the bottom of my answer – Elias Van Ootegem Jan 10 '14 at 12:43
  • @TonyTheLion: Performance? how (except for `const` assignments)? Asserting ternaries affect performance is, AFAIK, unfounded – Elias Van Ootegem Jan 10 '14 at 12:44

1 Answers1

4

It all depends. I can only think of one situation where a ternary operator undeniably improves readability and even performance (see bottom of this answer). But generally, using the ternary isn't going to improve readability/maintainability of your code. If you use it occasionally, and only for simple assignments then it's OK. not good, just acceptable IMO. Others are, of course, free to disgree with me on that.

Ternaries are expressions, so they should only be used as such (part of an assignment, for example). If you start using ternaries as statements, then you're in trouble. Ternaries are not statements, using them as such is like using a spoon as a screwdriver. Silly.

On the whole, though: it's a matter of taste as long as you don't overdo it. Once you start nesting ternaries, using them with pointers to pointers, or as statements, it's a matter of lunacy.
But to be honest: when in doubt: don't use the ternary operator. It saves you 1 line of code and doesn't improve performance. Take your example:

if (a > b) result = x;
else result = y;
//vs:
result = a > b ? x : y;

if result is the return value of a function, you could even write:

if (a > b) return x;
return y;

Even though I now have 2 return statements, I still find this code easier to read than:

result = a > b ? x : y;
//or
return a > b ? x : y;

That one use-case where a ternary is the best option is this:

const int two_options = some_arg ? 23 : 1;

This enables you to define + initialize a const within a function's scope, depending on a condition, without the overhead of an additional function call (const int two_opts = arg_func(some_arg);, where the function contains the if-else)

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
  • +1, but with reluctance. E.g. in the case of simple assignments, it is rather "good" than "OK". "OK" sounds as if you'd want to say "If you absolutely want, then do it." That's too weak, IMHO. But I agree it is a matter of personal taste. – glglgl Jan 10 '14 at 12:44
  • @glglgl: I'm not saying good, because, as someone commented: debugging ternaries is _"harder"_ than debugging traditional `if-else`'s, especially when pointers are involved (`*some_ptr2ptr->foo = *arg ? *arg->foo : 0;` -> find the NULL pointer) – Elias Van Ootegem Jan 10 '14 at 12:48
  • 1
    In this case, you don't have a "simple" assignment any longer, and readability is heavily affected, so I agree not to use it in this case. – glglgl Jan 10 '14 at 12:49
  • @glglgl: Nips... fair point. Forgot to apply the rule I just advocated :-D – Elias Van Ootegem Jan 10 '14 at 12:50
  • +1 For the const case. – Joe Jan 10 '14 at 12:53