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...
For further actions, you may consider blocking this person and/or reporting abuse
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:
That's not creative, that's "clever".
I frown on clever coding.
I wonder if other variables were named
iii
,iv
,v
, etc?the
ii
name was my own addition. I was under the impression this was fairly common for indexed array iteration.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
, orj
, but I prefer giving them better names when possible, likerow
, orx
, oritem_ndx
.Oh,
ii
is often used rather thani
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.I don't think I've ever searched for an iterator variable.
Is that ... a spread operator?
nah, [...] is often used to indicate that some part was cut from a quote. I guess it doesn't translate well to code blocks.
I often do three vertical dots for this:
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.
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.
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.
Does anything other than C++ offer this now?
I've usually resorted to a pre-build step that modifies source files.
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.
Whenever I hear magic number I always think of the quake fast inverse square root. en.m.wikipedia.org/wiki/Fast_inver...
That was an awesome read.
For square root fun, also see my article Calculating square root using Newton’s iterative method
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.
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.
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.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...
"In a row?"
Absolutely. It was amazing.
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.
Sounds like Do you recognize your code 6 months later?