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

x/lint: recommend parameters in func type signatures #37384

Closed
l0k18 opened this issue Feb 23, 2020 · 15 comments
Closed

x/lint: recommend parameters in func type signatures #37384

l0k18 opened this issue Feb 23, 2020 · 15 comments
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone

Comments

@l0k18
Copy link

l0k18 commented Feb 23, 2020

It could go further into a language change, but as a Go programmer who frequently uses function types constructing a reasonable standardised set of parameter names for these types of functions would be helpful for a reason that might not be obvious - you can then just copy the signature from the type spec and start the function immediately, and the names will be the same and thus more readable.

Thus, I am proposing golint start recommending the use of parameter names in function type specifications in order to improve readability and make functional programming in go just a little more streamlined.

@l0k18 l0k18 changed the title gofmt improvement proposal - recommend parameters in func type signatures golint improvement proposal - recommend parameters in func type signatures Feb 23, 2020
@ianlancetaylor ianlancetaylor changed the title golint improvement proposal - recommend parameters in func type signatures x/lint: recommend parameters in func type signatures Feb 23, 2020
@gopherbot gopherbot added this to the Unreleased milestone Feb 23, 2020
@ianlancetaylor
Copy link
Contributor

This suggestion doesn't seem to fit with the scope section of https://go.googlesource.com/lint/+/refs/heads/master/README.md. It would also cause lint warnings in a few places in the standard library, such as matchStringOnly in testing/testing.go or Filter in go/ast/filter.go or HandlerFunc in net/http/server.go. Linter warnings in the standard library aren't the worst thing, but they suggest that there is perfectly reasonable code that doesn't use function parameter names in func types, and we don't want to have linter warnings for perfectly reasonable code.

I suggest that you use your own tool for this, perhaps based on lint, to give warnings for code that violates your preferred style. It doesn't seem like something appropriate for the general purpose tool.

@l0k18
Copy link
Author

l0k18 commented Feb 23, 2020

If the rule specifically was that the function was exported the parameters that are function types should have a function type specification and the specification should have default parameters. That probably would bypass the issue you mention about the standard library because you are probably referring to internal functions. If they are internal functions their reuse potential is basically nonexistent anyway.

@ianlancetaylor
Copy link
Contributor

Two of the examples I cited from the standard library are not internal functions, as you can see from their names.

@l0k18
Copy link
Author

l0k18 commented Feb 23, 2020

Since it makes no changes to functionality, those standard library function typed parameters being nicely specified would break no promises and is obviously not a high priority but also is a tiny amount of work, and clears up the linter problem (which is why to make it in the linter).

It's just good manners, I think, but not obvious until you write a lot of handling libraries. Once you have copied the signature, added arbitrary names to the parameters so you can use the function a few times you start to notice that it would be nice if you didn't have to think of the names every time. By making it not flag non-exported function type function parameters also it isn't getting in the way when you only mean to write one or two different handlers for it and maybe think in future you need an easy option to add more, but aren't thinking that you want users of the library to do it.

I have a pretty strong opinion about exporting and the use of internal, personally. I default to not exporting symbols unless they need to reach outside of their scope, but if it helps reusability and reduces obstructions to re-engineering/re-architecting when things are exposed. Some things don't have to be, so they are fine. But it's damned stupid to completely hide them in internal IMO. I've found more than a few times I needed something inside an internal folder. Only way to reuse that is to copy and paste and do a lot of refactoring. This reduces maintainability. I don't think it serves any purpose whatsoever, except some kind of vanity about one's ability to predict the future.

@ianlancetaylor
Copy link
Contributor

I agree that naming the parameters would not break compatibility. That wasn't my point. My point is that as a general rule lint should not emit any warnings for well written idiomatic Go code. We think that the Go code in the standard library is well written and idiomatic. So the fact that the proposed warning would trigger on code in the standard library is a mark against it.

@cagedmantis cagedmantis added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Feb 26, 2020
@cagedmantis
Copy link
Contributor

/cc @matloob

@l0k18
Copy link
Author

l0k18 commented Feb 26, 2020

Doesn't the linter have 'recommendations' and not just 'this is (probably) bad code' warnings?

@ianlancetaylor
Copy link
Contributor

Yes. But it remains the case that we don't expect the linter to fire on ordinary code.

Also, again, see the Scope section of https://go.googlesource.com/lint/+/refs/heads/master/README.md. That describes the kind of changes we expect to implement in x/lint.

@l0k18
Copy link
Author

l0k18 commented Feb 26, 2020

I'm sure you read those documents well, maybe even contributed a lot to them, but I see nothing in it that mentions function types much less function types as parameters. It's not 'out of scope' it's 'not been mentioned'. If that's where the contribution would be made, how does one go about submitting an amendment of these?

@ianlancetaylor
Copy link
Contributor

See the comments at the top of https://golang.org/wiki/CodeReviewComments.

@l0k18
Copy link
Author

l0k18 commented Feb 27, 2020

https://github.com/golang/go/wiki/CodeReviewComments#named-result-parameters

That seems to be quite in agreement with my proposal, just doesn't directly address the use of functions as parameters to functions. It even mentions the fact right at the end that closures create extra conditions under which parameters in function signatures should be named, that could easily be expanded.

Thanks, I'll see if there is any interest in such an amendment as the link suggests (to make a discussion post in the wiki)

@matloob
Copy link
Contributor

matloob commented Feb 27, 2020

The behavior of lint is pretty much frozen at this point. I'd suggest making a different tool that provides these parameters. We've got a bunch of packages such as golang.org/x/tools/go/analysis that makes making these tools pretty easy.

@l0k18
Copy link
Author

l0k18 commented Feb 27, 2020

It's not a static analysis feature it's a lint feature. It doesn't affect compilation in the slightest, nor performance, whether you put names in there or not makes no difference to the binary that comes out, at all.

@matloob
Copy link
Contributor

matloob commented Feb 27, 2020

The analysis framework provides tools for writing tools that suggest diagnostics like the one you're suggesting adding here, and also provides a way to suggest code replacements to users too. Diagnostics and analyses don't need to affect compilation or performance to be produced by or written with the analysis framework.

@rsc
Copy link
Contributor

rsc commented Mar 11, 2020

#37495 was declined, so closing this one as well.

@rsc rsc closed this as completed Mar 11, 2020
@golang golang locked and limited conversation to collaborators Mar 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

6 participants