question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change no-magic-numbers to support default argument values

See original GitHub issue

What 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:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
michaelficarracommented, Sep 13, 2018

This is still a magic number. The program should be written like

const IEEE_RECOMMENDED_PRECISION = 2;
const LJNEON_PREFERRED_PRECISION = 3;
const COMPANY_STANDARD_PRECISION = 4;

function format(ms, precision = LJNEON_PREFERRED_PRECISION) {
  // do stuff
}

to give context-free meaning to 2 instead of just the format-context sensitive meaning precision.

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.

1reaction
platinumazurecommented, Aug 9, 2018

I’d slightly prefer that this be an option, so that users can choose either way. Otherwise 👍 from me.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found