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 allow short inlined if-return statements #27135

Closed
eldandev opened this issue Aug 21, 2018 · 7 comments
Closed

proposal: gofmt should allow short inlined if-return statements #27135

eldandev opened this issue Aug 21, 2018 · 7 comments

Comments

@eldandev
Copy link

You've seen it everywhere:

if err != nil {
    return err
}

Let's allow if err != nil { return err }

@gopherbot gopherbot added this to the Proposal milestone Aug 21, 2018
@ALTree
Copy link
Member

ALTree commented Aug 22, 2018

Statements that drastically change the program's control flow (like return) should be prominent and easy to spot. Inlining an if-return like that makes it less visible. It may be ok when the return statement is the whole body of the function, like in

func add(a, b int) { return a + b }

(which is allowed by gofmt), but I don't think it is when it's a jump buried inside an if somewhere in the function.

@eldandev
Copy link
Author

@ALTree if functions are so big you can't spot a return statement inside an if statement then the issue is with the cleanliness of the code, I think allowing to inline if-return will help reduce the clutter in the eyes.

Small functions or even just functions that adhere to Command & Query separation will not have the issue you describe.

Thanks for the quick reply!

@FlorianUekermann
Copy link
Contributor

I wholeheartedly support this. I never dared to push it because I'm sure it has been discussed before. Just to get the typical pro arguments out there:

  • Vertical space is much scarcer than horizontal space nowadays (with most people having 16:9 screens or similar).
  • It's been allowed for functions and that freedom has not caused much friction between people in my experience. So I doubt not enforcing a canonical format will be a major issue.
  • Almost everyone I talked to about this is unhappy about how much space the current formatting wastes and how much it distracts from the more interesting lines (almost 3:1 loc ratio of trivial error handling cod VS other code happens in some cases).
  • Adding distance between lines of code makes code more difficult to understand and read. People forget stuff if they need to scan or scroll.

@bronze1man
Copy link
Contributor

bronze1man commented Sep 3, 2018

@FlorianUekermann

Adding distance between lines of code makes code more difficult to understand and read. People forget stuff if they need to scan or scroll.

People forget stuff when there is too much information in the brain short memory any way. Making the code less line can not solve this problem.
Two statement one line may need the brain store more information, like there is two thing in that line, one thing in this line, the horizontal position of the statement. If all lines only have one statement, the brain do not need to store that information, it just need remember the vertical position of the statement.
If someone is smart, he can understand and remember the code with two statements in one line or one statement in one line easily. But I think he should sympathize his colleague which is not so smart. He also should put his smart into the real problem(like data race/dead lock/use too much memory) he need to solve.

Vertical space is much scarcer than horizontal space nowadays (with most people having 16:9 screens or similar).

The screen is big so you can see two files or three files in vertical parallel.

Also note:

  • Putting two statements in one line can be used to cheat line based test coverage tool.
  • Putting two statements in one line can make the panic stack trace more difficult to debug, since the panic stack trace only have line number.

@eldandev
Copy link
Author

eldandev commented Sep 3, 2018

@bronze1man
I agree that generally one line should do 1 thing but in my experience when the line is short enough if-returns as well as terinary operator usages are easy enough to follow, I agree that they could potentially be missed in long functions but for short ones that issue doesn't exist IMHO

It seems they are going to solve the clutter issue in go 2 by having a block of error handling, so that might solve the core issue

@networkimprov
Copy link

@rsc
Copy link
Contributor

rsc commented Sep 19, 2018

We decided long ago not to allow this kind of density. If the problem is specifically error handling, then we have other ideas for that (as noted). But the decision that there are no 1-line if statements is done.

@rsc rsc closed this as completed Sep 19, 2018
@golang golang locked and limited conversation to collaborators Sep 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants