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

Option to allow naming of return values, but forbid use of them #17

Open
silverwind opened this issue Jul 12, 2023 · 4 comments
Open

Option to allow naming of return values, but forbid use of them #17

silverwind opened this issue Jul 12, 2023 · 4 comments

Comments

@silverwind
Copy link

silverwind commented Jul 12, 2023

Names return values are useful for documentation, but problematic when those variables are actually used because of mutation. I would propose a option to the linter to allow naming return values but forbid their use.

Pass

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

Fail

func add(a, b int) (sum int) {
  sum = a + b // error: do not use named return variables
  return sum
}
@wxiaoguang
Copy link

wxiaoguang commented Jul 12, 2023

What's the real difference between

func add(a, b int) int {
  var sum int
  // some other code
  // ...
  // ...
  sum = a + b
  return sum
}

and

func add(a, b int) (sum int) {
  // some other code
  // ...
  // ...
  sum = a + b // error: do not use named return variables.
  // Question: what's the real difference? why it should be forbidden?
  return sum
}

?

I can see in both cases, the "sum" is declared explicitly, I do not think the "Fail" one is harmful or it would cause any real problem.

@silverwind
Copy link
Author

Technically they are equal. I guess the only benefit of the var variant is that it may be easier to understand for someone coming from languages that do not have named return values.

One benefit of allowing optional naming is that (int, int, int) becomes more readable with names, e.g. (x, y, z int). But of course the same can be achieved with a var statement.

Preferring a functional code style myself, I do prefer for mutable variables to be explicitely declared which the var statement achieves.

@glacials
Copy link

glacials commented Aug 6, 2023

The difference is that function signatures are a form of documentation often revealed by editors. e.g.

image

vs.

image

@kke
Copy link

kke commented Nov 29, 2023

The main problem with named return values is using naked returns.

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

In my opinion it would be actually better to actually require named returns when a function returns two or more values of the same or similar type. You don't need named params for (*Foo, error) but you do for (int, int64, int).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants