-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
unexportedglobal: Linter to require '_' prefix on globals #3941
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
Conversation
|
Hey, thank you for opening your first Pull Request ! |
|
In order for a pull request adding a linter to be reviewed, the linter and the PR must follow some requirements.
Pull Request Description
Linter
The Linter Tests Inside Golangci-lint
|
This sentence is partially wrong because all the linters are enabled when using I have seen different behavior with the handle of a new linter:
This is why for me the default behavior of a linter (and not default linters) is really important.
I should be a part of the style preset because it's a linter about style, nothing else is required to be in this preset. I personally dislike global variables: it's a source of confusion, and bugs, and can produce side effects. IMO only a few cases really need global variables. I'm also not a fan of prefixing things: it feels like a rule from another epoch or language. The fact that the rule comes from a company is just an authority argument, so it's not an argument that interests me. As you can understand, for now, I disagree with this rule and this linter, but I'm not alone on this project, I will wait for feedback. Also, I need to think more about this rule. So I'm not making any decision at this time. |
|
Thanks for looking over this, @ldez!
Makes sense. I'll add it to the preset.
Same! But are legitimate cases where you need them.
Fair. The prefixing is specifically to separate the namespaces -- similar to how exported variables are already a different namespace (because they start with uppercase letters).
It was not my intent to use the company's practices as an authority, only as an example. The idea is to prevent mistakes where you accidentally use or modify the global variable when you intended to use the local variable. By prefixing globals with This also has a nice effect on readability while reviewing code on a website (GitHub, Phabricator, whatever): when you see a |
ab39c45 to
07b0277
Compare
Your examples are the illustration of the problem because they are not valid use cases for me:
Another concrete illustration: you removed a global variable and an Valid use cases are, for example, analyzers that share their reports with other analyzers (like The discussion is a bit off-topic but it's interesting to talk about that 😸. |
07b0277 to
63bae19
Compare
This adds a unexportedglobal to golangci-lint. [unexportedglobal][1] is a linter that requires that unexported global variables and constants are prefixed with '_'. [1]: https://github.com/abhinav/unexportedglobal ```go package foo // Bad var pool = sync.Pool{ /* ... */ } // Good var _pool = sync.Pool{ /* ... */ } ``` The idea is to eliminate the risk of conflict between names of local variables and names of global variables. The linter is inspired by the [Prefix Unexported Globals with `_`][2] guidance in [Uber's Go Style Guide][3]. [2]: https://github.com/uber-go/guide/blob/master/style.md#prefix-unexported-globals-with-_ [3]: https://github.com/uber-go/guide/blob/master/style.md The linter is not enabled by default. I have also not added it to the 'style' preset because not everyone may agree with this change.
63bae19 to
fea8491
Compare
|
JFYI: some of Uber style guide rules were covered by Also gochecknoglobals (included in |
|
I don't want to be responsible for a new convention about global variable naming. This can be implemented as a plugin or as ruleguard rule. So we will decline the PR. thank you anyway, the discussion was fun. |
|
Reasonable. Thanks for reviewing. Thanks for the rule-guard tip, @Antonboom. |
|
@abhinav I respect your work and don't want to belittle it. |
No offense taken. Yep, an option in gochecknoglobals was indeed what I was considering next. 😄 |
This adds a unexportedglobal to golangci-lint.
unexportedglobal is a linter that requires that unexported global variables and constants are prefixed with '_'.
The idea is to eliminate the risk of conflict between names of local variables and names of global variables.
The linter is inspired by the Prefix Unexported Globals with
_guidance in Uber's Go Style Guide.The linter is not enabled by default.
I have also not added it to the 'style' preset because not everyone may agree with this change.