Use Claude to improve readability, remove duplication, and simplify complex logic — changing structure without changing behaviour.
Read time12 min
LevelBasic → Advanced
Quiz3 questions
SectionBootcamp 2026
01 — what & why
Refactoring changes shape, not behaviour
The golden rule: behaviour stays identical. Claude is excellent at mechanical refactors — but you must have a way to prove nothing broke.
Before refactoring, make sure there's a test or a way to verify behaviour. Then ask Claude for a specific improvement — extract a function, rename for clarity, remove a duplicated block. See the patterns in Backend → Skills & Patterns.
No tests? Write a characterization test first (competency 5) — otherwise you can't know the refactor was safe.
02 — the technique
Name the smell, then the fix
Vague "clean this up" gives risky rewrites. Name the specific smell.
✓
Be specific
"Extract the validation into a function", not "improve this".
✓
Verify after
Run tests / the app after each refactor.
✗
Don't mix in features
Refactor OR add behaviour — never both in one step.
✗
Don't refactor blind
Without a behaviour check, you're guessing.
03 — match the model to the risk
Mechanical refactors are cheap; risky ones need a plan
The model you reach for should track the risk of the change, not the size of the file.
Mechanical refactors — rename, extract a function, dedupe a block — are low-risk pattern work that runs fast and reliably on Sonnet. Risky refactors — reshaping behaviour-critical logic, untangling gnarly conditionals, or any change where the test net is thin — deserve Opus 4.8 to plan the change first, plus an advisory-strategy pass to catch behaviour you may have changed.
Behaviour-critical logic or a thin test net: plan it, then get a second opinion.
✗
Big rewrite, cheap model, unread
The riskier the change, the more thinking — and review — it needs.
The thinner your test net, the more model choice matters — but a stronger model and an advisor pass are extra safeguards, not a substitute for writing the characterization test first.
The balance math started life inline in the handler. Extract it to a pure, testable function without changing behaviour.
javascript
// BEFORE — logic buried in the route handler:
const used = requests
.filter(r => r.status === 'approved')
.reduce((sum, r) => sum + r.days, 0);
const remaining = ALLOWANCE - used;
javascript
// AFTER — a pure function you can unit-test:
export function calculateLeaveBalance(requests, allowance = ALLOWANCE) {
const used = requests
.filter(r => r.status === 'approved')
.reduce((sum, r) => sum + r.days, 0);
return allowance - used;
}
✓
Behaviour identical
Same result; now reusable and testable. The existing test stays green.
✗
Watch out
Don't sneak a behaviour change (e.g. counting pending) into a "refactor".
Now do this on YOUR project: find one function in your slice doing two jobs and extract the pure part — verify with a test.
05 — best practices
Change shape, not behaviour
Refactoring is safe only when behaviour is pinned by tests and the change is named.
✓
Turn minimalism on early
Run /ponytail lite at project start — YAGNI discipline is cheapest to enforce when the codebase is still small.
✓
Review your diff before every PR
Run /ponytail-review to flag over-engineering before it lands in main — a 5-minute check prevents weeks of debt.
✗
Act on findings blindly
Use /ponytail-gain for before/after reduction metrics and evaluate the tradeoff — “less code” that breaks an invariant is a regression.
✗
Let minimalism strip safety
Never remove input validation, auth checks, error handling, or security guards in pursuit of fewer lines — minimalism must not mean unsafe.
Check your understanding
1. What must stay the same during a refactor?
Correct.Not quite — the correct answer is highlighted above.
2. Before refactoring untested code, what should you do first?
Correct.Not quite — the correct answer is highlighted above.
3. Which prompt is safest for a refactor?
Correct.Not quite — the correct answer is highlighted above.
Basic · GuidedProject: Leave Management System
Improve readability of one file
Objective: pick a messy function in your project and have Claude improve naming and structure — behaviour unchanged.
prompt
Refactor this function for readability only — no behaviour change.
Improve names and extract helpers where it clarifies intent.
Explain each change.
Pick one hard-to-read function.
Ask for a readability-only refactor.
Verify behaviour is unchanged (run it / a test).
✓
Done when
The code reads clearly and behaves identically.
✗
Watch out
Accepting a rewrite you haven't verified.
Discuss with your facilitator: which rename made the biggest difference to understanding the code?
Intermediate · Semi-guidedProject: Leave Management System
Remove duplication across the codebase
Objective: find a block repeated in 2–3 places and consolidate it into one reusable unit.
Have Claude find duplicated logic in your project.
Extract it into one function/module.
Update all call sites; verify.
✓
Done when
One source of truth; all call sites updated and working.
✗
Watch out
Consolidating things that only look similar but differ in intent.
Discuss with your facilitator: was the duplication truly the same, or did merging hide a real difference?
Advanced · Open challengeProject: Leave Management System
Simplify complex logic safely
Objective: take your gnarliest function, add a characterization test if needed, then simplify it while keeping the test green.
✓
Done when
The function is simpler and the behaviour test still passes.
✗
Watch out
Simplifying without a test to prove behaviour held.
Discuss with your facilitator: what made the logic complex — and was it accidental or essential complexity?