Code Refactoring Suggestions
How to use
Provide the code you wish to refactor by replacing "{{args}}". The AI will analyse it for code smells, design patterns, simplification, and architectural improvements, offering high, medium, and low impact suggestions with before/after examples and a step-by-step guide.
Prompt
Code Refactoring Suggestions
Please analyze the following code and provide refactoring recommendations:
{{args}}Focus on:
1. Code Smells
Identify and suggest fixes for:
- Long methods/functions (>20-30 lines)
- Long parameter lists (>3-4 parameters)
- Duplicated code
- Large classes/modules
- Primitive obsession
- Feature envy
- Data clumps
- Switch/conditional complexity
2. Design Patterns
Suggest appropriate design patterns:
- Can Strategy pattern replace conditionals?
- Would Factory pattern improve object creation?
- Is Observer pattern needed for events?
- Should Decorator pattern be used?
- Would Adapter pattern help integration?
- Is Singleton appropriate (or anti-pattern)?
3. Simplification Opportunities
- Can complex conditionals be simplified?
- Are there opportunities to use guard clauses?
- Can nested structures be flattened?
- Should temporary variables be eliminated?
- Can expression complexity be reduced?
4. Extract & Compose
- Methods that should be extracted
- Classes that should be split
- Modules that should be separated
- Utilities that should be shared
- Constants that should be defined
5. Naming Improvements
- Variables with unclear names
- Functions that don't describe what they do
- Classes with vague or misleading names
- Naming inconsistencies
6. Dependency Management
- Dependencies that should be inverted
- Coupling that should be reduced
- Cohesion that should be improved
- Circular dependencies to eliminate
7. Modern Code Practices
- Legacy patterns to modernize
- Functional programming opportunities
- Async/await over callbacks
- Modern syntax improvements
- Type safety enhancements
8. Architecture Improvements
- Layer violations to fix
- Separation of concerns issues
- API design improvements
- State management enhancements
Provide:
- High Impact Refactorings: Most valuable changes
- Medium Impact: Worthwhile improvements
- Low Impact: Polish and minor enhancements
- Before/After Examples: Show concrete refactoring steps
- Step-by-Step Guide: Safe refactoring sequence
- Tests to Write: What to test before/during refactoring
For each refactoring, explain:
- What needs to change
- Why it's beneficial
- How to refactor safely
- What tests to have in place
- Expected improvement in maintainability
I remind you, no classes, only named functions, no arrow functions in top-level. We try to keep the code to code best practices ideally with KISS, DRY, SOLID and YAGNI if it makes sense in the given context and does not create unnecessary code. We try to keep the file size to a minimum, the goal is not for each part of the code to be isolated in a separate file. We try to name the files so that they indicate their function in relation to the project and their internal functionality. I write refactored code in a self-documenting way, I allow renaming of functions and variables and constants. I remind you that every refactoring must also check the tests to avoid damage.