What are "Magic Numbers"?

Asked

Viewed 866 times

21

My IDE informed that I am using magic numbers in my code, in the stretch f = 12.

I’d like to know more about "magic numbers". I need to have a variable with a value of 12, but the IDE said something about Javascript constants.

I must worry about these Magic Numbers long-term?

3 answers

25


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.

14

Magic numbers are numbers scattered in blocks of codes the two major problems are little semantics (information about use) and the possibility of this number being spread over N lines, which means N changes in different places.

The solution is to transform this number into a 'reference' can be to transform it into a constant or variable and/or create a single point of change (creation of a method for the activity).

Magic number example

var intD = 86400 * 30

Possible solution

var segundosDeUmDia = 86400;
var outroValor = 30;
var total = segundosDeUmDia * outroValor;
  • Why not var total=... @rray?

  • 1

    @Miguel pq I forgot the var hahaha xD

  • ha lol, I thought it had to do with the problem

13

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

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

You could tell me what’s going on?

Of course not, because you’d have to search the entire code until you find out what these numbers mean.

If you define constantes:

const EXPORTACAO = 12;
const PREMIUM = 2;

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

Things get much simpler.

Note: There’s a really cool video of Rafael Ponte about it here Tip #2 - Magic Numbers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.