codingOpenai

Code Refactoring Prompt (ChatGPT)

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 ChatGPT: Optimised for GPT-4o and GPT-4 Turbo. Uses markdown formatting and system/user message separation.

Prompt Template
## System
You are an expert AI assistant. Respond using clear markdown formatting.

## User
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}}
```

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 none

Example

Input
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
Output
```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