Code Refactoring Advisor
Transform messy, complex code into clean, maintainable, well-structured code with clear explanations.
Prompt Template
You are a software architect specializing in code quality and refactoring. Refactor the following code to be cleaner, more maintainable, and following best practices. **Language/Framework:** [language and version] **Current code:** ``` [paste your code here] ``` **What this code does:** [brief explanation] **Pain points:** [what's wrong — hard to test, too complex, duplicated logic, etc.] **Constraints:** [what can't change — public API, database schema, backwards compatibility, etc.] Please: 1. **Assess** — rate the current code (1-10) and identify the top 3 issues 2. **Refactored code** — provide the complete rewritten code 3. **Changes explained** — for each significant change, explain: - What you changed - Why it's better - What pattern/principle it follows 4. **Migration notes** — any breaking changes or things to update elsewhere 5. **Testing strategy** — what tests to add to cover the refactored code Prioritize: readability > maintainability > performance (unless perf is a stated concern). Keep the same external behavior — this is a refactor, not a rewrite.
Example Output
Assessment: 4/10
Top issues:
1. God function — `processOrder()` is 200 lines handling validation, pricing, inventory, and notifications
2. Magic numbers everywhere — `if (qty > 50)`, `discount * 0.15`, `timeout: 30000`
3. No error handling — any failure crashes the entire order flow
Refactored Code
// Extracted into focused, single-responsibility functions
class OrderProcessor {
constructor(
private validator: OrderValidator,
private pricer: PricingEngine,
private inventory: InventoryService,
private notifier: NotificationService
) {}
async process(order: Order): Promise<OrderResult> {
const validated = await this.validator.validate(order);
const priced = await this.pricer.calculate(validated);
await this.inventory.reserve(priced.items);
await this.notifier.sendConfirmation(priced);
return { success: true, orderId: priced.id };
}
}
Why This Is Better
- **Single Responsibility**: Each class has one job
- **Testable**: You can mock each dependency independently
- **Configurable**: Magic numbers extracted to config
Tips for Best Results
- 💡Include the full function/class — partial code limits the quality of suggestions
- 💡Mention your team's conventions if you have a style guide
- 💡Specify if you need backwards compatibility with existing callers
- 💡Ask for the refactoring as a series of small steps if it's a large codebase
Frequently Asked Questions
What is the Code Refactoring Advisor prompt?
Transform messy, complex code into clean, maintainable, well-structured code with clear explanations. It's a free ChatGPT prompt template from our Coding collection — copy it, fill in the bracketed variables, and paste it into your AI tool.
Which AI tools work with this prompt?
It's written and tested for ChatGPT, Claude and Gemini. Any AI assistant that accepts free-form text prompts will handle it well.
How do I customize this ChatGPT prompt?
Replace the bracketed variables — such as [language and version], [paste your code here], [brief explanation] — with your own details before running it. Include the full function/class — partial code limits the quality of suggestions
Is this prompt free to use?
Yes. Every prompt on PromptAtlas is free to copy, customize, and use — no signup required.
Related Prompts
Python FileNotFoundError Debugging Prompt
Debug Python FileNotFoundError from the exact traceback, resolved path, working directory, filesystem context, deployment environment, and failing operation.
Python ZeroDivisionError Debugging Prompt
Debug Python ZeroDivisionError from the exact traceback, operand values and types, data path, numeric semantics, runtime context, and failing input.
Python IndentationError Debugging Prompt
Debug Python IndentationError from the exact parser message, whitespace, block structure, source bytes, formatter settings, and runtime version.
Python SyntaxError: Invalid Syntax Debugging Prompt
Debug Python SyntaxError invalid syntax from the exact parser location, surrounding tokens, Python version, generated code, execution context, and minimal reproduction.
Python AttributeError: Object Has No Attribute Debugging Prompt
Debug Python AttributeError from the exact traceback, runtime object type, attribute contract, construction path, version, imports, and minimal reproduction.
Python UnboundLocalError Debugging Prompt
Debug Python UnboundLocalError from the exact traceback, binding scope, assignments, branches, closures, imports, runtime path, and minimal reproduction.