| name | refactor |
| description | Refactor code to improve structure, readability, or performance without changing external behavior. Use when the user wants to clean up or restructure existing code safely. |
Refactor
Refactor the specified code while preserving its behavior.
Steps
-
Understand the target — Read the file(s) or function(s) to refactor. Ask the user to clarify the goal if not clear (e.g., "reduce duplication", "split large function", "improve naming").
-
Identify the scope — Determine what other code depends on the target (callers, imports, tests). Run a grep/search if needed.
-
Plan the change — Describe what will be refactored and how, before touching any code. Get confirmation if the change is non-trivial.
-
Apply the refactor — Make the changes. Common patterns:
- Extract function / extract variable
- Rename for clarity
- Remove duplication (DRY)
- Simplify conditionals
- Split large file or class
- Replace magic values with named constants
-
Verify behavior is preserved:
- Run existing tests: use the project's test command (check
package.json, Makefile, etc.)
- If no tests exist, note this and offer to add them
- Do a quick manual check if the change is UI or API-facing
-
Report — Summarize what was changed and why, and confirm tests pass.
Rules
- Do not change external interfaces or function signatures without explicit user approval.
- Prefer small, incremental changes over large rewrites.
- If a refactor would require updating many call sites, show the plan first.