Home/Blog/ChatGPT Prompt for Code Refactoring: 10 Safe Refactors
Blog

ChatGPT Prompt for Code Refactoring: 10 Safe Refactors

P
promptstudio

10 ChatGPT prompts for code refactoring: pin behavior with tests first, name each refactoring move, ship small commits, and catch invented APIs before merge.

ChatGPT Prompt for Code Refactoring: 10 Safe Refactors

The best ChatGPT prompt for code refactoring asks for a plan before it asks for code: list current behavior, pin it with tests, then change the code in small steps that each pass. Paste the real code and your versions, and tell ChatGPT not to invent library calls. The 10 prompts below follow that order, from behavior inventory to the final review.

Pin behavior before you touch it

1. Behavior inventory

Here is my code: [code with file paths]. Stack: [language and versions]. List every behavior, edge case, error path, and side effect as plain statements I can check. Do not suggest changes yet.

2. Characterization test writer

Using [behavior inventory], write [test framework] tests that pin current behavior, including odd behavior that looks like a bug. Do not fix anything.

3. Suspected bug list

Read [code] and list behavior that looks wrong, with the line and why. Mark each as fix later, so the refactor stays behavior-preserving.

Plan named refactoring moves

4. Smell to refactoring map

These smells bother me: [smells]. Link each one, plus any you see in [code], to a named refactoring such as Extract Function or Introduce Parameter Object, with the lines it touches.

5. Small commit sequence

Goal: [refactor goal]. Constraints: [public signatures, no new dependencies, performance budget]. Write an ordered list of small commits that each compile and pass all tests, with a one-line commit message each.

6. Constraint conflict check

Does [refactor goal] conflict with [hard constraints] for this code: [code]? If so, tell me the safe part to do now and what needs a team decision.

Apply one step at a time

7. Single-step diff

Apply only step [number] from [commit sequence] to [code]. Return a unified diff and nothing else. Use only functions present in my code or the [language version] standard library.

8. Accidental behavior change risks

Compare [old code] and [new code]. List anything that could change behavior: rounding, ordering, input mutation, exception types, default values. Name the test that would catch each.

Review before merge

9. API honesty check

List every function, method, and import used in [refactored code]. Mark each as in my original code, in the [language version] standard library, or UNVERIFIED so I can check docs.

10. Reviewer summary for the pull request

Write a pull request description for this refactor: [commits and diff]. Say what changed in structure, that behavior is unchanged, which tests prove it, and which suspected bugs are left for later.

Plan the whole refactor with one prompt

The flagship Code Refactoring Prompt: Tests First, Small Safe Steps runs the behavior inventory, characterization tests, smell map, commit sequence, first diff, risk notes, and API check together. Its example untangles a nested Python shipping-cost function so Canada rates can be added without another if block.

Related prompts

To fill test gaps, try the unit test generator and the ChatGPT prompt for test cases guide. Before merging, run the pull request code review assistant or read Claude Code prompts for code review. If a step breaks something, the Python traceback debugger helps find the root cause.

Related: ChatGPT prompt for debugging and system prompt for a coding agent.

Find more in the Coding category, look through Browse, or post your own on Share.