What is Refactoring?
Refactoring, told as a story about a publishing house re-editing sentence structure while the plot stays exactly the same.
The older the manuscript, the scarier it gets to touch.The sentences are so tangled, nobody knows where to even start.
Readers notice if the plot changes, but leaving the sentences as-is makes the next edit harder.Could we retouch just the sentences and leave the plot alone?
Refactoring re-edits the sentence structure while the plot stays exactly the same.The outside (plot) stays the same — only the inside (sentence structure) gets reworked.
Refactoring repeats small edits, such as merging, splitting, and unifying names, each followed by a check.The proofreader checks the plot hasn't moved, every single time.
Without proofing (tests), you can't be sure the plot stayed the same.Refactoring isn't adding features — it's only reworking the structure.
Refactoring is a re-edit that keeps the book's plot (external behavior) exactly the same while reworking only the sentence structure (internal design).
Refactoring improves a program's internal code structure without changing its external behavior (functionality, output). It's done in small steps — removing duplication, splitting functions/modules, unifying names — verified by tests at each step to confirm behavior hasn't changed. It is distinct from adding new features.
When grown-ups say it
- Refactoring
- Same plot, retouched sentences. The external behavior never changes.
- External Behavior
- The plot readers actually read. Must stay identical before and after.
- Code Smell
- The whiff of sentences gone tangled. A signal that refactoring is needed.
- Unit Test
- The proofing that checks the plot hasn't moved. The safety net that makes refactoring safe.
- Technical Debt
- A debt that must be repaid later. Refactoring is one way to repay this debt. → the debt you pile up to move fast now
- Encapsulation
- Locking a drawer's insides so everything goes through its service window. The service window stays the same while only the drawer's inside gets refactored. → the writer's locked drawer
- Design Pattern
- The editing style guide. Often the destination refactoring moves toward.
- Regression
- An accident where the plot shifts while editing. The exact accident refactoring is built to avoid.