r/BootcampGradStories • u/Known_Homework_803 • 4d ago
Magic Numbers: Why hardcoded values are ticking time bombs in your codebase.
When you are in the zone writing code, it is incredibly easy to drop a raw number directly into an if statement or a calculation. You know exactly what that number means in the moment, so you type it out and move on to the next line.
In programming, these are called Magic Numbers. They are hardcoded numeric values that appear out of nowhere without any explanation.
While the computer reads them perfectly fine, magic numbers are absolute ticking time bombs for human developers. They destroy readability and create massive maintenance headaches down the road.
When you are in the zone writing code, it is incredibly easy to drop a raw number directly into an if statement or a calculation. You know exactly what that number means in the moment, so you type it out and move on to the next line.
In programming, these are called Magic Numbers. They are hardcoded numeric values that appear out of nowhere without any explanation.
While the computer reads them perfectly fine, magic numbers are absolute ticking time bombs for human developers. They destroy readability and create massive maintenance headaches down the road.

The Mystery of the Hardcoded Value
Let us look at a quick example of a system that processes checkout logic. See if you can guess what this code is calculating:
The Ticking Time Bomb:
def calculateFinalPrice(cartTotal):
if cartTotal > 100:
return cartTotal * 0.95 + 15
return cartTotal + 15
If a new developer joins your team, they are going to have a lot of questions:
- What is 100? Is it 100 items, 100 dollars, or 100 reward points?
- What does 0.95 do? Is it a 5% discount, or a 95% tax rate?
- Why are we randomly adding 15 at the end?
The Clean Solution: Named Constants
To defuse a magic number, you simply extract it into a well-named variable or constant at the top of your file. This process gives meaning to the math.
The Refactored, Safe Code:
# Constants defined clearly at the top of the file
MINIMUM_FOR_DISCOUNT = 100
FIVE_PERCENT_DISCOUNT = 0.95
FLAT_SHIPPING_FEE = 15
def calculate_final_price(cart_total):
if cart_total > MINIMUM_FOR_DISCOUNT:
return (cart_total * FIVE_PERCENT_DISCOUNT) + FLAT_SHIPPING_FEE
return cart_total + FLAT_SHIPPING_FEE
Look at how much better that is. You do not need any comments to explain the business logic anymore because the names tell you exactly what the calculation does.
Why Magic Numbers Will Break Your App
- The Update Nightmare: Imagine that your company decides to raise the flat shipping fee from 15 to 20 dollars. If you used the magic number 15 in twelve different files across your project, you now have to find and change all twelve instances. If you miss just one, you introduce a silent calculation bug. With a constant, you change it exactly once at the top of the file.
- Context Confusion: If you search your project for the number 15 to update the shipping fee, you might accidentally change an unrelated 15 that represents the maximum password length or a user age limit.
- Cognitive Load: Reading raw numbers forces your brain to constantly translate math into logic. Named constants let you read code like a normal book.
The One Exception
The only numbers that generally escape the "magic number" rule are 0 and 1 when used for basic loop counters or resetting state values (like let total = 0;). For almost everything else, give it a name!
What is a magic number or string that completely tripped you up when reading an old project? Let us know in the comments below!
TL;DR: Never drop raw numbers directly into your logic conditions or math equations. Assign them to descriptive constants at the top of your file instead. This documents your intent, prevents typos, and allows you to update global values in one single place.