When to refactor duplicated code in AI-assisted development
Core Idea: While traditional software engineering suggests refactoring after code is duplicated three times, LLMs naturally tend toward duplication unless explicitly instructed to refactor.
Key Elements
- Traditional "Rule of Three" suggests:
- Duplicate code once (two copies) is acceptable
- On third occurrence, refactor to eliminate duplication
- Balances DRY (Don't Repeat Yourself) with premature abstraction risks
- LLMs have strong tendencies toward code duplication:
- Models naturally produce complete copies with modifications
- Require explicit prompting to perform refactoring
- Will follow existing codebase patterns of duplication
- May not proactively identify refactoring opportunities
Implementation Strategies
- Explicitly instruct LLMs to reduce duplication when generating code
- Ask models to identify and factor out common patterns
- Request the creation of helper methods when writing similar tests
- Be aware that in agent mode, models need to discover helpers to use them
- Review generated code specifically for duplication patterns
Balancing Concerns
- Excessive DRY can lead to wrong abstractions
- Too little refactoring creates maintenance challenges
- Context window limitations may prevent models from seeing all duplicated code
- Balance between specificity (duplication) and abstraction (reuse)
- Consider readability and maintainability in refactoring decisions
Connections
- Related Concepts: Preparatory Refactoring (code restructuring), Culture Eats Strategy (environment influence)
- Broader Context: Code Quality Principles (software design fundamentals)
- Applications: DRY Principle Implementation (reducing repetition)
References
- Sandi Metz (2016). "The Wrong Abstraction." https://sandimetz.com/blog/2016/1/20/the-wrong-abstraction
- Edward Z. Yang (2025). "AI Blindspots" collection, March 2025.
#refactoring #code-quality #duplication #abstraction
Connections:
Sources:
- From: AI Blindspots