| name | refactor |
| description | Refactor code: extract functions, rename symbols, move modules, split files, inline variables, reduce duplication. Use when: (1) user asks to refactor, (2) code needs restructuring without behavior change, (3) extracting reusable components, (4) cleaning up after a feature is stable. NOT for: adding features (that's coding), fixing bugs (use debug), style-only changes. |
| when_to_use | Use when the user asks to refactor, restructure, extract, inline, rename, or reorganize code. |
| user-invocable | true |
| disable-model-invocation | false |
Refactor
Change structure without changing behavior. Prove it with tests.
Goal
Improve code organization, reduce duplication, or simplify complexity while keeping all existing behavior identical.
Workflow
- Run existing tests first. Establish the green baseline.
- Identify the refactoring type (see patterns below).
- Apply the refactoring in small, verifiable steps.
- Run tests after each step. If tests fail, the refactoring introduced a bug — fix it before continuing.
- Verify the final result compiles, tests pass, and behavior is unchanged.
Common Patterns
Extract Function
When: a block of code does one identifiable thing inside a larger function.
- Identify the block and its inputs/outputs.
- Create a new function with a descriptive name.
- Replace the block with a call to the new function.
- Verify: callers still behave identically.
Rename Symbol
When: a name is misleading, abbreviated, or doesn't match its purpose.
- Use
grep_search to find all references.
- Rename consistently across the codebase.
- Update docs, comments, and tests.
- Verify: build succeeds, tests pass.
Move Module / Split File
When: a file does too many things or a function belongs in a different package.
- Create the destination file/package.
- Move the code with its imports.
- Update all import paths.
- Verify: no circular dependencies, build succeeds.
Inline Variable / Function
When: an intermediate variable or trivial wrapper adds no clarity.
- Replace references with the inlined expression.
- Remove the now-unused declaration.
- Verify: behavior unchanged, readability improved.
Reduce Duplication
When: similar code appears in multiple places.
- Identify the common pattern and its variations.
- Extract a shared helper that handles the variations via parameters.
- Replace all duplicates with calls to the helper.
- Verify: all callers still work correctly.
Guardrails
- Never refactor and add features in the same step.
- If tests don't exist for the code being refactored, write them first.
- Keep the diff minimal — resist the urge to "improve" unrelated code.
- If the refactoring is large, break it into reviewable commits.