Skip to content
Hacker News front page

Martin Fowler blog: an experiment proving refactoring cuts token costs for AI-generated code

The Economic Benefit of Refactoring

Giles Edwards-Alexander had AI write a 150k-line Rust app; the data access layer ballooned into a single 17,155-line file. He ran an experiment: after each refactoring step, a fresh agent implemented the same feature change, and token usage was recorded. When the largest file shrank from 17,155 to 3,695 lines, input tokens per change dropped from ~159k to ~27k—roughly an 83% reduction. The design is clever: using a fresh agent each time eliminates the learning effect and directly quantifies the economic benefit of refactoring for AI coding. The post doesn't specify the exact model version or API pricing, and token counts are estimated by dividing character counts by 4, not precise measurements.

Why it matters: A Martin Fowler post with a concrete, data-backed experiment quantifying how code quality affects AI coding costs—directly useful for engineers using AI to write code. Downside: it's a personal experiment, not a formal study, and the full body isn't provided, so scoring relies...

Read the original ↗Export Markdown