/refactor
Restructure code for clarity and maintainability without changing behavior.
Usage
/refactor <target> [--strategy <strategy>] [--safe]
What It Does
Analyzes the target code, identifies improvement opportunities, plans a safe refactoring sequence, executes it, and verifies no behavior changes. Runs tests before and after every transformation.
Implementation
- Analyze -- read the target code, identify code smells (long functions, deep nesting, duplication, poor naming, high coupling).
- Plan -- propose refactoring strategy: extract function, rename, decompose, inline, introduce pattern.
- Baseline -- run tests to establish green state. If tests fail, stop.
- Execute -- apply refactorings one at a time. Run tests after each.
- Verify -- confirm all tests still pass, behavior unchanged.
- Report -- diff summary, complexity reduction metrics, before/after.
Strategies: extract, rename, decompose, simplify, decouple, auto (detect best).
Examples
# Auto-detect and refactor
/refactor lib/optimal_engine/intake.ex
# Extract functions from a long module
/refactor lib/app.ex --strategy extract
# Safe mode: smaller steps, test after every change
/refactor lib/complex_module.ex --safe