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.

no-magic-numbers should allow passing magic numbers to specific functions like setTimeout

See original GitHub issue

I propose a new option for the no-magic-numbers rule:

{
    "ignoreParamsForFunctions": [
        "setTimeout",
        "setYear"
    ]
}

In this case the following code would be valid:

setTimeout(function () {
    (new Date()).setYear(1984)
}, 500)

In contrary to https://github.com/eslint/eslint/issues/4236 this option would only target specific functions. There are probably not many functions where magic numbers are ok but in my case I just want to omit // eslint-disable-line no-magic-numbers which I currently have to add every single time I use setTimeout.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:6
  • Comments:16 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
aubergine10commented, Jun 16, 2016

Code that does animations is another example of where it’s often difficult to find meaningful descriptions for numbers, and trying to have a named variable actually makes the code incredibly confusing. In many situations, the number is derived by trial and error to get a value that “feels right” rather than some thoughtful, describable process. In such scenarios, the magic number is actually the most descriptive way to convey what the number is: a magic number. Also, in these scenarios, the main focus of someone reading the code is not what the numbers mean (it’s pretty obvious due to context) but rather being able to clearly see the flow of the animation code and reason about its intent; a load of wordy number constants can actually make such code very difficult to read.

Being able to define some exceptions to the rule like suggested in OP would be beneficial in such scenarios.

2reactions
ilyavolodincommented, Jun 16, 2016

I can sort of understand setYear example that you provided. Even with that I’m having a trouble convincing myself it’s not a magic number, because while it’s clear that 1984 is a year, it’s not clear why you chose 1984 and not 1882. However, why do you think 500 is not a magic number? The reason for setting timeout to 500ms is completely magical to me. Why not 502?

This rule is very strict and hard to follow 100%, but that’s exactly it’s intention. It exists to create self-documenting code. And self-documenting code requires explanation for each number you choose.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-magic-numbers - ESLint - Pluggable JavaScript Linter
The no-magic-numbers rule aims to make code more readable and refactoring easier by ensuring that special numbers are declared as constants to make...
Read more >
TypeScript static code analysis: Magic numbers should not be ...
That is why magic numbers must be demystified by first being assigned to clearly named variables before being used. -1, 0 and 1...
Read more >
What is a magic number, and why is it bad? - Stack Overflow
First, magic numbers are not just numbers. Any basic value can be "magic". Basic values are manifest entities such as integers, reals, doubles,...
Read more >
Does context (like as an argument in a function) allow for ...
It seemed like no one has considered context as a means of making it acceptable to use a number and not have it...
Read more >
no-magic-numbers - Rule
Magic numbers should be avoided as they often lack documentation. Forcing them to be stored in variables gives them implicit documentation. Config. Options...
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