DEV Community

Cover image for The Sleep-Deprived Senior Test: Why I Hate Your "Magic Numbers"
Doogal Simpson
Doogal Simpson

Posted on • Originally published at Medium

The Sleep-Deprived Senior Test: Why I Hate Your "Magic Numbers"

It is 3:14 AM.

Your phone is buzzing on the nightstand. The on-call alert is screaming that the production server is on fire.

You stumble out of bed, open your laptop, and squint at the screen with one eye open. You are groggy. You are panicked. You are not thinking about Big O notation or the history of C++. You just want to stop the bleeding and go back to sleep.

You open the file causing the crash and see this:

if (user.status === 2 && (now - user.ts) > 86400) {
  terminate(user);
}
Enter fullscreen mode Exit fullscreen mode

You have exactly three seconds to figure out what this does.

What is 2? Is it an error code? An admin level? A suspended account?
What is 86400? Is it a buffer size? A memory limit? A timeout in milliseconds?

Because you used raw numbers, I now have to scroll up 50 lines or search through three other files just to understand your logic.

This is the Sleep-Deprived Senior Test. And you just failed it.

Magic Numbers are "Context Vampires"

In the industry, we call these raw values Magic Numbers. They are dangerous.

A Magic Number is a raw value sitting in the middle of your logic without explanation. I call them Context Vampires because they suck the meaning right out of your code.

As a Junior, you fall into the trap of thinking, "I know what 86400 is. It's the number of seconds in a day. It’s obvious."

It is not obvious. Not at 3 AM. Not to the new hire. Not even to Future You six months from now.

When you write code, you are writing for two audiences:

  1. The Computer: It doesn't care. It loves numbers.
  2. The Human: We are bad at math and need context.

Your job isn't to make the math work. Your job is to make the intent clear.

The Code: Turning Puzzles into Sentences

Let’s look at how we fix the "Nightmare at 3 AM" scenario.

// Before: The Mystery Box

This code forces the developer to do mental math and memorize arbitrary status codes.

// Why 86400? Why 2? 
// The developer has to do math in their head while the server burns.
if (user.status === 2 && (now - user.ts) > 86400) {
  terminate(user);
}
Enter fullscreen mode Exit fullscreen mode

// After: The Professional Junior

We extract those values into named constants. We trade a few seconds of typing for hours of future clarity.

const STATUS_SUSPENDED = 2;
const SECONDS_IN_DAY = 86400;

const isSuspended = user.status === STATUS_SUSPENDED;
const isExpired = (now - user.timestamp) > SECONDS_IN_DAY;

// Now we read the INTENT, not the math.
if (isSuspended && isExpired) {
  terminateAccount(user);
}
Enter fullscreen mode Exit fullscreen mode

The Pro Move

Do you see the difference?

In the first example, I have to decode the Matrix.
In the second example, I am reading a sentence.

If the user is suspended and the time is expired, terminate the account.

The compiler produces the exact same binary for both snippets. But the second snippet prevents bugs, saves time, and most importantly, lets your Senior developer go back to sleep faster.

Don’t be a vampire. Name your constants.


Stop writing code just to please the compiler.

This article was an excerpt from my handbook, "The Professional Junior: Writing Code that Matters."

It’s not a 400-page textbook. It’s a tactical field guide to unwritten engineering rules.

👉 Get the Full Handbook Here


Top comments (0)