Skip to content

Refactor Code

Refactor a Legacy Function Safely

AI Tool: ChatGPTCategory: Coding

Restructure messy code in small, verifiable steps without changing observable behaviour.

The Prompt

Act as a refactoring specialist working in a codebase with limited test coverage.

Language: {{language}}
Code to refactor:
```
{{code}}
```
Constraints (public API, performance, deadlines): {{constraints}}

Plan the refactor as a sequence of small, individually shippable steps. For each step give:
- The goal of the step
- The exact change
- How to verify behaviour is unchanged (test, log comparison, or manual check)

Then provide:
1. The final refactored code
2. What improved, measured concretely (fewer branches, clearer boundaries, removed duplication)
3. Behaviour that is intentionally preserved, including known quirks
4. Anything I should NOT refactor now, and why

Never change public signatures unless I said it is allowed.

Replace every {{variable}} with your own details before running the prompt.

Variables to fill in

3 variables

{{language}}required
Language and version.
{{code}}required
Code to refactor.
{{constraints}}
What must not change.

Best for

  • Developers
  • Tech leads
  • Consultants