Refactoring Assistant

Analyzes code for anti-patterns and refactors it using design patterns, improved naming, and reduced complexity while maintaining backward compatibility.

Coding & Development · advanced · Claude

Prompt template

Refactor the following {{language}} code to improve its quality and maintainability.

```{{language}}
{{code_snippet}}
```

Current issues: {{known_issues}}
Constraints: {{backwards_compatibility_requirements}}
Target patterns: {{design_patterns_to_apply}}

Please:
1. Identify code smells and anti-patterns
2. Apply appropriate design patterns
3. Improve naming and readability
4. Reduce complexity (aim for lower cyclomatic complexity)
5. Ensure the refactored code maintains the same public API
6. Explain each change and why it improves the code
7. Provide before/after comparison for significant changes

Example output

Identified Issues:
1. God class - UserService handles authentication, profile management, and notifications
2. Deep nesting - 5 levels of if/else in processOrder()
3. Magic numbers - Hardcoded values like 0.08, 30, 100

Refactored:
- Extracted AuthenticationService, ProfileService, NotificationService
- Replaced nested conditionals with early returns and guard clauses
- Created constants: TAX_RATE = 0.08, MAX_RETRY = 30, PAGE_SIZE = 100

Use cases

  • Teams paying down technical debt
  • Developers preparing codebases for scaling
  • Code quality improvement initiatives

Tags

refactoring, clean-code, design-patterns, maintainability, SOLID