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 ignore calculations

See original GitHub issue

I 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:closed
  • Created 8 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
IanVScommented, Oct 22, 2015

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.

const millisecondsPerDay = 86400000; // 24 * 60 * 60 * 1000
0reactions
keithamuscommented, Feb 23, 2016

I would request this rule for instances where a simple calculation is needed. Take for example the following code:

const myArray = [1,2,3];
const lastIndex = myArray.length - 1;

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:

const myArray = [1,2,3];
const lastIndexLengthDelta = 1;
const lastIndex = myArray.length - lastIndexLengthDelta;

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.

/* eslint no-magic-numbers: 2 { "numbersAllowedInCalculations": 0 } */
const lastIndex = myArray.length - 1; // errors

/* eslint no-magic-numbers: 2 { "numbersAllowedInCalculations": 1 } */
const lastIndex = myArray.length - 1; // does not error
Read more comments on GitHub >

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

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