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

Suggest replacing variables that have constant values with actual constants #851

Open
dominikh opened this issue Oct 17, 2020 · 5 comments
Labels
needs-decision We have to decide if this check is feasible and desirable new-check

Comments

@dominikh
Copy link
Owner

var magicNumber = 42
use(magicNumber)

could use a const instead. Using a real constant would signal to people and tools alike that the value of the variable intentionally never changes.

Labelled as needs-decision for now because I'm not sure how much people appreciate "technically better" suggestions.

@dominikh dominikh added needs-decision We have to decide if this check is feasible and desirable new-check labels Oct 17, 2020
@cespare
Copy link

cespare commented Oct 17, 2020

On the surface it sounds good. How hard is it to implement? I'd be interested in running it over my code if you have some prototype.

@dominikh
Copy link
Owner Author

@cespare should be easy enough to implement, and I'll definitely create a prototype before deciding on this issue. I'll let you know when there's something to test (but that will still be a while.)

@ainar-g
Copy link
Contributor

ainar-g commented Oct 17, 2020

As long as the check will take into account const vs. var quirks, like the ones in #833 and golang/go#28591, I'm all for it.

@dominikh
Copy link
Owner Author

Another quirk to be mindful of: negative zero floating point.

@Jacalz
Copy link

Jacalz commented Aug 5, 2022

I think this is a great idea. Recently, I have come across quite a few cases where values are declared as variables even though they ideally should be constants. All my cases have been global variables but I suppose that it could be useful for function-local variables as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-decision We have to decide if this check is feasible and desirable new-check
Projects
None yet
Development

No branches or pull requests

4 participants