Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Refactoring with AI Agents

Traditional refactoring advice assumes human developers who naturally write DRY, well-structured code from the start. AI agents exhibit fundamentally different coding patterns that require adapted refactoring strategies. Rather than fighting these patterns, effective AI collaboration embraces them and integrates systematic cleanup into the development workflow.

The AI Verbosity Problem

Large language models demonstrate a consistent tendency toward verbose, repetitive code generation. Even when explicitly prompted to follow DRY principles or write clean, modular code, AI agents typically produce implementations with significant duplication and unnecessarily complex structures.

This pattern emerges from how LLMs process context and generate code. They excel at pattern matching and rapid code production, but struggle with the architectural discipline that humans develop through experience. The result is functional code that works correctly but contains substantial redundancy and missed abstraction opportunities.

Attempting to force DRY principles during initial code generation creates friction and slows development without proportional benefit. AI agents often misunderstand abstraction requests, leading to over-engineered solutions or incomplete implementations that require more correction effort than systematic post-generation cleanup.

The Three-Phase Refactoring Cycle

Effective AI collaboration adopts a three-phase approach that separates code generation from code optimization:

Phase 1: Generate to Green

Allow the AI agent to write repetitive, verbose code without DRY constraints. Focus entirely on functionality and test coverage. The goal is working code that passes all tests, regardless of structural quality.

This phase leverages AI agents' natural strengths while avoiding their weaknesses. Agents excel at rapid implementation when freed from architectural constraints. The repetitive code they generate typically follows consistent patterns that become clear refactoring targets in subsequent phases.

Phase 2: Plan the Cleanup

Once tests are passing, prompt the AI agent to review its own implementation and propose a refactoring plan. This meta-cognitive step often produces better results than upfront architectural guidance because the agent can analyze actual code patterns rather than working from abstract requirements.

The refactoring plan should identify specific duplication patterns, extract common abstractions, and propose architectural improvements. The human developer reviews this plan, suggests modifications, and approves the refactoring strategy before implementation begins.

Phase 3: Execute Refactoring

Implement the approved refactoring plan while maintaining test coverage. This phase benefits from the safety net that TDD provides—comprehensive tests catch regressions introduced during restructuring operations.

The AI agent performs the mechanical refactoring work under human oversight. The human ensures that the refactoring preserves intended behavior and maintains architectural consistency with the broader system.

Why This Approach Works

The three-phase cycle addresses the fundamental mismatch between AI code generation patterns and human architectural expectations. Rather than forcing AI agents to work against their natural tendencies, it creates a workflow that maximizes their contributions while maintaining code quality.

Separation of Concerns: Code generation and optimization become distinct activities with different success criteria. Generation focuses on functionality; optimization focuses on structure.

Leveraging AI Strengths: AI agents excel at rapid implementation and mechanical refactoring operations. The workflow emphasizes these strengths while minimizing exposure to architectural decision-making where they perform poorly.

Human Oversight: Critical architectural decisions remain under human control through the plan review process. This ensures that refactoring improves rather than degrades system architecture.

Safety Through Testing: TDD provides continuous validation throughout the refactoring process. This safety net enables aggressive restructuring that would be risky without comprehensive test coverage.

Application in Different Workflow Modes

Discovery Mode Refactoring

During discovery workflow, refactoring serves architectural exploration. As toy models reveal effective patterns, aggressive refactoring extracts these patterns into reusable forms. The three-phase cycle accelerates this extraction process while maintaining the experimental velocity that discovery mode requires.

Discovery refactoring often involves more radical restructuring as understanding evolves. The AI agent's willingness to perform extensive mechanical changes becomes particularly valuable when architectural insights require significant code reorganization.

Execution Mode Refactoring

In execution workflow, refactoring maintains architectural consistency as the system grows. The three-phase cycle becomes mandatory after each feature implementation, preventing the gradual degradation that typically occurs in evolving codebases.

Execution refactoring focuses on integration seams and pattern consistency rather than architectural discovery. The AI agent identifies where new code deviates from established patterns and proposes alignment strategies.

Practical Implementation

The refactoring cycle integrates naturally into both workflow modes through consistent prompting patterns:

Generation Phase: "Implement [feature] to make tests pass. Focus on functionality over code organization."

Planning Phase: "Review the implementation and identify opportunities for reducing duplication and improving structure. Propose a specific refactoring plan."

Execution Phase: "Implement the approved refactoring plan while maintaining all test coverage."

This structured approach transforms what traditional development treats as occasional cleanup into routine system maintenance. The economic reality that AI assistance makes refactoring dramatically less expensive enables this shift from optional to mandatory practice.

Economic Impact

The three-phase cycle fundamentally changes refactoring economics. Traditional development delayed refactoring due to high manual effort costs. With AI assistance, refactoring becomes routine maintenance rather than expensive technical debt remediation.

This economic shift enables continuous quality improvement rather than gradual degradation. Systems maintain architectural integrity through incremental improvements rather than requiring periodic major restructuring efforts.

The result is codebases that improve consistently over time while maintaining rapid development velocity. The AI agent handles the mechanical aspects of refactoring while human oversight ensures architectural coherence and quality improvement.