## Definition **AI-assisted code refactoring** is the practice of using LLM prompts to restructure existing code — improving readability, modularity, and maintainability — without changing external behaviour. The AI acts as a refactoring partner: the developer identifies the smell and frames the prompt; the model generates the restructured form. ## Why AI Helps Here Refactoring is often skipped under deadline pressure. AI lowers the cost of each operation to a prompt or two: clarifying ninja code, extracting methods, or renaming identifiers. The developer supplies the goal and judgment; the model supplies the mechanical transformation. (Taulli, *AI-Assisted Programming*, ch. 8.) ## Common Refactoring Operations ### Clarify Ninja Code Dense, one-liner trickery can be dumped into a prompt with an instruction like "Explain this step by step and rewrite it in a more maintainable form." The model names the pattern (e.g., a recursive Fibonacci) and expands it to named functions. ### Extract Method When a function has grown too large, prompt the model to identify sections suitable for extraction and suggest cohesive names. Pair with a single-responsibility check: each extracted function should have one clearly named purpose. ``` Prompt: I've attached a piece of my C# code. Could you suggest which sections would be good candidates for extract method refactoring? ``` ### Decompose Conditionals Long `if-then-else` chains are extracted into named predicate methods, making the main flow readable at a glance. Example: `if (user.isActive() && user.hasSubscription() && !user.isBlocked())` → `canUserAccessContent()`. ### Rename Identifiers Ask the model to propose descriptive names for functions, variables, and classes that have drifted from their original purpose. **Caution:** renaming in agentic tools (e.g., Copilot inline) can silently break call sites that use the old name; always verify references. ### Dead Code Detection LLMs can flag apparently unused code, but the approach is risky: dead-looking code may handle rare edge cases or be referenced by reflection. Static analysis tools (ESLint, Pylint, SonarQube) are more reliable for this operation than an LLM alone. ## Prompt Patterns | Operation | Prompt template | |---|---| | Clarify dense code | "Explain this code step by step. Rewrite it in a simpler, more maintainable way." | | Extract method | "Identify sections suitable for extract-method refactoring in the code below." | | Decompose conditional | "Break down this complex if-then-else into named predicate methods." | | Rename | "Suggest better names for the identifiers in this snippet." | | Dead code | "Does any part of this code appear unused or redundant? Note any risks before removing." | ## Constraints and Risks - AI does not understand the full call graph across a large codebase; it works on the snippet it sees. - Refactored code still requires the developer's review and a test run. - For dead code, prefer linters and static analysis tools over LLM judgment. ## Related - [[Prompt Engineering]] - [[In-Context Learning]] - [[Spec-Driven Development]] - [[Evaluation-Driven Development]] ## Sources - [[AI-Assisted Programming - Tom Taulli]]