home / skills / shunsukehayashi / miyabi / refactor-helper

refactor-helper skill

/.claude/skills/refactor-helper

This skill guides safe, incremental refactoring to improve structure, performance, and maintainability without changing behavior.

npx playbooks add skill shunsukehayashi/miyabi --skill refactor-helper

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.7 KB
---
name: refactor-helper
description: Refactor code to improve quality, performance, and maintainability. Use when refactoring code, improving code structure, or modernizing legacy code.
allowed-tools: Bash, Read, Write, Grep, Glob
---

# Refactor Helper

**Version**: 1.0.0
**Purpose**: Safe, systematic code refactoring

---

## Triggers

| Trigger | Examples |
|---------|----------|
| Refactor | "refactor this", "リファクタリングして", "clean up code" |
| Improve | "improve this code", "コード改善" |
| Modernize | "update to modern syntax", "モダン化" |

---

## Refactoring Principles

### 1. Make Small Changes

```
✅ GOOD: One refactoring per commit
❌ BAD: Multiple unrelated changes in one commit
```

### 2. Ensure Tests Pass

```bash
# Before refactoring
npm test

# After each change
npm test
```

### 3. Keep Behavior Unchanged

```
Refactoring = Improving structure WITHOUT changing behavior
```

---

## Common Patterns

### Extract Function

```typescript
// Before
function processOrder(order: Order) {
  // validate
  if (!order.items.length) throw new Error('Empty');
  if (order.total < 0) throw new Error('Invalid total');

  // calculate
  const subtotal = order.items.reduce((s, i) => s + i.price, 0);
  const tax = subtotal * 0.1;
  const total = subtotal + tax;

  // save
  db.save({ ...order, total });
}

// After
function validateOrder(order: Order): void {
  if (!order.items.length) throw new Error('Empty');
  if (order.total < 0) throw new Error('Invalid total');
}

function calculateTotal(items: Item[]): number {
  const subtotal = items.reduce((s, i) => s + i.price, 0);
  const tax = subtotal * 0.1;
  return subtotal + tax;
}

function processOrder(order: Order) {
  validateOrder(order);
  const total = calculateTotal(order.items);
  db.save({ ...order, total });
}
```

### Replace Conditionals with Polymorphism

```typescript
// Before
function getPrice(type: string, base: number): number {
  switch (type) {
    case 'premium': return base * 0.8;
    case 'vip': return base * 0.7;
    default: return base;
  }
}

// After
interface PricingStrategy {
  calculate(base: number): number;
}

class RegularPricing implements PricingStrategy {
  calculate(base: number) { return base; }
}

class PremiumPricing implements PricingStrategy {
  calculate(base: number) { return base * 0.8; }
}
```

### Simplify Conditionals

```typescript
// Before
if (user !== null && user !== undefined && user.isActive === true) {
  if (user.role === 'admin' || user.role === 'moderator') {
    // ...
  }
}

// After
const isActiveUser = user?.isActive ?? false;
const hasPrivileges = ['admin', 'moderator'].includes(user?.role ?? '');

if (isActiveUser && hasPrivileges) {
  // ...
}
```

### Remove Dead Code

```bash
# Find unused exports
npx ts-prune

# Find unused dependencies
npx depcheck
```

---

## Workflow

### Step 1: Identify Smell

| Code Smell | Refactoring |
|------------|-------------|
| Long function | Extract Function |
| Duplicate code | Extract and reuse |
| Complex conditionals | Simplify/Polymorphism |
| God class | Split responsibilities |
| Feature envy | Move method |

### Step 2: Write Tests (if missing)

```typescript
describe('processOrder', () => {
  it('should calculate total correctly', () => {
    const order = { items: [{ price: 100 }] };
    expect(processOrder(order).total).toBe(110);
  });
});
```

### Step 3: Refactor

Small, incremental changes with tests after each.

### Step 4: Verify

```bash
npm test
npm run lint
npm run typecheck
```

---

## Checklist

- [ ] Tests exist and pass before refactoring
- [ ] Each change is small and focused
- [ ] Tests pass after each change
- [ ] No behavior changes
- [ ] Code is more readable
- [ ] Commit with clear message

Overview

This skill refactor-helper automates and guides safe, systematic code refactoring for TypeScript projects. It focuses on improving code quality, performance, and maintainability while preserving behavior. Use it to modernize legacy code, break up large functions, and apply common refactoring patterns reliably.

How this skill works

refactor-helper inspects code for common smells (long functions, duplicate logic, complex conditionals, god classes, dead code) and proposes small, focused changes such as extract-function, replace-conditionals-with-polymorphism, and simplifications. It enforces a workflow: identify smells, add or verify tests, perform incremental refactors, and run verification steps (tests, lint, typechecks). It also suggests tooling commands and concrete code examples tailored to TypeScript.

When to use it

  • Modernizing legacy TypeScript code to newer syntax or patterns
  • Breaking up large functions or classes to improve readability
  • Removing dead code and unused dependencies safely
  • Consolidating duplicated logic or simplifying complex conditionals
  • Preparing code for performance or maintainability improvements

Best practices

  • Make one small refactor per commit to keep changes reviewable
  • Ensure tests exist and pass before starting any refactor
  • Run tests, lint, and type checks after each incremental change
  • Preserve external behavior — refactor structure, not logic
  • Use automated tools (ts-prune, depcheck) to find dead code and unused deps

Example use cases

  • Extracting validation and calculation helpers from a long order-processing function
  • Replacing switch-based pricing logic with strategy classes to enable extension
  • Simplifying nested user checks using optional chaining and array includes
  • Removing unused exports and dependencies discovered by automated scans
  • Adding focused unit tests before refactoring a critical module

FAQ

Will refactors change runtime behavior?

No — the goal is to keep behavior unchanged. Always add or run tests to verify behavior remains identical.

Which tools does this recommend for TypeScript?

Use npm test for unit tests, npm run lint and npm run typecheck for verification, and tools like ts-prune and depcheck to detect dead code and unused deps.