Code Refactoring is for Perfectionists Who Never Ship
Listen kid, let me tell you about the biggest scam in software engineering: refactoring.
You know what refactoring really is? Itâs rewriting code that already works so you can feel better about yourself. Thatâs not engineeringâthatâs therapy. And therapy costs $200 an hour. Are you paying for that out of the sprint budget?
The Refactoring Trap
Every time I see a junior developer open a PR with ârefactored for readability,â I die a little inside. You know whatâs readable? Working code. You know whatâs not readable? Unemployment paperwork.
| What They Say | What It Means |
|---|---|
| âI refactored for clarityâ | âI spent 3 days renaming variablesâ |
| âThis reduces complexityâ | âI moved the complexity somewhere elseâ |
| âItâs more maintainable nowâ | âIâll be gone before anyone maintains itâ |
| âTechnical debt reductionâ | âI was boredâ |
Why Refactoring is a Lie
Hereâs the dirty secret: code doesnât get better when you refactor. It gets different. And different means new bugs, new edge cases, and new opportunities for things to break in production at 3 AM.
The old code? Battle-tested. Survived Black Friday. Weathered the Great Database Outage of 2019. It has scars, and those scars mean something.
Your âcleanâ refactored code? A newborn baby. Hasnât seen war. Doesnât know what itâs like when the load balancer gives up.
# "Ugly" code that's been in production for 6 years
def process_order(o):
if o:
if o.items:
for i in o.items:
if i.qty > 0:
# DON'T TOUCH THIS - broke prod in 2018
if i.price != None and i.price > 0:
# Jerry said this is fine
total = i.qty * i.price
# WHY DOES THIS WORK? I DON'T KNOW
if total < 0:
total = 0.01
return total
# "Clean" refactored version
def process_order(order: Order) -> Decimal:
"""Process order and calculate total."""
return sum(item.quantity * item.price for item in order.items)
# ^ This lost us $47,000 in the first week
See that comment âWHY DOES THIS WORK?â Thatâs not bad codeâthatâs wisdom encoded. Someone before you fought a battle there. Honor their sacrifice.
The Refactoring Productivity Formula
Hours spent refactoring Ă $150/hour = Money that could have been features
Let me show you what management actually sees:
| Metric | Before Refactoring | After Refactoring |
|---|---|---|
| Features shipped | 12 | 8 |
| Bugs fixed | 45 | 12 |
| New bugs introduced | 3 | 47 |
| Sprint velocity | 100% | 60% |
| Manager happiness | đ | đ¤ |
As XKCD 1205 taught us, you need to calculate how much time automation saves. Well, refactoring saves negative time. Itâs the anti-automation.
The Dilbert Test
Wally from Dilbert has the right idea. He once told me: âI donât refactor because I might accidentally make the code work better, and then theyâll expect me to do it again.â
The Pointy-Haired Boss doesnât understand âcleaner abstractions.â But he understands âNEW FEATURE DEMO READY.â Guess which one gets you a raise?
When Refactoring is Actually Acceptable
- Never
- When the building is literally on fire and refactoring is somehow the only way to call 911
- Still never
My Personal Story
In 1994, I refactored a COBOL payroll system âfor readability.â It was beautiful. Elegant even. Then payday came and 400 employees got paid in Vietnamese Dong instead of US Dollars.
The original code had a mysterious IF COUNTRY-CODE = 'US' OR COUNTRY-CODE = 'ZZ' condition. I removed âZZâ because it seemed wrong. Turns out, âZZâ was how the system handled direct deposit edge cases since 1978.
I havenât refactored since. The code is still running. Iâm scared to look at it.
The Wisdom
âIf it ainât broke, donât touch it. If it is broke, thatâs QAâs problem.â
â Me, every code review
Your job isnât to write pretty code. Your job is to solve business problems and go home at 5 PM. Every hour you spend refactoring is an hour youâre not spending with your family, your hobbies, or competitive CS:GO.
Next week: âWhy Consistent Code Style is Fascismâ
The author once refactored a function and accidentally deleted the production database. The function worked perfectly, though.