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:17

A so many answers have said, it depends. I find that if the ternary comparison is not visible in a quick scan down the code, then it should not be used.

As a side issue, I might also note that its very existence is actually a bit of an anomoly due to the fact that in C, comparison testing is a statement. In Icon, the if construct (like most of Icon) is actually an expression. So you can do things like:

x[if y > 5 then 5 else y] := "Y" 

... which I find much more readable than a ternery comparison operator. :-)

There was a discussion recently about the possibility of adding the ?: operator to Icon, but several people correctly pointed out that there was absolutely no need because of the way if works.

Which means that if you could do that in C (or any of the other languages that have the ternery operator), then you wouldn't, in fact, need the ternery operator at all.

查看更多
ら面具成の殇う
3楼-- · 2018-12-31 22:17

I use it quite often in places where I'm constrained to work in a constructor - for example, the new .NET 3.5 LINQ to XML constructs - to define default values when an optional parameter is null.

Contrived example:

var e = new XElement("Something",
    param == null ? new XElement("Value", "Default")
                  : new XElement("Value", param.ToString())
);

or (thanks asterite)

var e = new XElement("Something",
    new XElement("Value",
        param == null ? "Default"
                      : param.ToString()
    )
);

No matter whether you use the ternary operator or not, making sure your code is readable is the important thing. Any construct can be made unreadable.

查看更多
低头抚发
4楼-- · 2018-12-31 22:18

For simple if cases, I like to use it. Actually it's much easier to read/code for instance as parameters for functions or things like that. Also to avoid the new line I like to keep with all my if/else.

Neseting it would be a big NO-NO in my book.

So, resuming, for a single if/else I'll use the ternary operator. For other cases a regular if/else if/else (or switch)

查看更多
梦该遗忘
5楼-- · 2018-12-31 22:19

I typically use in things like this:

before:

if(isheader)
    drawtext(x,y,WHITE,string);
else
    drawtext(x,y,BLUE,string);

after:

    drawtext(x,y,isheader==true?WHITE:BLUE,string);
查看更多
浪荡孟婆
6楼-- · 2018-12-31 22:19

I love them, especially in type-safe languages.

I don't see how this:

int count = (condition) ? 1 : 0;

is any harder than this:

int count;

if (condition)
{
  count = 1;
} 
else
{
  count = 0;
}

edit -

I'd argue that ternary operators make everything less complex and more neat than the alternative.

查看更多
永恒的永恒
7楼-- · 2018-12-31 22:21

I like 'em. I don't know why, but I feel very cool when I use the ternary expression.

查看更多
登录 后发表回答