Developer

Live from LA: Tales of the magic number

Do you remember the days before the enum, when magic numbers ruled the developer world? In this edition of Live from LA, Lamont Adams revisits that mystical age.


A friend of mine who is preparing for the MCAD exam recently noticed how heavily the .NET Framework relies on enumerated types, also known as enums. Wearing a puzzled look on his face, he knocked on my half-open office door the other day and asked, “What’s the big deal with enums?” I proceeded to tell my perplexed friend a story that went something like this.

The age of magic numbers
You haven’t been around long enough to remember the early days of programming, but back then, developers lived in a mystical world filled with magic numbers. These magic numbers had special powers for certain developers who knew how to use them. For instance, many VB developers, like myself, often called upon the magic in the numbers 13 and 10, as these were part of the recipe handed down by the ASCII gods for the new-line and carriage return combination. Toss equal amounts of each into a string, perform a bit of ASCII alchemy, and you get a line break in that string—a useful trick when you output a lot of text files.

There were, of course, other magic numbers for us, like –1, which held the power of unshakable truth. Still other numbers had more fleeting and specific powers, and these powers often changed depending on the environment in which they were used. The number 37, for instance, might be particularly powerful if you were working with a document that held 37 lines of text per page. In other cases though, 37 was the numeric equivalent of Harry Potter’s magic-less muggles. Indeed, part of the magic of these numbers was that you never really could tell what their powers were until you used them.

Enums: The end of the magic
Eventually, in the interests of code that was more readily understood, tool and compiler vendors supplied us with newer versions of our languages that minimized the importance of magic numbers. These new versions included constants that duplicated the powers of the magic numbers most programmers had depended on.

For myself, this change came about sometime around the release of VB4, if I recall correctly, when my friends 13 and 10 were replaced by vbCrLf. Although I was saddened by their departure, I had bought into the arguments of the anti-magic-number self-documenters, who claimed that less reliance on magic numbers made for more readable code. Sometime later, the folks at Microsoft presented me with the power of enumerated types, now colloquially known as enums, which promised to replace magic numbers entirely and thoroughly converted me to a self-documenter.

There were, of course, those who wanted to continue their mystical work with magic numbers. Many belonged to a sect of programmers practicing a dark art known as “Win32 API.” These folk sprinkled their code liberally with strange magical numbers of great importance to their craft. Chief among these were the numbers 0 and 1, the twin indicators of stirring success and utter, crushing, failure.

Friction quickly grew between the mystic factions and their self-documenter rivals. Eventually, the Great Enum War broke out, as the self-documenters attempted to force the magic number devotees to repent their mystic ways and acknowledge the single truth of self-documenting code. Many developers, disillusioned by the bickering, escaped to the land of management, while others left for the strange land of Assembler, where rumor has it they can still be found today.

The aftermath
Nowadays, there aren’t many magic-number developers left—at least not many who will admit to it, for fear of reprisals. Occasionally, younger, undisciplined developers will flirt with the art early in their careers, and older developers, when pressed for time, may occasionally sprinkle a few magic numbers into their code. Mostly though, particularly with the .NET Framework providing enums for most every value a developer could possibly need, magic numbers are a lost art.

Still, there are wandering adventurers and consultants who will tell you tales of wild lands where the magic numbers still exist and incomprehensible source code abounds. Most people dismiss these tales as just legend, but I’m sometimes tempted to believe….

Where do you fit in?
Are you a mystic developer or a self-documenter? Declare your allegiance in our discussion board below.

 

Editor's Picks