Hello, I’m fried fish.

This is a point in time when many other language engineers are switching to Go, which makes comparisons inevitable. 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 of poor design. Is history going backwards?

Today we are going to explore why fish fry.

What is a ternary operator

A ternary operator, in the typical mathematical sense, or from the parser’s point of view, is an operator that takes three arguments. In our daily lives, the most common binary operators are:

x + y
x / y
x * y
Copy the code

There are also unary operators:

-a ~b ! cCopy the code

And the hero of the day, the triple operator. In several languages, such as C/C++, we can optionally use ternary conditional operators based on our conditional declaration and initialization habits:

int index = val > 0 ? val : -val
Copy the code

Go uses ternary operators

When I tried to use the ternary operator in Go, I didn’t find it. The only way to implement the same code snippet as above seems to be:

var index int

if val > 0 {
    index = val
} else {
    index = -val
}
Copy the code

It seems redundant and not concise enough.

Why doesn’t Go have a ternary operator

Why didn’t Go? : operator, if not, what is the official recommended method.

We can learn from the Go FAQ:

Go officially recommended us to use the above mentioned method to replace, and made clear the following attitude:

  • Not in Go? The reason for: is that language designers see this operation often used to create complex expressions that are difficult to understand.

  • On the alternative, the if-else form, while longer, is undoubtedly clearer. A language needs only one conditional control flow structure.

Overall, Go’s designers rejected ternary operators for readability, and “less is more.” was a catchphrase.

Community dispute

There are some things about Go that are different and almost universally known. Whether it’s if ERR! = nil, or the ternary operator in this case, we want you to replace it with if-else:

if expr {
    n = trueVal
} else {
    n = falseVal
}
Copy the code

Objection and consent

against

Therefore, some community partners gave objections, which were basically divided into the following categories:

  1. The designer’s argument that if-else can be abused in similar cases is not sufficient and is considered an “excuse”.

  2. It is considered that the “ugly” problem of ternary operators is a coding problem of developers, not a language problem. Triples are common in various languages, they are normal, and Go should have them as well.

  3. Replacing the ternary operator with if-else was also considered cumbersome, causing the developer to read 3-4 more lines and an extra level of indentation.

agree

There are also a lot of people who agree with this decision, which gives a lot of real engineering cases.

In general, we want to use the ternary operator like this:

cond ? true_value : false_value
Copy the code

You may have seen it used this way:

cond ? value_a + value_b : value_c * value_d
Copy the code

And I’ve seen this:

(((cond_a ? val_one) : cond_b) ? val_two) : val_three

cond_a ? (val_one : (cond_b ? (val_two : val_three)))
Copy the code

It can also nest ternary operators:

int a = cond_a ? val_one :
    cond_b ? val_two :
    cond_c ? val_three : val_four;
Copy the code

Less readable ones can also appear:

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; }Copy the code

To put it simply, in real code engineering, you’ve seen a lot of misuse of ternary operators, giving a lot of hard to understand examples, and making you confused.

conclusion

In this article, we first give a basic introduction to “ternary operators”. Then according to the Go language does not support triadic attitude is explained, and community-oriented disputes are divided into positive and negative basic interpretation.

Actually a simple one? : Neat and useful, but there is no good and efficient way to prevent ugly nesting, i.e. eliminate readability problems.

In real business projects, it’s common to see a ternary operator that starts out simple. The deeper the nesting becomes, the more complex the logic is written, which brings many maintenance problems.

Here’s the question:

  • Do you think the Go language should have ternary operators?

  • What about complex nested ternary operators, if any?

You are welcome to leave your comments in the comments section 🙂

If you have any questions, welcome feedback and communication in the comments section. The best relationship is mutual achievement. Your praise is the biggest motivation for the creation of fried fish, thank you for your support.

This article is constantly updated. You can read it on wechat by searching “Brain into fried fish”. GitHub github.com/eddycjy/blo… Already included, learning Go language can see Go learning map and route, welcome Star urged more.