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

Discussion on named return values #831

Closed
rootulp opened this issue Aug 13, 2022 · 4 comments
Closed

Discussion on named return values #831

rootulp opened this issue Aug 13, 2022 · 4 comments

Comments

@rootulp
Copy link
Collaborator

rootulp commented Aug 13, 2022

Golang has named return values so a function can be written with unnamed return values:

func parseMsgShares(shares [][]byte) ([]Message, error) {

or named return values

func parseMsgShares(shares [][]byte) (messages []Message, err error) {

celestia-core currently uses both forms which leads to code style nits in PR comments. If we reach a decision on which form is preferred, we can enforce it via linters and avoid this type of code style inconsistency.

⚠️ named return parameters enable bare returns which reduce readability so if we do enforce named return values, we should also enforce no bare returns.

Ref:

Original discussion: #820

@rootulp
Copy link
Collaborator Author

rootulp commented Aug 13, 2022

@adlerjohn points out two drawbacks to using named return values:

Bare returns are one big issue. The other is un-obvious side effects. Assigning to a named return value is a side-effect to an otherwise pure function, and there's nothing in the syntax to indicate this explicitly to the reader. Named return values are basically global variables.

I don't understand the second drawback (re: global variables). To my knowledge, a named return value is declared inside the scope of the function so a function that uses named return values can still be pure.

@rootulp rootulp changed the title Discussion on named return parameters Discussion on named return values Aug 13, 2022
@evan-forbes
Copy link
Member

evan-forbes commented Aug 15, 2022

...which reduce readability so if we do enforce named return values, we should also enforce no bare returns.

I almost never use them for bare returns, but sometimes I like adding the names as documentation and saving me a line or two of variable declarations.

func() (has bool) {}
func(t []int) (s []int) {
    for _, i := range t {
        if i == 4 {
            s = append(s, i)
        }
    }
    return s
}

tbh though I realllllllyyyy don't care. if someone has a strong preference we can do that as long as we do not change tendermint code

@evan-forbes
Copy link
Member

sorry, hit control + enter at the wrong time and it closed the issue...

@rootulp
Copy link
Collaborator Author

rootulp commented Aug 15, 2022

as long as we do not change tendermint code

That's a really good point. Any decision we reach here would only be productive if enforced through linters. I think minimizing the diff between celestia-core and tendermint/tendermint is a more important goal than codebase consistency with regard to this minor code style rule.

My preference would be to remain consistent with whichever linters are enforced by https://github.com/tendermint/tendermint/blob/main/.golangci.yml

@rootulp rootulp closed this as not planned Won't fix, can't repro, duplicate, stale Aug 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants