

What is a magic number, and why is it bad? - carlosgg
http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad

======
bicx
I just have a Numbers class I import into all my projects:

public final static int ZERO = 0; public final static int ONE = 1; public
final static int TWO = 2; public final static int THREE = 3; public final
static int FOUR = 4; ....

(jk)

~~~
arethuza
I've seen a project that defined string constants like:

    
    
       public static String HTTP = "http";
       public static String COLON = ":";
       public static String SLASH = "/";
    

with code that looked like:

    
    
       url = HTTP + COLON + SLASH + SLASH + ....
    

[NB I am NOT joking]

~~~
tzs
It can be fun with code like that to see if you can imagine situations where
you could argue with a straight face that the code is OK.

    
    
       public static String HTTP = "http";
    

An argument for this might be that it allows you to easily change to https by
changing that one line, although then the name of the string would be
misleading.

    
    
       public static String COLON = ":";
       public static String SLASH = "/";
    

These are harder. Maybe you don't want ":" and "/" to appear literally because
for some reason you grep for ":" and "/" a lot looking for instances that are
syntactically significant in your language and you got tired of hitting those
characters in URL strings.

All of these potential excuses fall, though, to the objection that all three
of those could have been rolled into a single string, with a sensible name
like URL_COMMON_PREFIX:

    
    
       public static String URL_COMMON_PREFIX = "http://";

------
radio4fan
The primary purpose of the DATA statement is to give names to constants;
instead of referring to pi as 3.141592653589793 at every appearance, the
variable PI can be given that value with a DATA statement and used instead of
the longer form of the constant.

This also simplifies modifying the program, should the value of pi change.

\-- Early FORTRAN manual for Xerox Computers

------
dllthomas
No one seems to be mentioning another use of the phrase "magic number" which
isn't bad at all - numbers put in files to mark the file type. 0xCAFEBABE in
java class files, for instance.

~~~
greenyoda
Unix even has "man 5 magic", which describes the format of the magic numbers
file that the "file" command uses to determine the type of a file (executable,
object code, shell script, etc.) from its first few characters.

My personal favorite magic numbers are the values that debuggers use to fill
uninitialized memory, like 0xDEADBEEF, 0xBAADF00D, etc.

Wikipedia has a good article on magic numbers:

[https://en.wikipedia.org/wiki/Magic_number_(programming)](https://en.wikipedia.org/wiki/Magic_number_\(programming\))

------
bluedino
That is about the worst top-rated answer I've seen in a while on SO.

~~~
simias
Agreed.

In my opinion there are two kinds of magic numbers:

* the first kind is the one most answers describe: an arbitrary number used in various places that may change at any moment. The size of a buffer for instance.

* the second kind are numbers that represent something very specific but not immediately obvious when reading the code: for instance in a device driver "read_register(base + DEVICE_STATUS)" is much clearer than "read_register(base + 0x3c)". In this case naming the magic number is useful even if it's used only once in the code. It acts as a comment basically.

I'd also add that sometimes adding too many levels of indirection is a bad
thing, and in the (currently) top voted answer I prefer the original over the
"fixed" version. Either you do the right thing and make MAX_PASSWORD_SIZE
something meaningful or you might as well write "if (password.length() > 7)".
It's clear, non ambiguous and (in the example) not reused anywhere. Defining a
macro for that just hides poor code and forces me to go look for the
definition in order to understand what's going on (while debugging for
instance).

------
nicholassmith
I'd have personally thought the number used in the fast inverse square root
([http://en.wikipedia.org/wiki/Fast_inverse_square_root](http://en.wikipedia.org/wiki/Fast_inverse_square_root))
would qualify as a 'magic number', everything else is just a hardcoded value.

------
timje1
I find these a lot with CSS - banishing them seem to be a major goal of LESS,
SASS etc.

They also seem to crop up too often in JS / JQuery, particularly the
manipulation of CSS.. where do you folks tend to find them?

------
dsego
Also, it can be a good idea to yank out all of those into a separate config
file. Although, it destroys locality, so it depends on the use case I guess.

~~~
qu4z-2
I wouldn't move it into a config file unless you're also testing the code
works correctly with different values. Otherwise it's just asking for trouble.

------
prezjordan
New term added to my vocabulary - thanks for sharing!

------
tzs
I just call any constants that slip into my code "advanced technology numbers"
if someone gives me a hard time about them.

------
a3voices
Caring about magic numbers is a distraction from real problems, such as
getting your software working.

~~~
timje1
Once the software's working, and has been for several years, and it has become
unmaintainable because lax guidelines let this stuff through, this sort of
thing becomes your _real problem_.

~~~
xyzzy123
Nah, ubiquitous session re-use, bad APIs, arbitrary data structures (e.g
everything in complex sets of nested arrays), hardening of code against static
analysis (e.g. database lookups to instantiate classes at runtime) rate way
higher on my scale.

Local use of magic numbers (across 1 file) don't bother me any more, although
I will recommend fixing them when I find them.

