Refactor
ELI5 — The Vibe Check
Refactoring is cleaning and reorganizing your code without changing what it does — like tidying your room without throwing anything away. The program behaves exactly the same, but the code is cleaner, easier to read, and less likely to cause future problems. It is paying off technical debt.
Real Talk
Refactoring is the disciplined process of improving the internal structure of code without altering its external behavior. It involves applying techniques like extracting functions, removing duplication (DRY), renaming for clarity, simplifying conditionals, and restructuring data. Good refactoring is backed by tests that prove behavior has not changed. Martin Fowler's book 'Refactoring' is the canonical reference.
Show Me The Code
// Before refactoring:
function calc(u, d) {
return u.p * d * (1 - (u.vip ? 0.2 : 0));
}
// After refactoring (same behavior, much clearer):
const VIP_DISCOUNT = 0.2;
function calculateOrderTotal(user, quantity) {
const baseTotal = user.price * quantity;
const discountRate = user.isVip ? VIP_DISCOUNT : 0;
return baseTotal * (1 - discountRate);
}
When You'll Hear This
"Refactor that function before adding new features." / "Don't refactor and fix a bug in the same commit."
Related Terms
Code Review
A code review is when another developer reads your code before it gets merged, looking for bugs, bad practices, or anything confusing.
DRY (Don't Repeat Yourself)
If you find yourself copy-pasting the same code in multiple places, STOP. Make it a reusable function instead.
Technical Debt
Technical debt is the coding equivalent of putting things on a credit card.