Skip to content

proposal: Go 2: reducing "if err != nil" by implicit form of it #41908

@alisherfozilov

Description

@alisherfozilov

Explicit is better than implicit, but there may be exceptions of the rule.

Briefly

Before:

		request, err := http.NewRequest(http.MethodGet, "http://example.com", nil)
		if err != nil {
			log.Println(err)
			return
		}
		_ = request

After:

		request, err := http.NewRequest(http.MethodGet, "http://example.com", nil) {
			log.Println(err)
			return
		}
		_ = request

Proposal q&a

  • Would you consider yourself a novice, intermediate, or experienced Go programmer?
    Novice. 6 months using Go.

  • What other languages do you have experience with?
    C/C++ and just a little bit of Java, C#, Python, JS, Elixir.

  • Would this change make Go easier or harder to learn, and why?
    Insignificantly harder to learn: change introduces one simple thing to know.

  • Has this idea, or one like it, been proposed before?
    Yes, there are 1 relative (Proposal: gofmt should format single-statement error return on 1 line instead of current 3 lines. #33113), 2 relatively similar (proposal: Go 2: Use ?<variable> simplify handling of multiple-return-values #33074, proposal: Go 2: add "or err: statement" after function calls for error handling #33029), 2 very similar (proposal: Go 2: "onerr return" #32848, proposal: Go 2: improve error handling with “??” keyword for a special case #37243) proposals.

    • If so, how does this proposal differ?
      Described below in the answer on question "Does this affect error handling?".
  • Who does this proposal help, and why?
    This proposal helps every Go programmer and to every who switches to Go from another language. Why?
    It will reduce "if err != nil" code line, improves readability and developer's productivity, because no one will see "if err != nil" after every function call, no one will wasting time typing this line. Code will become clearer.
    For those switching from another language to Go, this will soften their attitude towards error handling in Go.

  • What is the proposed change?
    Introducing new syntax construction when function call.

    • Please describe as precisely as possible the change to the language.
      If after function call's curly brace there is "{" symbol on the same line, then there should automatic check happen "is the function's last value is not nil?". If yes, then the block that was opened with "{" symbol will execute. Otherwise that block will skipped.
    • What would change in the language spec?
      Nothing.
    • Please also describe the change informally, as in a class teaching Go.
      We just get rid of the explicit typing "if err != nil" after every function call (that returns an error).
  • Is this change backward compatible?
    Yes, absolutely.

  • Show example code before and after the change.
    Shown at the beginning of the proposal.

  • What is the cost of this proposal? (Every language change has a cost).
    The cost is time of implementing it.

    • How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
      Many tools. Compiler, vet, gofmt, IDEs.
    • What is the compile time cost?
      There is a insignificant compile cost i think. I can't answer exactly.
    • What is the run time cost?
      I think there is no run time cost. I can't answer exactly.
  • Can you describe a possible implementation?
    No.

  • How would the language spec change?
    One paragraph describing this change will be added.

  • Orthogonality: how does this change interact or overlap with existing features?
    The change does not conflicts or overlap existing features.

  • Is the goal of this change a performance improvement?
    The goal is increasing performance of developer: in creating and reading code.

    • If so, what quantifiable improvement should we expect?
      Reducing the number of lines of code in Go projects.
    • How would we measure it?
      By counting number of lines of code in public Go repositories.
  • Does this affect error handling?
    Yes.
    If so, how does this differ from previous error handling proposals?
    The reason, why proposal: Go 2: Use ?<variable> simplify handling of multiple-return-values #33074 and proposal: Go 2: add "or err: statement" after function calls for error handling #33029 were declined is as said in proposal: Go 2: add "or err: statement" after function calls for error handling #33029 (comment) :

... it requires a new keyword, which while not impossible, is a higher bar for a language change. It also introduces yet another assignment form ...

The reason, why #32848 was declined due to a problems with scope and introducing one more keyword.
The reason, why #37243 was declined due to a problems with ?? operator that has another meaning is C#. (and in my opinion operator ?? just does not fit the Golang language)

Current proposal does not do this. There is no new keyword, no new assignment form and no new operator. Language stay as simple as before.

  • Is this about generics?
    No.

Author's words

The only questions I have for now are:

  1. Is there any problem with this approach?
  2. Is it hard to implement technically and will it have a big impact on compile time?
  3. If this change is good enough, then we need to think about what is the function last return value should be when using such flow construction. Is it must be only error interface or any interface? Maybe it can be any type which can be nil?

In my opinion checking "if err != nil" is a clear and great error handling mechanism and it seems that nothing can really take place of it in Go. But it's a problematic to type this line literally after every function call, that return an error. Readability suffers too.
So let the compiler do the job for us. Reducing this line will have a big impact on code readability and programming experience.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions