Magic Numbers
In programming circles, there's an evil phenomenon known as "magic numbers". This describes the practice of using a number in the program's code whose meaning might not be clear to others. For example, if I wrote:
x = t/(60 * 1440);
you might not be sure what the code did. On the other hand, if I wrote:
SECONDS_PER_MINUTE = 60;
MINUTES_PER_DAY = 1440;
x = t/(SECONDS_PER_MINUTE * MINUTES_PER_DAY);
MINUTES_PER_DAY = 1440;
x = t/(SECONDS_PER_MINUTE * MINUTES_PER_DAY);
it's easy(er) to see that I'm calculating days from seconds. 60 and 1440 were magic numbers that hid the meaning of the code. An additional benefit of programming with named constants (like those found in the second example) is that if for some reason I need to change one of the constants, I only need to change it in one place. So, for example, if by divine decree all minutes are now 61 seconds long (maybe the earth is slowing?) I only need to change it in one place and all my calculations are updated. Nice.
The other day, though, I found a whole new meaning to the phrase "Magic Number" in a computer sense. I use Adium as my instant messaging client. It's an OS X-only program which allows me to talk to users on AIM, MSN, Yahoo, ICQ, Jabber, Bonjour, Google Talk, (etc. etc. etc.) all in the same program. Adium has an emoticon replacement feature, fairly common to such clients that turns :) into an actual smiley. Of course, it doesn't just cover your generic smiley face; it has hundreds of smileys available through various arcane and unexpected character sequences. Occasionally I'll run across one unintentionally, but looking back, I can usually see how the text might be construed as a face of some sort.
I was chatting with Dragon Lady the other day, and she was telling me about some problems she was having at work. She was running a query on a database and getting inconsistent results. She then told me this:
How many records???
I clicked on the smiley to see the underlying text. This is what I found:
Oh. 1414. I see.
If that's not a "magic number", I don't know what is.
P.S. Okay, okay. It's a lame analogy. I know. But it's still what I thought of when I saw it. And if any of you Adium users are curious and want to verify it, just make sure you're using the "Default" emoticon set. I understand that this will be fixed in the 1.3 release, so you have only a short time to enjoy your magic-number-ness. In the meantime, if anyone knows how to get "sarcastic" from 1414, please let me know.
2 comments:
Heh... loved the post! Being a programmer I always love to see little weird things like this because I find them interesting... don't really know why... just do :). Just realized you changed your theme... good choice.
Well written article.
Post a Comment