DEV Community

Cover image for Stop waving the wand of magic numbers

Stop waving the wand of magic numbers

edA‑qa mort‑ora‑y on February 25, 2019

37. You have no idea what that number is, do you? A number without context nor a label is a random value. It doesn't tell us anything. Imagine walk...
Collapse
 
drbearhands profile image
DrBearhands

Story from a friend.

Code was outsourced offshore, because that would save money (it didn't). The company had a rule, and tooling I expect, against magic numbers. So the creative little buggers did this:

int seven = 7;

[...]

for (int ii = 0; ii < seven; ii++)
Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

That's not creative, that's "clever".

I frown on clever coding.

I wonder if other variables were named iii, iv, v, etc?

Collapse
 
drbearhands profile image
DrBearhands

the ii name was my own addition. I was under the impression this was fairly common for indexed array iteration.

Thread Thread
 
mortoray profile image
edA‑qa mort‑ora‑y

I don't think I've seen it before. I might have blocked out the memory. If I need a generic variable I just use i, or j, but I prefer giving them better names when possible, like row, or x, or item_ndx.

Thread Thread
 
drbearhands profile image
DrBearhands

Oh, ii is often used rather than i for iterators because it's easier to text search. Then again if you have to search for the iterator you might be doing something wrong.

Thread Thread
 
mortoray profile image
edA‑qa mort‑ora‑y

I don't think I've ever searched for an iterator variable.

Collapse
 
jenc profile image
Jen Chan

Is that ... a spread operator?

Thread Thread
 
drbearhands profile image
DrBearhands

nah, [...] is often used to indicate that some part was cut from a quote. I guess it doesn't translate well to code blocks.

Thread Thread
 
mortoray profile image
edA‑qa mort‑ora‑y

I often do three vertical dots for this:

trailing_code
.
.
.
next_important_code
Collapse
 
itr13 profile image
Mikael Klages

If a magic number is hard to name, then a comment explaining how it works might be needed too.

And if there's a true magic number that you don't know why works, then you probably shouldn't have it at all.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

I'm okay with a comment on the constant, but not where it is used. Sometimes numbers come from external calculations that are not done in the code. This is common for many formulas in math, science, finance, and graphics.

I've had comments before referring to the code used to calculate the constant. I did this when it was too costly to calculate each time.

Collapse
 
itr13 profile image
Mikael Klages

One feature I can't wait for more languages to implement, is compile time code execution. Then such costly functions could be visibly coded, but also not have to run each time the program is opened.

Thread Thread
 
mortoray profile image
edA‑qa mort‑ora‑y

Does anything other than C++ offer this now?

I've usually resorted to a pre-build step that modifies source files.

Thread Thread
 
itr13 profile image
Mikael Klages

I think I read that scala has it? Personally I'm pretty excited for JAI (though that might not be released for a few years), but that may be mostly suited for game-development rather than general programming and software.

Collapse
 
craigmc08 profile image
Craig McIlwrath

Whenever I hear magic number I always think of the quake fast inverse square root. en.m.wikipedia.org/wiki/Fast_inver...

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

That was an awesome read.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

For square root fun, also see my article Calculating square root using Newton’s iterative method

Collapse
 
gmartigny profile image
Guillaume Martigny

To read more on the subject, you could to the eslint page about magic numbers.
It's written for Javascript, but you should consider this rule for any language.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

I'm not so happy with their detectObjects rule. I like grouping my constants into objects, but they seem to encourage making them all static globals. I guess it's configurable though.

Collapse
 
gmartigny profile image
Guillaume Martigny

The default value for "detectObjects" seems to be false, meaning it will allow you to put magic numbers inside objects (like you do). You don't have to activate the rule, and I think constants under "enums" is a great thing.

Collapse
 
jenc profile image
Jen Chan • Edited

Omg! Second time coming across a counting cards question...
And started thinking I ought to know card games.

I think the takeaway here is simplifying a problem and making semantic vars.

But if I understand the problem clearly I can apply a set of rules without thinking about card game rules per se, but the interviewer would need to supply the back stories such as suits and how one wins...

Collapse
 
jrtibbetts profile image
Jason R Tibbetts

37

"In a row?"

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

Absolutely. It was amazing.

Collapse
 
evalenzuela profile image
evalenzuela

Magic numbers are pure evil! :D. If you use them and revisit your code in 3 months, you won't remember what were they for.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y