-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x/website: reword FAQ answer on lack of ternary operator #36288
Comments
This comment has been minimized.
This comment has been minimized.
// @dmitshur |
Is the phrasing "unquestionably clearer" not still present in the proposed change? Personally as someone who questions the clarity of both workarounds versus a one-line ternary expression, I can actually feel my blood pressure go up as I read that. Also, I'd urge reconsidering this stance. I understand that Go is targeting making code that isn't clever, is easy to read, and is resistant to making and hiding simple subtle mistakes (although variable shadowing and typed nils are pretty big strikes against this goal IMO).
Why not just restrict the return values to exclude expressions - include only literals, constants, and variables? It prevents using the expressions for control flow, prevents the creation of lengthy expressions, and prevents the use of nested ternary if since that would be an expression. It would still cover the main cases that are frequently brought up (binary configuration choices and substituting default values for nil / zero values). Then perhaps everyone can be happy? |
You should also remove the last line:
as it doesn't apply to Go. Go has two control flow constructs: if-else and the switch statement. Indeed, as stated here: https://golang.org/doc/effective_go.html#switch
Compare:
to:
Both are perfectly valid Go. |
You should really add ternary operator instead. Looking at those examples it looks like you don't need ternary operator, but when you initialize huge structure, then the code becomes really ugly because instead of couple ?: in struct initialization I have to add bunch of lines with if statements after struct initialization. |
Go version:
1.13
Introduction
There is recurring proposal about "ternary operator" in the issues (e.g. 1, 2, 3...). This suggestion is in hope to reduce their frequency in future.
Problem
Trouble seems to have two parts:
It seems currently there is strong opposition to improve the 1-st part. Then perhaps we can improve the 2-nd?
Further I shall copy current text and proposed change to it, but here are main points:
Supposed update to text
this variant is a bit lengthy, feel free to suggest cuts
Currently there is no conditional expression in Go (neither ternary expression, nor anything similar). General workaround is:
which often may be shortened to (at the expense of reassignment):
The reason for absence of conditional expression in Go is the popular opinion among developers (in various languages), that it may lead to poor code readability and style, e.g.:
Future versions of Go may introduce some alternatives (like allowing return value from
if-else
or type parameters for functions which will allow creating custom operator-like function etc.). Please don't post yet another issue "add ternary operator". It is omitted by design and is not going to be added at least in the same form as inC++
andJava
.Current Text
There is no ternary testing operation in Go. You may use the following to achieve the same result:
The reason ?: is absent from Go is that the language's designers had seen the operation used too often to create impenetrably complex expressions. The if-else form, although longer, is unquestionably clearer. A language needs only one conditional control flow construct.
UPD: yet one more about the same #36303
The text was updated successfully, but these errors were encountered: