Back to prompts
CodingChatGPTClaudeGemini

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