The Economic Benefit of Refactoring

Giles Edwards-Alexander (published on martinfowler.com, byline Martin Fowler) ยท rss ยท 2026-07-30 source โ†— Reader ๐Ÿ–ฅ๏ธ

Agentic EngineeringClaude CodeCode QualityMeasuring AI ImpactSoftware Design FundamentalsTechnical DebtToken Efficiency
Refactoring an agentic codebase according to a strict, correctness-preserving discipline can pay a measurable economic dividend โ€” in this experiment, an 83% reduction in the input-token cost of an identical representative future change โ€” because it lets an AI agent read a smaller, more targeted slice of the code to make the same change, even though the total code volume barely shrinks.

practical ยท mixed โ€” Primarily a narrated experiment report (narrative + expository data tables), but in service of a practical claim: that refactoring an agent-maintained codebase is worth doing because it measurably lowers future token cost. The author is careful to hedge the practical recommendation with explicit limitations, so it reads as evidence-gathering toward a practical conclusion rather than a confident prescription.

Structure

PartRole
Origin narrative: building a 150k-LoC, entirely agent-written app and discovering its data-access layer had silently grown into a single 17,155-line Rust file.context
Experimental design: a fixed 'representative change' prompt, executed by a fresh memoryless sub-agent before refactoring and after each of 15 refactoring steps, recording token cost, time, and LoC each time.method
Results table and chart: LoC and input/output token counts at baseline and after each refactoring step.evidence
'Refactoring reduces token consumption' section: input tokens fell 159,564 โ†’ 27,360 (83%); interpreted as the agent successfully narrowing which files it needs to read, not as there being less code overall.conclusion/interpretation
'Notes on the process': Claude needed explicit human direction to identify refactorings, was clumsy at mechanically applying them (grep/sed scripts breaking on indentation), and the run took ~8 hours mostly unattended with one silent-skip intervention.caveat/context
'Further work and broader implications': the token cost of the refactoring itself is unknown (upper-bounded at ~5M tokens across mixed work), this is a single case on a greenfield single-developer app, and further questions (bigger changes, whole-codebase refactoring, continuous refactoring) are left open.limitations/future work
Appendices: the exact representative-change prompt and the 13-step refactoring plan (each step tagged with a specific Fowler 'Refactoring' 2nd-edition technique and section number).evidence/reproducibility

Key terms

Propositions

Arguments

Refactoring pays an economic dividend for agent-driven software development, at least in this case. strength: medium
Explicit premises:
Implicit premises:
Inferences:
The savings come specifically from the agent reading a smaller, better-targeted subset of files, not from there being less code to read overall. strength: low
Explicit premises:
Implicit premises:
Inferences:

Narratives

Questions addressed

Gaps

Closes gaps elsewhere

Related documents