refactoring
Guide code refactoring using Martin Fowler's catalog of behavior-preserving transformations. Use during TDD REFACTOR phase, when code smells are detected (duplication, long methods, feature envy), when discussing structural improvements, or before behavioral changes (Tidy First).
When & Why to Use This Skill
The Refactoring skill empowers developers to improve code quality using Martin Fowler's behavior-preserving transformations. It provides a disciplined framework for identifying and fixing code smells—such as duplication, long methods, and feature envy—during the TDD refactor phase. By focusing on structural improvements without altering external behavior, this skill helps teams reduce technical debt and maintain a clean, scalable codebase.
Use Cases
- TDD Refactor Phase: Streamline the 'Red-Green-Refactor' cycle by applying disciplined transformations immediately after a test passes.
- Eliminating Code Smells: Systematically identify and resolve technical debt like Long Methods, Large Classes, and Feature Envy using proven patterns.
- Preparatory Refactoring (Tidy First): Restructure and clean up existing code to make the implementation of a planned new feature easier and safer.
- Legacy Code Modernization: Safely reorganize module boundaries and simplify complex expressions in older codebases while ensuring external behavior remains unchanged.
| name | refactoring |
|---|---|
| description | Guide code refactoring using Martin Fowler's catalog of behavior-preserving transformations. Use during TDD REFACTOR phase, when code smells are detected (duplication, long methods, feature envy), when discussing structural improvements, or before behavioral changes (Tidy First). |
Refactoring Skill
Apply behavior-preserving transformations to improve quality of any codes, including tests.
Core Principle
"Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior." — Martin Fowler
Structural Changes Only
Allowed:
- Renaming for clarity
- Extracting functions/methods
- Removing duplication
- Simplifying expressions
- Moving files/modules between directories
- Reorganizing module boundaries
NOT allowed:
- Adding new functionality
- Fixing bugs (if you find one, write a failing test first)
- Changing observable behavior in any way
See library-application-criteria.md for public API constraints (libraries vs applications).
Refactoring Workflow
- Ensure tests pass (you need a safety net)
- Identify ONE smell to address
- Choose appropriate pattern from
patterns.md - Apply mechanically in small steps
- Run tests after each step
- Commit when green
Safety Rules
- Never refactor with failing tests
- One pattern at a time
- Commit after each successful refactor
- Revert immediately if tests fail