Hello everyone, I am fried fish.
This is a time point for many other language engineers to switch to the Go language, and it is inevitable to make a comparison. One of the classic operations is the "ternary operator":
Why does the Go language not support ternary operators? Go does not support ternary operators because it is not well designed. Is history going backwards?
Today, let’s try the fried fish to explore why.
What is the ternary operator
The ternary operator, in a typical mathematical sense, or from the perspective of a parser, is an operator that requires three parameters. In our daily life, the most common binary operator is:
x + y
x / y
x * y
There are also unary operators:
-a
~b
!c
And today's actor "Ternary Operator". In C/C++ and other languages, we can selectively use ternary conditional operators according to the habits of conditional declaration and initialization of variables:
int index = val > 0 ? val : -val
Go uses ternary operators
When I want to use the ternary operator in the Go language, I found that there is no... the way to achieve the same code segment as above seems to be:
var index int
if val > 0 {
index = val
} else {
index = -val
}
It seems very redundant and not concise enough.
Why does Go have no ternary operator
Why doesn't Go have the ?:
operator? If not, what is the official recommended method?
Through Go FAQ we can know:
Go officially recommends that we use the previously mentioned method instead, and the following attitude is clear:
- The reason for the absence of
?:
in Go is that language designers see that this operation is often used to create complex expressions that are difficult to understand. - In the alternative, although the if-else form is longer, it is undoubtedly clearer. A language only needs one conditional control flow structure.
On the whole, the designers of the Go language considered readability and refused to implement the ternary operator, and "less is more." is also a advertised line.
Community dispute
Some of the differences in the Go language are basically known to everyone. Whether it is if err != nil, or the ternary operator this time, you need to use if-else instead:
if expr {
n = trueVal
} else {
n = falseVal
}
Object and agree
be opposed to
Therefore, some community partners have objected, and they are basically divided into the following categories:
- It is believed that if-else can be abused in similar circumstances, and the designer’s reason is not sufficient, and it is regarded as an “excus”.
- Think that the "ugly" problem of ternary operators is a developer's coding problem, not a language problem. Trigrams are common in various languages, they are normal, and Go should have them.
- I think it's troublesome to replace the ternary operator with if-else, so developers read 3-4 lines and an extra level of indentation.
agree
Many people recognized this decision, and a large number of real engineering cases were given for this.
Generally speaking, we want to use the ternary operator in this way:
cond ? true_value : false_value
You may have seen it used like this:
cond ? value_a + value_b : value_c * value_d
Have seen this:
(((cond_a ? val_one) : cond_b) ? val_two) : val_three
cond_a ? (val_one : (cond_b ? (val_two : val_three)))
You can also nest ternary operators:
int a = cond_a ? val_one :
cond_b ? val_two :
cond_c ? val_three : val_four;
There can also be less readable ones:
void rgb_to_lightness_(
const double re, const double gr, const double bl, double &li)
{
li=((re < gr) ? ((gr < bl) ? bl : gr) : ((re < bl) ? bl : re) +
(gr < re)
? ((bl < gr) ? bl : gr)
: ((bl < re) ? bl : re)) / 2.0;
}
To put it bluntly, in real code projects, you have seen a large number of scenarios where ternary operators are abused, and a large number of difficult to understand examples have been given, which puzzled everyone.
Summarize
In this article, I first give a basic introduction to the "ternary operator". Then it was explained based on the Go language's attitude that it does not support ternary, and the community-oriented disputes are divided into basic interpretations of pros and cons.
In fact, a simple ?:
is neat and practical, but there is no good and efficient way to prevent ugly nesting, that is, to eliminate the problem of readability.
In real business engineering, you can often see a ternary operator, is just very simple at the beginning. The deeper the nesting is, the more complicated the logic is, which brings many maintenance problems .
The following questions are thrown to everyone:
- Do you think the Go language should have ternary operators?
- If so, how to consider complex nested ternary operators?
Welcome everyone to leave a message and exchange in the comment area:)
If you have any questions, welcome feedback and exchanges in the comment area. The is mutual achievement of . Your is the biggest motivation for the creation of fried fish
The article is continuously updated, and you can read it on search [the brain is fried fish], this article 1617f745e67f33 GitHub github.com/eddycjy/blog has been included, learn the Go language can see Go learning map and route , welcome to remind more 1617f745e67f.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。