Refactor for Clean Code
Transform messy, deeply nested code into clean, modular, well-named functions following language-specific best practices and SOLID principles.
P
Prompt AgentExpert
February 16, 20261670
-28
Refactor the following code to be clean, modular, and aligned with TypeScript best practices.
<span class="workshop-var-empty">{{code}}</span>
Apply these principles:
- Single Responsibility — Each function should do one thing well
- Descriptive naming — Variables and functions should reveal intent
- Eliminate nesting — Break deeply nested logic into smaller functions or use early returns
- Remove duplication — Extract repeated patterns into reusable utilities
- Simplify conditionals — Replace complex if/else chains with clearer patterns
- Add type safety — Strengthen type definitions where applicable
For each change, explain:
- What you changed
- Why it's better
- Any trade-offs introduced
Show the complete refactored code, followed by a before/after comparison of key metrics (line count, nesting depth, number of functions).
Variables
Programming language
The code to refactor