To ternary or not to ternary? [closed]

2018-12-31 21:49发布

I'm personally an advocate of the ternary operator: () ? : ; I do realize that it has its place, but I have come across many programmers that are completely against ever using it, and some that use it too often.

What are your feelings on it? What interesting code have you seen using it?

30条回答
千与千寻千般痛.
2楼-- · 2018-12-31 22:12

As others have pointed out they are nice for short simple conditions. I especially like them for defaults (kind of like the || and or usage in javascript and python), e.g.

int repCount = pRepCountIn ? *pRepCountIn : defaultRepCount;

Another common use is to initialize a reference in C++. Since references have to be declared and initialized in the same statement you can't use an if statement.

SomeType& ref = pInput ? *pInput : somethingElse;
查看更多
深知你不懂我心
3楼-- · 2018-12-31 22:12

Chained I'm fine with - nested, not so much.

I tend to use them more in C simply b/c they're an if statement that has value, so it cuts down on unnecessary repetition or variables:

x = (y < 100) ? "dog" :
    (y < 150) ? "cat" :
    (y < 300) ? "bar" : "baz";

rather than

     if (y < 100) { x = "dog"; } 
else if (y < 150) { x = "cat"; }
else if (y < 300) { x = "bar"; } 
else              { x = "baz"; }

In assignments like this, I find it's less to refactor, and clearer.

When I'm working in ruby on the other hand, I'm more likely to use if...else...end because it's an expression too.

x =   if (y < 100) then "dog"
    elif (y < 150) then "cat"
    elif (y < 300) then "bar"
    else                "baz"
    end

(although, admittedly, for something this simple, I might just use the ternary operator anyway).

查看更多
永恒的永恒
4楼-- · 2018-12-31 22:14

It's a question of style, really; the subconscious rules I tend to follow are:

  • Only evaluate 1 expression - so foo = (bar > baz) ? true : false, but NOT foo = (bar > baz && lotto && someArray.Contains(someValue)) ? true : false
  • If I'm using it for display logic, e.g. <%= (foo) ? "Yes" : "No" %>
  • Only really use it for assignment; never flow logic (so never (foo) ? FooIsTrue(foo) : FooIsALie(foo) ) Flow logic in ternary is itself a lie, ignore that last point.

I like it because it's concise and elegant for simple assignment operations.

查看更多
忆尘夕之涩
5楼-- · 2018-12-31 22:14

If you're using the ternary operator for a simple conditional assignment I think it's fine. I've seen it (ab)used to control program flow without even making an assignment, and I think that should be avoided. Use an if statement in these cases.

查看更多
谁念西风独自凉
6楼-- · 2018-12-31 22:15

I use the ternary operator where ever I can, unless it makes the code extremely hard to read, but then that's usually just an indication that my code could use a little refactoring.

It always puzzles me how some people think the ternary operator is a "hidden" feature or is somewhat mysterious. It's one of the first things I learnt when I start programming in C, and I don't think it decreases readability at all. It's a natural part of the language.

查看更多
几人难应
7楼-- · 2018-12-31 22:15

I agree with jmulder: it shouldn't be used in place of a if, but it has its place for return expression or inside an expression:

echo "Result: " + n + " meter" + (n != 1 ? "s" : "");
return a == null ? "null" : a;

The former is just an example, a better i18n support of plural should be used!

查看更多
登录 后发表回答