Skip to content
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: gofmt should format single-statement error return on 1 line instead of current 3 lines. #33113

Closed
urban-wombat opened this issue Jul 15, 2019 · 3 comments
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge Proposal v2 An incompatible library change
Milestone

Comments

@urban-wombat
Copy link

This is a proposal, not an fault report. It's about if statements of the form:

if err != nil {
return err // with possible addition arguments based on the return list
}

A simple rule change in gofmt would solve the visual issue (too many lines: 3) without compromising Go's error philosophy. Just change gofmt to format single-line if statements that return an error value, as a single line, e.g.: if err != nil { return err }

err := myFunction()
if err != nil { return err }

What version of Go are you using (go version)?

$ go version 1.12.7 windows/amd64

Does this issue reproduce with the latest release?

gofmt works correctly for me. My issue is a proposed change to help with err != nil issue.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env


set GOARCH=amd64
set GOBIN=f:\Dropbox\golang/bin
set GOCACHE=C:\Users\Malcolm\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=f:\Dropbox\golang
set GOPROXY=
set GORACE=
set GOROOT=c:\go
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=G:\cygwin64\tmp\go-build401339902=/tmp/go-build -gno-record-gcc-switche

What did you do?

What did you expect to see?

gofmt works correctly for me. My issue is a proposed change to help with err != nil issue.

What did you see instead?

gofmt works correctly for me. My issue is a proposed change to help with err != nil issue.

@gopherbot gopherbot added this to the Proposal milestone Jul 15, 2019
@urandom
Copy link

urandom commented Jul 15, 2019

I don't think it will do anything visually. IMHO it might actually make readability worse, since now there will be this dense piece of code that will stick out like a sore thumb.

@rsc
Copy link
Contributor

rsc commented Jul 16, 2019

As Rob said in his proverbs talk, "Gofmt's style is no one's favorite, yet gofmt is everyone's favorite." It is far more important to have one format than to debate minor points. There is not a compelling reason to change this one. (The original rationale was to keep conditional code clearly separated from the surrounding code; that still seems like it applies, but the bigger point is that gofmt format is basically done.)

See https://go-proverbs.github.io/ and the links to video clips there.

@urban-wombat
Copy link
Author

Proposal #33113 could do with before and after code samples to make it clear what is meant by reducing boilerplate err != nil code from 3 lines to 1 line.

The formatting changes have been made to func unhappyFunc() (int, string, error) in the following two before and after samples on Go Playground:

  1. With existing gofmt rules func unhappyFunc() has 35 lines of code vs 23 lines for the proposed gofmt formatting.

  2. With the proposed gofmt rules func unhappyFunc() has 23 lines of code vs 35 lines of code.

  • There are no syntax changes required for the Go compiler.
  • Nothing is hidden from the programmer.
  • Boilerplate error handling no longer outnumbers by 3 to 1 the line of code it checks.

@bradfitz bradfitz added the error-handling Language & library change proposals that are about error handling. label Oct 29, 2019
@golang golang locked and limited conversation to collaborators Oct 28, 2020
@ianlancetaylor ianlancetaylor added the v2 An incompatible library change label Jan 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
error-handling Language & library change proposals that are about error handling. FrozenDueToAge Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

6 participants