-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
Proposal: selector-specific control flow & gofmt rules to improve error-checking readability #39372
Comments
Sorry for the misoperation |
See also https://github.com/golang/go/wiki/Go2ErrorHandlingFeedback |
I agree that the error checking and returning in the simple case if verbose in go. On the other hand it forces the developer to think about the actual handling, i.e. to not only return the error but also implement additional cleanup tasks (closing files etc). In the proposal here I'm unclear how return values for the non-error results would be passed back? |
@andig I want to make the distinction between handling an error (where appropriate action is taken and the error is not returned) and not handling an error (the error is transformed in some way, clean-up tasks are run and the error is returned so that somewhere up the stack it can get handled). In Go, errors are explicit and a decision to handle needs to be made every time an error value is recieved. This is one of Go's strengths. Handling errors in Go is great. Returning errors are... not so great. It looks like the error is being handled and this obstructs the readabillity of the code. In my experience, Go doesn't force you to handle errors, it can actually discourage you from doing so. "My function is incredibly long and unreadable from all this error checking, why would I want to make it worse, I'll just return the error". An argument can be made that having a shorter way to return errors will encourage less error handling. However I would speculate that the inverse can be true, having shorter functions psychologically gives the developer more room for error-handling. Less is more! Keep in mind that this proposal is not strictly about saving characters, it's about moving error-passing to the right to improve readability. Personally I'm not a fan of previous check/try/? proposals (not explicit enough, everything is hidden, etc). To clarify the behavior of non-error results: |
It seems like having very long lines with error handling on the right will tend to obscure the error handling. I understand that that is kind of the point of this issue. But errors do need to be handled. Hiding the handling at the end of a long line doesn't clarify the function, it obscures it. If we don't try to put the error handling at the end of a long line, we could put it on the next line. But then we aren't saving all that much with this approach. We're saving an I feel like the |
Based on the comments above, and the lack of support in emoji voting, this proposal is a likely decline. Leaving open for four weeks for final comments. |
No further comments. |
In order to reduce basic error-checking bloat in Go software and improve readability, I propose shifting eligible error-checking to the right by the addition of control-flow that operates on the error type and a related gofmt rule.
Errors passed to the caller should not interrupt logical-flow
Go has an error-checking issue. Whenever a function is called that returns a value and an error. At least 3 additional lines of code are required to check it.
This isn't an issue when the code is actually handling the error (as the error handling becomes part of the logical-flow of the code) but when the error is simply passed to the caller, the error-checking obstructs the logical-flow of the code and makes it more difficult to understand what the code is trying to do.
In a perfect world, there wouldn't be any errors and IO functions could be written as such.
The behaviour of this function is easy to reason about.
Now consider the OpenImage functions below, OpenImage2 checks errors and OpenImage1 does not. The logical-flow of OpenImage1 is easier to read than OpenImage2.
Most of the time, errors do need to be checked though and this causes the logical-flow of the code to be obstructed. Here is another example from here.
Trying to read functions like this is jarring as the reader is interrupted by error checking at almost every step.
Proposal: selector-specific control flow
For a value x of type error, x.return inside of function F terminates the execution of F
if x != nil, and optionally provides one or more resulting error values. Any functions deferred by F are executed before F returns to its caller.
There are three ways to return values from a function with a result type:
Proposal: gofmt rules for error.return
Result
What impacts will this have on Go
Pros
Cons
The text was updated successfully, but these errors were encountered: