Refactor
Refactor the target for readability and structure without changing behavior.
/refactor[file or function]npx agentscamp add commands/refactorInstall to ~/.claude/commands/refactor.md
A slash command that refactors a file or function for readability and structure without changing behavior: it establishes a passing test baseline, targets concrete issues like vague names, duplication, deep nesting, and dead code, applies small verifiable steps re-running tests after each, and reports what changed and why.
Refactor $ARGUMENTS to improve readability, structure, and maintainability while keeping observable behavior exactly the same. If $ARGUMENTS is empty, ask which file or function to target before making any changes.
WARNING
This is a behavior-preserving refactor, not a rewrite. Do not add features, change public APIs, alter return values, or modify side effects. If you discover a genuine bug, stop and report it instead of silently "fixing" it.
1. Establish a baseline
Before touching anything, understand the current behavior and how it is verified.
- Read
$ARGUMENTSand the code that calls it. Note the public interface: function signatures, exported symbols, and any side effects (I/O, network, mutation, logging). - Find the relevant tests. Run them to confirm they pass before you start, so you have a known-good baseline.
# Adjust to the project's test runner
npm test # or: pytest, go test ./..., cargo testIf there are no tests covering the target, say so. Add a minimal characterization test that captures current behavior before refactoring, or ask the user how to proceed.
2. Identify what to improve
Look for concrete, well-known issues rather than stylistic preference:
- Naming — vague or misleading identifiers (
data,tmp,doStuff). - Duplication — repeated logic that can be extracted into one place.
- Long functions — units doing several jobs that should be split.
- Deep nesting — guard clauses and early returns can flatten control flow.
- Dead code — unused variables, unreachable branches, stale comments.
- Leaky structure — mixed levels of abstraction in one function.
3. Apply changes in small steps
Make one focused transformation at a time. Prefer many small, verifiable edits over one large rewrite.
A typical move is replacing nested conditionals with guard clauses:
// Before
function getDiscount(user) {
if (user) {
if (user.isActive) {
return user.isPremium ? 0.2 : 0.1;
}
}
return 0;
}
// After
function getDiscount(user) {
if (!user || !user.isActive) return 0;
return user.isPremium ? 0.2 : 0.1;
}After each meaningful step, re-run the tests so a regression is caught immediately and isolated to the change that caused it.
4. Verify behavior is unchanged
- Run the full test suite again; it must pass with no modifications to the assertions.
- Run the linter and type checker to confirm nothing was broken.
npm run lint
npm run build # or the project's typecheck commandNOTE
If a test had to change to keep passing, that means behavior changed. Revert and rethink, or surface the discrepancy to the user.
5. Summarize
Report back concisely:
- What changed — the specific refactorings applied (e.g. "extracted
validateInput, flattened nesting inparse"). - Why — the readability or structure problem each change addressed.
- Verification — that tests, lint, and types still pass.
- Follow-ups — anything out of scope you noticed (suspected bugs, missing test coverage) listed separately, not acted on.
Related
- Refactoring SpecialistUse this agent to safely restructure code without changing behavior — extracting, renaming, decoupling. Examples — breaking up a god object, removing duplication, improving testability.
- Dead Code FinderFind genuinely unused code — unreferenced exports, unreachable files, and unused dependencies — and remove it safely with build/test verification. Use when trimming a codebase or untangling years of accreted cruft.
- Extract FunctionExtract a code region into a well-named function and update the call site.
- Optimize ImportsRemove unused imports and organize the rest in a file or directory per the project's conventions — preferring the project's own import tool where one is configured — without changing behavior.
- Rename SymbolSafely rename a symbol project-wide, distinguishing the real symbol from coincidental substring matches.