Definition of the term
In fact this is true for any language. Are numbers with no clear meaning, numbers that seem to have been taken out of the hat.
Assuming that a variable is just a name for a value, the magic numbers are just unnamed "constants". When we give them a name, they cease to be magicians. In general we prefer that they be placed in constants. Nothing prevents them from being variables that should not actually vary during execution. There are languages that use text substitution only (change name by number).
At least this is one of the definitions of the term. In the context presented is this.
IDE acting
Of course, for an IDE it’s hard to say that, it depends on context. This particular one you’re using thinks it can’t have something like that. There must be a "good practice" rule attached to it to make you do everything the way he thinks is right.
Validity of the technique
There are cases where it is good to create a constant and use it. It better documents the intention of that number (it gives more semantics), after all comments should be avoided anyway, and mostly achieves the DRY, that is, to put information in one place in a canonical way, so a necessary change would require the exchange of one place to reflect on all and avoid unbalancing the application.
See examples in other responses of how this might be necessary. Other:
const maxLenghtPassword = 8;
...
if (texto.Lenght < maxLenghtPassword) { //melhor que por o 8 aqui, já que ele pode mudar
There are numbers that are not so magical and it makes sense to use naturally. This happens a lot with 0 (of course the problem is more of context and not of the digit itself). Zero is often used as the low limit of something and will never change. The same goes for 1 and up to -1. But then it depends on how the person likes to work.
Would you use 7 as the number of days a week (never changes) or create a constant, I don’t know, const DaysInAWeek = 7
to represent this and use the constant whenever you need this information? Whatever, be consistent.
Everyone knows the value of PI and it is constant, it will never change. This constant exists. Of course in his case it is something that not everyone will remember. And even if a person sees one 3.14159
and know what it is, it is more documented that the intention is to use PI. For that same reason someone would use DaysInAWeek
.
Then there are those who think that everything should be constant and nothing should be direct number. I like the constants, but not exaggeration, everything depends on the case. You have to be careful not to create obvious, like const abreParentese = "("
(does not need to be number even). Is there gain in doing this? It may be in some case, but doubt. There is gain in doing const zero = 0
?. It is not to describe the content of the variable, but to give meaning to it. I think it does not look good like this.
It has the disadvantage that if you want to know the concrete number even, you have to look for it. Some Ides can easily show you, but it’s not there in the text.
In the past it was recommended to declare everything in upper case to differentiate. Today this makes less sense, but it depends on language. Today an IDE can highlight that it is a constant and not a variable.
Memory performance and consumption
In some languages this can be done without taking up any variable space and without causing indirect, Everything is solved at compile time and the artifice is restricted for better readability of the code. Has language that causes costs to code, but nothing exaggerated.
Magic texts
Of course it doesn’t have to be just numbers, it can be texts, or even other things that are literal to them and can be created at compile time.
Why not
var total=...
@rray?– Miguel
@Miguel pq I forgot the var hahaha xD
– rray
ha lol, I thought it had to do with the problem
– Miguel