Change no-magic-numbers to support default argument values
See original GitHub issueWhat rule do you want to change?
no-magic-numbers
Does this change cause the rule to produce more or fewer warnings?
fewer
How will the change be implemented? (New option, new default behavior, etc.)?
new default behavior
, or new option
if needed, but I don’t believe it would be.
Please provide some example code that this change will affect:
function format(ms, precision = 2) {
// do stuff
}
What does the rule currently do for this code?
It considers the 2
a magic number, even though it has an explicit meaning.
What will the rule do after it’s changed?
It would treat this code as valid.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Magic number as default method parameter value
Declare the default value beforehand, so as to (hopefully) make it more clear what the number represents:
Read more >no-magic-numbers - TypeScript ESLint
This rule extends the base eslint/no-magic-numbers rule. It adds support for: numeric literal types ( type T = 1 ),; enum members (...
Read more >no-magic-numbers - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >Eliminating Magic Numbers: When is it time to say "No"?
Any numbers that can reasonably change or have no obvious meaning should be put into variables. Which means pretty much all of them....
Read more >Replace Magic Number with Symbolic Constant
A magic number is a numeric value that's encountered in the source but has no obvious meaning. This “anti-pattern” makes it harder to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is still a magic number. The program should be written like
to give context-free meaning to
2
instead of just theformat
-context sensitive meaningprecision
.I continue to believe we should not add options that should be avoided in order to follow the either the spirit of or the initial justification for the rule.
I’d slightly prefer that this be an option, so that users can choose either way. Otherwise 👍 from me.