AI Prompts Library

Curated collection of expert prompts for coding, writing, marketing, image generation, and more

Have a great prompt? Submit it to the library →

Coding

Code Refactoring Expert

Optimized for: general • TEXT
You are a refactoring expert applying Clean Code principles, SOLID design, and proven design patterns. Analyze the provided code and suggest refactoring improvements.

**Code to Refactor:**
[PASTE CODE HERE]

**Language:** [SPECIFY]
**Context:** [WHAT THE CODE DOES, WHERE IT RUNS]
**Constraints:** [ANY LIMITATIONS - backwards compatibility, performance requirements, etc.]

**Refactoring Analysis:**

1. **Code Smells Detection** (identify all that apply):
   - Long Method (> 20 lines): Break into smaller, focused functions
   - Large Class: Split by responsibility using SRP
   - Feature Envy: Move methods to the class they use most
   - Data Clumps: Group related parameters into objects
   - Primitive Obsession: Replace primitives with value objects
   - Duplicate Code: Extract to shared functions or base classes
   - God Object: Distribute responsibilities across multiple classes
   - Deep Nesting: Use early returns, guard clauses, or strategy pattern
   - Magic Numbers/Strings: Extract to named constants
   - Dead Code: Remove unused functions, imports, and variables

2. **SOLID Principles Check:**
   - S: Does each class/module have a single responsibility?
   - O: Can behavior be extended without modifying existing code?
   - L: Can subclasses be used interchangeably with their base class?
   - I: Are interfaces lean and focused?
   - D: Do high-level modules depend on abstractions?

3. **Design Pattern Opportunities:**
   - Identify where patterns like Strategy, Observer, Factory, Builder, or Repository would improve the code
   - Only suggest patterns that genuinely simplify the code

4. **Refactored Code:**
   - Provide the complete refactored version
   - Explain each change and why it improves the code
   - Ensure the refactored code maintains identical behavior (no functional changes)
   - Add/improve type annotations

5. **Verification:** Describe how to verify the refactoring did not change behavior (tests to write or run).

Code improvement, technical debt reduction, legacy code modernization

Want Custom Prompts?

Get personalized AI prompts tailored to your specific needs and workflow.

Contact Us