Benefits of using the conditional ?: (ternary) ope

2018-12-31 10:22发布

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?

17条回答
柔情千种
2楼-- · 2018-12-31 10:33

Sometimes it can make the assignment of a bool value easier to read at first glance:

// With
button.IsEnabled = someControl.HasError ? false : true;

// Without
button.IsEnabled = !someControl.HasError;
查看更多
零度萤火
3楼-- · 2018-12-31 10:33

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.

查看更多
春风洒进眼中
4楼-- · 2018-12-31 10:38

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:

readonly int speed = (shiftKeyDown) ? 10 : 1;

is not the same as:

readonly int speed;  
if (shifKeyDown)  
    speed = 10;    // error - can't assign to a readonly
else  
    speed = 1;     // error  

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:

MoveCar((shiftKeyDown) ? 10 : 1);

...may generate less code than having to call the same method twice:

if (shiftKeyDown)
    MoveCar(10);
else
    MoveCar(1);

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:

object thing = (reference == null) ? null : reference.Thing;

... 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.

查看更多
荒废的爱情
5楼-- · 2018-12-31 10:38

The scenario I most find myself using it is for defaulting values and especially in returns

return someIndex < maxIndex ? someIndex : maxIndex;

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:

bool hey = whatever < whatever_else ? true : false;

Because it's so easy to read and understand, but that idea should always be tossed for the more obvious:

bool hey = (whatever < whatever_else);
查看更多
低头抚发
6楼-- · 2018-12-31 10:41

I usually choose a ternary operator when I'd have a lot of duplicate code otherwise.

if (a > 0)
    answer = compute(a, b, c, d, e);
else
    answer = compute(-a, b, c, d, e);

With a ternary operator, this could be accomplished with the following.

answer = compute(a > 0 ? a : -a, b, c, d, e); 
查看更多
伤终究还是伤i
7楼-- · 2018-12-31 10:41

While the above answers are valid, and I agree with readability being important, there are 2 further points to consider:

  1. In C#6, you can have expression-bodied methods.

This makes it particularly concise to use the ternary:

string GetDrink(DayOfWeek day) 
   => day == DayOfWeek.Friday
      ? "Beer" : "Tea";
  1. Behaviour differs when it comes to implicit type conversion.

If you have types T1 and T2 that can both be implicitly converted to T, then the below does not work:

T GetT() => true ? new T1() : new T2();

(because the compiler tries to determine the type of the ternary expression, and there is no conversion between T1 and T2.)

On the other hand, the if/else version below does work:

T GetT()
{
   if (true) return new T1();
   return new T2();
}

because T1 is converted to T and so is T2

查看更多
登录 后发表回答