-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: spec: error handling with err != nil ? return #71808
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
Comments
see https://go.dev/doc/faq#Does_Go_have_a_ternary_form |
This proposal is not about providing yet another “conditional control flow construct” with ternary operator. This proposal is for error-handling and we should see from lens of solving error-handling problem and not in lens of introducing ternary operator support. @seankhliao I would appreciate it if this proposal could remain open for a day or two to allow for further input from the Go community. Closing the proposal based on the reasoning that Why Go doesn't support the ternary operator seems like a swift decision, and it may be beneficial to consider additional viewpoints before concluding. |
I can reopen this if you like, but this looks like a different syntax for an |
Thanks, @ianlancetaylor for follow-up. 👍
Here, we need to see how much it's simplifying error-handling. I have been writing Go code on the job for more than 8 years, and I definitely see value in the below left-side error-handling being converted to right-side error-handling.
Again, we need to see a ternary operator to simplify error-handling rather than an alternative to if/else.
As stated in proposal for Kubernates project, 88% of error-handing code will simplify with this proposal. This will be true for any Go project. One can run below # number of times "if err != nil" is checked
$ cat **/*.go | pcregrep --buffer-size=1000000 -M "if err != nil {" | wc -l
# number of times "if err != nil" is checked and has one line in if block
$ cat **/*.go | pcregrep --buffer-size=1000000 -M "if err != nil {\n.*\n.*}" | wc -l
I would request you to reopen and see how much Go community finds this proposal as practical progress for error-handling. Thank you. |
This solves nothing for the interleaving of errors with other values.
this is exactly the same as before for reading and just reinforces the wrong idea that the problem many have with error handling is "not wanting to write 3 lines" |
While the analogy to the C-style ternary conditional operator is a useful hook to introduce this, it seems like what's proposed here is quite different:
Therefore I guess I would characterize what's proposed here as a "conditional return statement" rather than as a general-purpose "conditional operator", and assume it has a specification something like the following:
Does the above seem like a suitable interpretation of the proposal? The first advantage listed under "Pros" admittedly does seem to suggest that you intended to permit any statement to appear in the second and optional third clauses, but all of your examples show Since not having a conditional expression operator in Go was an intentional design choice (as previously discussed), I think it's best to focus narrowly only on the error-handling use-case for the sake of this proposal, since otherwise your proposal would need to offer some justification for revisiting the position that the C-style conditional operator tends to be hard to read. |
Thank you all for sharing thoughts on the proposal. I can now see challenges with this proposal, like less readability with right-side error-handling code and there are differences compared to be ternary operator. I also gather some thoughts / feedbacks from reddit (link). @ianlancetaylor @seankhliao thanks for your feedback and reopening this proposal which helped me to better understand issues with the proposal. I am closing this proposal, Thank you Go team. |
Problem
The primary issue with current error handling is the repetitive nature of the error-handling code.
Particularly 3-lines of error handling code block.
Example as below,
Proposal
Let's introduce support for the ternary operator in Go and use it to simplify error handling.
Syntax:
Note
A separate proposal for the "Ternary operator in Go" will be required once this error-handling proposal is approved.
Let's understand how ternary operator will simplify error-handling.
Example (repetition of error handling code)
Example (error handling using ternary operator)
Example (error handling using ternary operator "with parentheses")
Here is a version with parentheses. This
(err != nil)
insteaderr != nil
.The decision on whether to include parentheses for condition will be determined in the ternary operator proposal.
Overall,
3-lines
of error handling code block is converted to1-line
with the help of ternary operator.3-lines
1-line
Also, 3-lines of error handling code block is most used in go projects. Below are details about "Kubernetes" project, which is one of the biggest Go projects.
Kubernetes error handling details
In kubernetes project, there are 41466 times err is checked, out of that 36799 times having 3-lines of error handling code block.
These 36799 (88% of 41466) error handling code block can use ternary operator.
Pros
Pros-1: Orthogonal
Yes, ternary operator will be orthogonal to Go language.
The ternary operator will be a general feature, allowing the replacement of any if condition that has a single-line if and else section (else section is optional).
Below if condition,
Using ternary operator,
Pros-2: Backward compatible
Yes, the addition of ternary operator is backward compatible.
The existing error-handling method will remain unchanged. Users will have the option to use the ternary operator for error handling or stick with the traditional approach.
Pros-3: Error is Value
Error is Value in Go and Error will remain Value in Go with this proposal.
This proposal does not change any semantics of Go errors. Errors in Go will continue to be values.
Cons
Cons-1: Not apply to all error-handling scenario
Scenario-1
A scenario with error-handling code spanning multiple lines cannot utilise a ternary operator.
To utilize the ternary operator, the error-handling code can be moved to a separate function, as shown below.
In general, the scenario of multi-line error-handling is comparatively very low. Most cases involve single-line error-handling, where the ternary operator shines.
Scenario-2
A scenario with an in-line error check cannot use the ternary operator.
To use the ternary operator, the in-line check can be separated, as shown below.
While it's subjective, using the ternary operator in this scenario seems more streamlined.
Cons-2: Two ways of doing if/else
Go advocates one way of doing a particular thing. The ternary operator now provides an alternative way to implement if/else.
But there is a chance that Go developers might prefer using the ternary operator over if/else when the logic in the if/else section is a single line.
Discussion
What is the scope of this proposal wrt error-handling?
This proposal focuses on single-line error handling, which accounts for over 80% of cases in Go projects (for ex, Kubernetes, as previously detailed out). While it covers a major part of error handling, it doesn't address all scenarios.
Is this the full and final proposal for error-handling?
This proposal suggests introducing a ternary operator in Go, which can help address challenges related to error handling. However, it is not directly for error handling, leaving room for future error-handling proposals.
Ternary operator details
This proposal primarily focuses on error handling rather than the introduction of the ternary operator in Go. A separate proposal is needed to fully detail ternary operator support in Go, covering various aspects, including the following:
(err != nil)
insteaderr != nil
max = (a > b) ? a : b
max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)
Additional details of the Proposal
Go Programming Experience
Experienced. I have been using Go at work for over 8 years.
Other Languages Experience
Java, C, C++
Related Idea
Has this idea, or one like it, been proposed before?
No. As per my knowledge.
Does this affect error handling?
Yes
Is this about generics?
No
Is this change backward compatible?
Yes. Check Pros.
Orthogonality: How does this change interact or overlap with existing features?
Check Pros.
Would this change make Go easier or harder to learn, and why?
The ternary operator is a well-known programming construct, making it easy to use with no learning curve.
Click to expand for more details
Language Spec Changes
It could be part of the ternary operator proposal.
Informal Change
It could be part of the ternary operator proposal.
Cost Description
It could be part of the ternary operator proposal.
Changes to Go ToolChain
It could be part of the ternary operator proposal.
Performance Costs
It could be part of the ternary operator proposal.
Prototype
It could be part of the ternary operator proposal.
The text was updated successfully, but these errors were encountered: