Code Complexity Reducer
Analyzes complex code and suggests specific refactoring patterns to improve readability and maintainability.
Category: coding
Difficulty: beginner
Platforms: chatgpt claude
Tags: refactoring clean-code complexity maintainability
Prompt Template
You are a software craftsmanship expert who simplifies complex code. Analyze and simplify the following code.
Complex code:
```{{language}}
{{code}}
```
What makes it complex: {{complexity_concerns}}
Constraints: {{constraints: must maintain behavior/can change API/performance-critical}}
## Complexity Analysis
### Metrics
| Metric | Current | Target | Status |
| Cyclomatic complexity | | < 10 | |
| Function length | | < 30 lines | |
| Nesting depth | | < 3 levels | |
| Parameter count | | < 4 | |
### Problem Areas
| Line(s) | Issue Type | Description | Impact |
Issue types: Deep nesting, God function, Feature envy, Primitive obsession, Long parameter list, Complex conditional, Duplicated logic
## Refactoring Plan
Ordered sequence of refactoring steps (each step should leave the code working):
### Step 1: [Refactoring Name]
- What: Description of the change
- Why: What complexity it removes
- Risk: Low/Medium/High
```{{language}}
// Before
// After
```
### Step 2: [Refactoring Name]
[Same format]
## Final Refactored Code
```{{language}}
// Complete refactored version with comments
```
## Before/After Comparison
| Metric | Before | After | Improvement |
## Tests to Add
Tests that should exist before and after refactoring to ensure behavior is preserved.
## Patterns Applied
| Pattern | Where | Why It Helps |
Tips
- Add tests for current behavior BEFORE refactoring - they're your safety net
- Apply one refactoring at a time and verify tests pass after each step
- Reducing nesting depth usually has the biggest readability impact for the least risk
- If the refactored code is harder to understand than the original you went too far - simplicity beats cleverness