LLM Skills
~/catalog/debugging & maintenance//tdd-refactor

Refactoring process

/tdd-refactor

Refactor code with confidence using comprehensive test safety net:

wshobsonwshobson
2.6k
October 12, 2025
// skill content

--- model: claude-opus-4-1 --- Refactor code with confidence using comprehensive test safety net: [Extended thinking: This tool uses the tdd-orchestrator agent (opus model) for sophisticated refactoring while maintaining all tests green. It applies design patterns, improves code quality, and optimizes performance with the safety of comprehensive test coverage.] ## Refactoring Process Use Task tool with subagent_type="tdd-orchestrator" to perform safe refactoring. Prompt: "Refactor this code while keeping all tests green: $ARGUMENTS. Apply TDD refactor phase excellence: 1. Pre-Refactoring Assessment - Analyze current code structure and identify code smells - Review test coverage to ensure safety net is comprehensive - Identify refactoring opportunities and prioritize by impact - Run all tests to establish green baseline - Document current performance metrics for comparison - Create refactoring plan with incremental steps 2. Code Smell Detection - Duplicated Code: Extract methods, pull up to base classes - Long Methods: Decompose into smaller, focused functions - Large Classes: Split responsibilities, extract classes - Long Parameter Lists: Introduce parameter objects - Feature Envy: Move methods to appropriate classes - Data Clumps: Group related data into objects - Primitive Obsession: Replace with value objects - Switch Statements: Replace with polymorphism - Parallel Inheritance: Merge hierarchies - Dead Code: Remove unused code paths 3. Design Pattern Application - Creational Patterns: Factory, Builder, Singleton where appropriate - Structural Patterns: Adapter, Facade, Decorator for flexibility - Behavioral Patterns: Strategy, Observer, Command for decoupling - Domain Patterns: Repository, Service, Value Objects - Architecture Patterns: Hexagonal, Clean Architecture principles - Apply patterns only where they add clear value - Avoid pattern overuse and unnecessary complexity 4. SOLID Principles Enforcement - Single Responsibility: One reason to change per class - Open/Closed: Open for extension, closed for modification - Liskov Substitution: Subtypes must be substitutable - Interface Segregation: Small, focused interfaces - Dependency Inversion: Depend on abstractions - Balance principles with pragmatic simplicity 5. Refactoring Techniques Catalog - Extract Method: Isolate code blocks into named methods - Inline Method: Remove unnecessary indirection - Extract Variable: Name complex expressions - Rename: Improve names for clarity and intent - Move Method/Field: Relocate to appropriate classes - Extract Interface: Define contracts explicitly - Replace Magic Numbers: Use named constants - Encapsulate Field: Add getters/setters for control - Replace Conditional with Polymorphism: Object-oriented solutions - Introduce Null Object: Eliminate null checks 6. Performance Optimization - Profile code to identify actual bottlenecks - Optimize algorithms and data structures - Implement caching where beneficial - Reduce database queries and network calls - Lazy loading and pagination strategies - Memory usage optimization - Always measure before and after changes - Keep optimizations that provide measurable benefit 7. Code Quality Improvements - Naming: Clear, intentional, domain-specific names - Comments: Remove obvious, add why not what - Formatting: Consistent style throughout codebase - Error Handling: Explicit, recoverable, informative - Logging: Strategic placement, appropriate levels - Documentation: Update to reflect changes - Type Safety: Strengthen types where possible 8. Incremental Refactoring Steps - Make small, atomic changes - Run tests after each modification - Commit after each successful refactoring - Use IDE refactoring tools when available - Manual refactoring for complex transformations - Keep refactoring separate from behavior changes - Create temporary scaffolding when needed 9. Architecture Evolution - Layer separation and dependency management - Module boundaries and interface definition - Service extraction for microservices preparation - Event-driven patterns for decoupling - Async patterns for scalability - Database access patterns optimization - API design improvements 10. Quality Metrics Tracking - Cyclomatic Complexity: Reduce decision points - Code Coverage: Maintain or improve percentage - Coupling: Decrease interdependencies - Cohesion: Increase related functionality grouping - Technical Debt: Measure reduction achieved - Performance: Response time and resource usage - Maintainability Index: Track improvement - **Code Duplic

// original public source
wshobson/commands
/tools/tdd-refactor.md
License: License not specified. Review the repository before reusing it.
Independent project, not affiliated with Anthropic. This skill remains the property of its original author.
// install this skill
Paste this command in your terminal at the root of your project:
mkdir -p .claude/commands && curl -o ".claude/commands/tdd-refactor.md" "https://raw.githubusercontent.com/wshobson/commands/main/tools/tdd-refactor.md"
Then in Claude Code, type /tdd-refactor to activate it.
open_in_newOpen original source
// save
Save available after sign in.
loginSign in to save
// information
Creatorwshobson
Stars 2.6k
UpdatedOctober 12, 2025
Format.md
AccessFree
// similar

Skills Debugging & maintenance

View allarrow_forward