All tools
SkillCurated · reviewed
Refactor Skill
Updated Jul 7, 2026
A Claude Code skill that performs targeted, behavior-preserving refactors: extract function, rename symbol, deduplicate logic, or split an oversized module. Every transform is listed individually with a one-line rationale so each step is independently reviewable.
What it does
- /refactor
Analyze the selected or pasted code and apply behavior-preserving refactors with a step-by-step change log.
- /refactor <target>
Refactor a named function, class, or file; optionally specify the transform type (extract, rename, dedupe, split).
Files (1)
SKILL.mdprimary · markdown · 3.5 KB
# Refactor Skill
---
slug: refactor-skill
version: 1.0.0
category: engineering
command: /refactor
---
## What it does
Performs targeted, behavior-preserving refactors on a function, file, or module.
Applies one or more transforms: extract function, inline function, rename symbol,
deduplicate repeated logic, or split a module at a clear boundary. Observable
behavior is never changed — only structure.
## Trigger
Use this skill when asked to refactor, clean up, or restructure code.
Typical invocations:
- "Refactor this function to extract the validation logic"
- "Clean up this module — it's doing too much"
- `/refactor` in Claude Code
- `/refactor <target>` where target is a function name, file, or concern
## Input
Provide one or more of:
1. The code to refactor (pasted directly or referenced by file path)
2. The specific transform wanted ("extract function", "rename X to Y", "split module")
3. The motivation ("this function is 300 lines and handles three unrelated concerns")
If no code is provided, ask for it before proceeding.
## Method
Work in this order:
1. **Identify smells** — long functions, duplicated blocks, mixed abstraction levels, names that mislead.
2. **Plan the transforms** — list each step before applying ("1. Extract lines 42–67 into validateInput()").
3. **Apply one transform at a time** — do not bundle unrelated edits.
4. **Verify behavior is unchanged** — note side effects, shared state, or external callers that must update.
### Transform rules
- Extract function: only when the block has a single clear purpose and a name you can give it.
- Rename: rename every usage site visible in the provided code; flag external callers you cannot see.
- Deduplicate: extract into a shared helper only when the duplication is substantial (not two uses of three lines).
- Split module: propose the new boundary with a rationale before splitting; never split without a clear separation of concerns.
## Output format
Produce the refactored code, then a **Changes** section:
```
## Changes
1. Extracted <block> into `functionName()` — <one-line reason>.
2. Renamed `oldName` → `newName` — <one-line reason>.
3. ...
```
Flag any step that touches shared state or changes exports with a **Risk** note on the same line.
## Example output
```typescript
// split 80-line processOrder() into three focused functions
function validateOrder(order: Order): void {
if (!order.items.length) throw new Error('Order has no items');
if (order.total < 0) throw new Error('Negative total');
}
function applyDiscount(order: Order, code: string): Order {
const rate = DISCOUNT_CODES[code] ?? 0;
return { ...order, total: order.total * (1 - rate) };
}
async function persistOrder(order: Order): Promise<string> {
const { data, error } = await supabase.from('orders').insert(order).select('id').single();
if (error) throw error;
return data.id;
}
```
## Changes
1. Extracted lines 12–19 into `validateOrder()` — single responsibility; now unit-testable in isolation.
2. Extracted lines 31–37 into `applyDiscount()` — pure function, no side effects.
3. Extracted supabase block into `persistOrder()` — isolates async error handling.
4. Updated two call sites in `checkout.ts` to call the three functions in sequence. **Risk**: if `processOrder` is exported, the export name must be updated by callers outside this file.
## Commands (Claude Code)
- `/refactor` — Analyze the current selection or pasted code and apply behavior-preserving refactors.
- `/refactor <target>` — Refactor a named function, class, or file; optionally specify the transform type.