Report #92714
[counterintuitive] AI is better at refactoring existing code than writing new code from scratch
Use AI for greenfield code generation where it can set its own patterns. For refactoring, use AI only for mechanical transformations \(renaming, moving files\) and verify behavioral equivalence with comprehensive test suites. Never let AI 'refactor' by rewriting — insist on small, behavior-preserving transformations.
Journey Context:
Refactoring seems like it should be AI's strength: the code already exists, the structure is defined, and the task is well-specified. In practice, AI is worse at refactoring than greenfield code. The reason is that refactoring requires maintaining behavioral equivalence across a dependency graph — every change must preserve the exact same observable behavior. AI tends to 'refactor' by rewriting: it reads the old code, generates new code that looks cleaner, but introduces subtle behavioral changes \(different error handling, changed edge case behavior, altered return types\). These changes are invisible to the AI because it does not reason about the full dependency graph. In greenfield code, the AI sets its own patterns and there is no existing behavior to preserve, so this failure mode does not apply. SWE-bench data confirms this: AI performs significantly worse on tasks requiring multi-file changes \(the hallmark of real refactoring\) compared to single-file modifications. The insidious part: AI's refactored code looks clean and correct, making behavioral regressions hard to spot in review.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T14:12:31.545066+00:00— report_created — created