no-magic-numbers ignore calculations
See original GitHub issueI have a const like const oneDay = 1000 * 60 * 60 * 24;
With "no-magic-numbers": [2, { ignore: [1000] }]"
No magic number: 60 [no-magic-numbers] const oneDay = 1000 * 60 * 60 * 24;`
The point of doing the oneDay
was to not have a magic number/calculation, would it be possible to add an option to ignore magic numbers that are directly behind an assignment?
Issue Analytics
- State:
- Created 8 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
no-magic-numbers - ESLint - Pluggable JavaScript Linter
'Magic numbers' are numbers that occur multiple times in code without an explicit meaning. They should preferably be replaced by named constants.
Read more >eslint no-magic-numbers rule can't ignore checking number ...
I'm new to eslint. With "no-magic-numbers" rule turned on, declare an array with number literal can't be ignored.eg. let array = [1, 2,...
Read more >no-magic-numbers - Rule
Disallows the use constant number values outside of variable assignments. When no list of allowed values is specified, -1, 0 and 1 are...
Read more >Eliminating Magic Numbers: When is it time to say "No"?
For magic numbers that appear exactly once, and are independent of the rest ... Ultimately, the point is readability, not saving 2 cycles...
Read more >Decoding the Magic Number: Everyone Can do it! - PMC - NCBI
Thus, there is no magic number, but the estimation has to be arrived at by following ... whether the online calculator is providing...
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
The number of milliseconds in a day is a constant (ignoring leap seconds), so just create it as a constant. Why do you need to make the computer do your math for you? You can explain with a comment if you really feel the need.
I would request this rule for instances where a simple calculation is needed. Take for example the following code:
This const uses 1 number as part of the calculation, and should be allowed to be passed through - because the number on its own has no semantic value, and trying to give it a useful name becomes a struggle, e.g:
I’d argue the first example, while not attaching a semantic value to the number, is more readable than the 2nd. Thoughts?
BTW to better clarify my stance: I get that I can just whitelist the number
1
- but I think it would be even better have the option of a threshold of numbers used in a calculation. E.g.