Code Refactoring Prompt (Claude)
Effective refactoring prompts must specify what to change and what must not change. This template uses explicit constraints to prevent the AI from breaking the public API or silently altering behaviour, which is the most common failure mode when asking models to rewrite code. The summary table helps you understand every change before you commit it. This variant is formatted for Claude: Optimised for Claude 3.5 Sonnet and Claude 3 Opus. Uses XML tags for structured input and output.
<role>You are an expert AI assistant with deep knowledge in this domain.</role>
<task>
You are a senior software engineer specialising in code quality and refactoring.
Refactor the following {{language}} code to improve {{goals}}.
Constraints:
- Do NOT change the public API or function signatures
- Do NOT add new dependencies unless absolutely necessary
- Preserve all existing behaviour including edge cases
- {{additional_constraints}}
For each change you make, add a brief inline comment explaining why you made it.
After the refactored code, provide a summary table listing:
| Change | Reason | Risk |
Original code:
```{{language}}
{{code}}
```
</task>
<instructions>Structure your response clearly with headers and concrete examples.</instructions>Variables
{{language}}Programming language (e.g., Python, JavaScript, Java){{goals}}Refactoring objectives, e.g., "readability and removing duplication"{{code}}The code to refactor{{additional_constraints}}Any extra rules, or write "None" if there are noneExample
language: Python
goals: readability and removing duplication
additional_constraints: Must stay compatible with Python 3.9
code:
def process(items):
result = []
for i in items:
if i > 0:
result.append(i * 2)
result2 = []
for i in result:
if i < 100:
result2.append(i)
return result2```python
def process(items: list[int]) -> list[int]:
# Combine filter+transform into a single comprehension to eliminate two loops
return [i * 2 for i in items if 0 < i * 2 < 100]
```
| Change | Reason | Risk |
|--------|--------|------|
| List comprehension | Eliminates duplicate loop logic | Low — same semantics |
| Type hints | Documents expected types | None — annotations only |Related Tools
FAQ
- Will the refactored code always be semantically identical?
- Not guaranteed. Always run your test suite against the refactored code. The "Preserve all existing behaviour" constraint reduces the risk significantly, but edge cases with floats, side effects, or exception handling can still differ.
- Can I refactor entire files rather than snippets?
- Yes, but keep the file under 400 lines to stay within context limits for most models. For larger files, refactor one function or class at a time using this prompt repeatedly.
- How do I handle refactoring across multiple interdependent files?
- Include the relevant imports and type definitions alongside the code, and note any external dependencies in the additional_constraints field. Then apply changes file by file.
Related Prompts
Effective refactoring prompts must specify what to change and what must not change. This t...
Code Refactoring Prompt (ChatGPT)Effective refactoring prompts must specify what to change and what must not change. This t...
Code Refactoring Prompt (Gemini)Effective refactoring prompts must specify what to change and what must not change. This t...
Code Refactoring Prompt (LLaMA / Ollama)Effective refactoring prompts must specify what to change and what must not change. This t...
Code Review PromptThis prompt structures code reviews into five clear categories so the AI produces actionab...
Code Documentation PromptAuto-generated documentation is only useful when it goes beyond repeating the function sig...