A “magic number” is a number used by itself in code, with no context. It is a lot less fun than its whimsical name would have you believe. Ultimately, a magic number in your code will create legacy headaches for future developers, who will be all like “What does this 5 mean?!?”
Looking back at my first project (a recommendation engine for blog posts based on writing style), I used a few magic numbers. For example:
# retrieves standard deviation of all post feature vectors in database std_dev = model.calculate_std_dev() + 0.001
Past Mica, what is that 0.001 for? Who even knows! Its meaning is lost to the annals of time.
Getting my first code review comment
no magic numbers, plz was a small but significant step in learning to write software and work as part of a larger engineering organization. Because if you just go about throwing magic numbers around, you (and probably other people) will hate you for it later.
If I were to re-write the above line, I would now have a global variable or, better yet, a constants file for the mysterious 0.001:
NORMALIZATION_VAR = 0.001 std_dev = model.calculate_std_dev() + NORMALIZATION_VAR
Sure, this is probably self-explanatory, but it was one of the first things I learned as a software engineer. And I want to document it here.
I do what I want!