20 of the best prompts for ChatGPT for refactoring, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
20 of the best prompts for ChatGPT for refactoring, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
Published July 9, 2026
Most people try to use AI for ChatGPT for Refactoring with a single vague prompt and get generic results. This guide takes a different approach: 4 targeted stages, from Identifying Refactoring Opportunities through Safe Refactoring and Testing, each with a prompt that gives the AI exactly the context it needs. Improve existing code quality, readability, and maintainability using ChatGPT to identify problems and write cleaner implementations. Works with ChatGPT, Claude, and Gemini.
Use ChatGPT to diagnose code that works but is difficult to maintain, extend, or understand.
Analyze code
Analyze this code for refactoring opportunities: [PASTE CODE]. Identify the top 5 most impactful improvements ranked by value vs. effort.
Code smells
What are the code smells in this file: [PASTE CODE]? List each smell, explain why it is a problem, and suggest what type of refactoring would address it.
Function is too
This function is too long and hard to follow: [PASTE CODE]. Where should it be broken up and what should each smaller function be responsible for?
Identify places
Identify all the places in this codebase where logic is duplicated: [PASTE CODE OR DESCRIBE FILES]. For each duplication, suggest how it should be consolidated.
Rate code
Rate this code on readability, maintainability, and extensibility: [PASTE CODE]. Give specific reasoning for each rating and prioritize what to fix first.
Use ChatGPT to redesign code structure for better separation of concerns, modularity, and testability.
Refactor function
Refactor this function into smaller, single-responsibility functions: [PASTE CODE]. Each new function should do one thing and be independently testable.
Extract reusable utility/helper
Extract a reusable utility/helper from this code that is used in multiple places: [PASTE CODE]. Write the extracted function and show how to replace the inline duplications.
Refactor class
Refactor this class to follow SOLID principles: [PASTE CODE]. Identify which principles are violated and show the refactored version.
Code mixes concerns
This code mixes concerns (UI, business logic, data access). Refactor it to separate these layers clearly: [PASTE CODE]. Show the new structure.
Replace them
I have multiple if/else chains doing similar things in this code: [PASTE CODE]. Replace them with a more maintainable pattern (strategy pattern, lookup table, etc).
Use ChatGPT to update code to modern patterns, idioms, and language features.
Modernize code
Modernize this [JAVASCRIPT/PYTHON/JAVA/ETC] code to use current best practices and language features: [PASTE CODE]. Explain what each change improves.
Refactor callback-based code
Refactor this callback-based code to use async/await: [PASTE CODE]. Preserve the behavior exactly and add proper error handling.
Replace raw SQL strings
Replace these raw SQL strings with parameterized queries or an ORM: [PASTE CODE]. Maintain the query logic while eliminating injection risk.
Simplify code
Simplify this code using more idiomatic [LANGUAGE] patterns: [PASTE CODE]. Show the shorter, cleaner version and explain what each simplification does.
Code was written
This code was written without considering testability. Refactor it to be unit testable by injecting dependencies: [PASTE CODE]. Show the refactored version and an example test.
Use ChatGPT to refactor without breaking behavior, with tests to verify the changes.
Tests
What tests should I write before refactoring this code to ensure I do not break behavior: [PASTE CODE]? List the specific test cases that would catch regressions.
Refactor this code
I want to refactor this code but I am afraid of breaking it: [PASTE CODE]. What is the safest order of changes and what should I verify at each step?
Write before
Write before and after versions of this refactoring: [PASTE CODE + DESCRIBE DESIRED CHANGE]. Show the original, the refactored version, and confirm the behavior is identical.
Refactored code:
I refactored this code: [PASTE ORIGINAL AND NEW VERSION]. Are there any behavioral differences I may have introduced? Check for subtle logic changes.
Minimum set
What is the minimum set of changes needed to make this code testable without a full refactor: [PASTE CODE]? I want to add tests first before any structural changes.
Yes, in increments. Work file by file or function by function. Ask ChatGPT to identify the highest-impact changes first, then tackle them in small, testable steps. Attempting to refactor everything at once is risky regardless of tool.
Paste the specific code to refactor, describe the goal (more readable, more testable, simpler), and ask for the refactored version with an explanation of each change. Then review the output before applying it.
It should not, but always verify. Ask ChatGPT to confirm it has preserved the original behavior. Write tests before refactoring and run them after applying the changes.
ChatGPT reliably catches: long functions, duplicated code, deeply nested conditionals, mixed concerns, poor naming, god objects, and magic numbers. Ask it to specifically look for these if you want a thorough scan.
Write tests for the current behavior first, then refactor. After each change, run the tests to confirm behavior is unchanged. Ask ChatGPT to help write the tests if they do not already exist.
AI Prompts for ChatGPT for Code Review
Use ChatGPT to catch bugs, improve code quality, and prepare better code before human reviewers see it..
See promptsAI Prompts for ChatGPT for Debugging
Diagnose and fix bugs faster by using ChatGPT as a debugging partner across any programming language..
See promptsAI Prompts for ChatGPT for Code Documentation
Write clear, comprehensive code documentation faster using ChatGPT to generate comments, docstrings, READMEs, and API docs..
See prompts