What are "Magic Numbers" in JavaScript?

13

My IDE reported that I'm using magic numbers in my code, in the f = 12 stretch.

I'd like to know more about what "magic numbers" are. I need to have a variable of value 12, but the IDE has said something about constants in JavaScript.

Should I worry about these magic numbers in the long run?

    
asked by anonymous 15.06.2016 / 15:47

3 answers

18

Definition of the term

In fact this holds true for any language. They are numbers with no clear meaning , numbers that appear to have been taken off the top.

Assuming that a variable is just a name for a value, the magic numbers are just unnamed "constant" numbers. When we give them a name, they stop being magical. 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.

At least this is one of the definitions of the term. In the context presented this is it.

IDE acting

Of course, for an IDE it's hard to say, it depends on context. This specific guy you're using thinks he can not have anything like that. You should have a "good practice" rule attached to it to make you do everything the way it feels right.

Validity of the technique

It has cases that are good to even create a constant and use it. It better documents the intent of that number (it gives more semantics), after all comments should be avoided anyway, and mainly reaches the DRY , that is, put in one place the information in canonical form, so a necessary change would require the exchange from one place to reflect on everyone and avoid unbalancing the application.

See examples in the other answers for how this may be necessary. Other:

const maxLenghtPassword = 8;
...
if (texto.Lenght < maxLenghtPassword) { //melhor que por o 8 aqui, já que ele pode mudar

It has a number that is not so magical so it even makes sense to use it 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 here it depends on how the person likes to work.

Would you use 7 as number of days of the week (never change) or would you create a constant, I do not know, const DaysInAWeek = 7 to represent this and use the constant whenever you need this information? Whatever it is, be consistent.

Everyone knows the value of PI and it is constant forever, it will never change. This constant exists. Of course in his case it's something that not everyone will remember. And even if a person sees a 3.14159 and knows what it is, it is more documented that the intention is to use PI. For this same reason someone would use DayInAWeek .

Then there are those who think that everything should be constant and nothing should be direct number. I like the constants, but I do not exaggerate, it all depends on the case. You have to be careful not to create truisms, something like const abreParentese = "(" (it does not have to be the same number). Is there a gain in doing this? It may even be in some case, but I doubt it. There is gain in doing const zero = 0 ?. It is not to describe the contents of the variable, but to give meaning to it. I do not think so.

It has the disadvantage that if you want to know the same concrete number, 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 capital letters to differentiate. Today it makes less sense, but it depends on language. Today an IDE can highlight that it is a constant and not a variable.

Performance and memory consumption

In some languages this can be done without occupying any variable space and without causing indirection, everything is solved at compile time and the artifice is restricted for better readability of the code. It has language that causes costs for the code. Nothing exaggerated.

Magic Texts

Of course, it does not have to be just numbers, they can be texts, or even other things that literally require for them and can be created at compile time.

    
15.06.2016 / 15:58
11

Imagine that you picked up a maintenance program and see the following line:

if (meuCodigo == 12) {
   varX = 2;
}

Could you tell me what's going on?

Of course not, because you would have to search the whole code until you find out what these numbers mean.

If you set constantes :

const EXPORTACAO = 12;
const PREMIUM = 2;

if (meuCodigo == TIPO_EXPORTACAO) {
   varX = PREMIUM;
}

Things get much simpler.

Note: There is a nice video of Rafael Ponte about this here Tip # 2 - Magic Numbers

    
15.06.2016 / 15:58
11

Magic numbers are numbers scattered in code blocks the two big problems are little semantics (information on usage) and the possibility of that number being spread by N rows, which means N changes in different places.

The solution is to transform this number into a 'reference', either by transforming it into a constant or a variable and / or creating a single point of change (creating a method for the activity)

Magic number example

var intD = 86400 * 30

Possible Solution

var segundosDeUmDia = 86400;
var outroValor = 30;
var total = segundosDeUmDia * outroValor;
    
15.06.2016 / 15:56