Magic Number Vs. Symbolic Constant: When to replace?
Magic: Unknown semantic
Symbolic Constant -> Provides both correct semantic and correct context for use
Semantic: The meaning or purpose of a thing.
"Create a constant, name it after the meaning, and replace the number with it." -- Martin Fowler
First, magic numbers are not just numbers. Any basic value can be "magic". Basic values are manifest entities such as integers, reals, doubles, floats, dates, strings, booleans, characters, and so on. The issue is not the data type, but the "magic" aspect of the value as it appears in our code text.
What do we mean by "magic"? To be precise: By "magic", we intend to point to the semantics (meaning or purpose) of the value in the context of our code; that it is unknown, unknowable, unclear, or confusing. This is the notion of "magic". A basic value is not magic when its semantic meaning or purpose-of-being-there is quickly and easily known, clear, and understood (not confusing) from the surround context without special helper words (e.g. symbolic constant).
Therefore, we identify magic numbers by measuring the ability of a code reader to know, be clear, and understand the meaning and purpose of a basic value from its surrounding context. The less known, less clear, and more confused the reader is, the more "magic" the basic value is.
Helpful Definitions
- confuse: cause (someone) to become bewildered or perplexed.
- bewildered: cause (someone) to become perplexed and confused.
- perplexed: completely baffled; very puzzled.
- baffled: totally bewilder or perplex.
- puzzled: unable to understand; perplexed.
- understand: perceive the intended meaning of (words, a language, or speaker).
- meaning: what is meant by a word, text, concept, or action.
- meant: intend to convey, indicate, or refer to (a particular thing or notion); signify.
- signify: be an indication of.
- indication: a sign or piece of information that indicates something.
- indicate: point out; show.
- sign: an object, quality, or event whose presence or occurrence indicates the probable presence or occurrence of something else.
Basics
We have two scenarios for our magic basic values. Only the second is of primary importance for programmers and code:
- A lone basic value (e.g. number) from which its meaning is unknown, unknowable, unclear or confusing.
- A basic value (e.g. number) in context, but its meaning remains unknown, unknowable, unclear or confusing.
An overarching dependency of "magic" is how the lone basic value (e.g. number) has no commonly known semantic (like Pi), but has a locally known semantic (e.g. your program), which is not entirely clear from context or could be abused in good or bad context(s).
The semantics of most programming languages will not allow us to use lone basic values, except (perhaps) as data (i.e. tables of data). When we encounter "magic numbers", we generally do so in a context. Therefore, the answer to
"Do I replace this magic number with a symbolic constant?"
is:
"How quickly can you assess and understand the semantic meaning of the
number (its purpose for being there) in its context?"
Kind of Magic, but not quite
With this thought in mind, we can quickly see how a number like Pi (3.14159) is not a "magic number" when placed in proper context (e.g. 2 x 3.14159 x radius or 2*Pi*r). Here, the number 3.14159 is mentally recognized Pi without the symbolic constant identifier.
Still, we generally replace 3.14159 with a symbolic constant identifier like Pi because of the length and complexity of the number. The aspects of length and complexity of Pi (coupled with a need for accuracy) usually means the symbolic identifier or constant is less prone to error. Recognition of "Pi" as a name is a simply a convenient bonus, but is not the primary reason for having the constant.
Meanwhile: Back at the Ranch
Laying aside common constants like Pi, let's focus primarily on numbers with special meanings, but which those meanings are constrained to the universe of our software system. Such a number might be "2" (as a basic integer value).
If I use the number 2 by itself, my first question might be: What does "2" mean? The meaning of "2" by itself is unknown and unknowable without context, leaving its use unclear and confusing. Even though having just "2" in our software will not happen because of language semantics, we do want to see that "2" by itself carries no special semantics or obvious purpose being alone.
Let's put our lone "2" in a context of: padding := 2
, where the context is a "GUI Container". In this context the meaning of 2 (as pixels or other graphical unit) offers us a quick guess of its semantics (meaning and purpose). We might stop here and say that 2 is okay in this context and there is nothing else we need to know. However, perhaps in our software universe this is not the whole story. There is more to it, but "padding = 2" as a context cannot reveal it.
Let's further pretend that 2 as pixel padding in our program is of the "default_padding" variety throughout our system. Therefore, writing the instruction padding = 2
is not good enough. The notion of "default" is not revealed. Only when I write: padding = default_padding
as a context and then elsewhere: default_padding = 2
do I fully realize a better and fuller meaning (semantic and purpose) of 2 in our system.
The example above is pretty good because "2" by itself could be anything. Only when we limit the range and domain of understanding to "my program" where 2 is the default_padding
in the GUI UX parts of "my program", do we finally make sense of "2" in its proper context. Here "2" is a "magic" number, which is factored out to a symbolic constant default_padding
within the context of the GUI UX of "my program" in order to make it use as default_padding
quickly understood in the greater context of the enclosing code.
Thus, any basic value, whose meaning (semantic and purpose) cannot be sufficiently and quickly understood is a good candidate for a symbolic constant in the place of the basic value (e.g. magic number).
Going Further
Numbers on a scale might have semantics as well. For example, pretend we are making a D&D game, where we have the notion of a monster. Our monster object has a feature called life_force
, which is an integer. The numbers have meanings that are not knowable or clear without words to supply meaning. Thus, we begin by arbitrarily saying:
- full_life_force: INTEGER = 10 -- Very alive (and unhurt)
- minimum_life_force: INTEGER = 1 -- Barely alive (very hurt)
- dead: INTEGER = 0 -- Dead
- undead: INTEGER = -1 -- Min undead (almost dead)
- zombie: INTEGER = -10 -- Max undead (very undead)
From the symbolic constants above, we start to get a mental picture of the aliveness, deadness, and "undeadness" (and possible ramifications or consequences) for our monsters in our D&D game. Without these words (symbolic constants), we are left with just the numbers ranging from -10 .. 10
. Just the range without the words leaves us in a place of possibly great confusion and potentially with errors in our game if different parts of the game have dependencies on what that range of numbers means to various operations like attack_elves
or seek_magic_healing_potion
.
Therefore, when searching for and considering replacement of "magic numbers" we want to ask very purpose-filled questions about the numbers within the context of our software and even how the numbers interact semantically with each other.
Conclusion
Let's review what questions we ought to ask:
You might have a magic number if ...
- Can the basic value have a special meaning or purpose in your softwares universe?
- Can the special meaning or purpose likely be unknown, unknowable, unclear, or confusing, even in its proper context?
- Can a proper basic value be improperly used with bad consequences in the wrong context?
- Can an improper basic value be properly used with bad consequences in the right context?
- Does the basic value have a semantic or purpose relationships with other basic values in specific contexts?
- Can a basic value exist in more than one place in our code with different semantics in each, thereby causing our reader a confusion?
Examine stand-alone manifest constant basic values in your code text. Ask each question slowly and thoughtfully about each instance of such a value. Consider the strength of your answer. Many times, the answer is not black and white, but has shades of misunderstood meaning and purpose, speed of learning, and speed of comprehension. There is also a need to see how it connects to the software machine around it.
In the end, the answer to replacement is answer the measure (in your mind) of the strength or weakness of the reader to make the connection (e.g. "get it"). The more quickly they understand meaning and purpose, the less "magic" you have.
CONCLUSION: Replace basic values with symbolic constants only when the magic is large enough to cause difficult to detect bugs arising from confusions.