スキル一覧に戻る
mrzacsmith

refactor

by mrzacsmith

3🍴 1📅 2026年1月21日
GitHubで見るManusで実行

SKILL.md


name: Refactor description: Safe code refactoring with proper testing and incremental changes triggers:

  • /refactor
  • refactor this
  • clean up code
  • restructure allowed-tools:
  • Read
  • Write
  • Edit
  • Glob
  • Grep
  • Bash(npm run test*)
  • Bash(pnpm test*)
  • Bash(yarn test*)
  • Bash(npm run lint*)
  • Bash(git diff*)
  • Bash(git status*)

Refactor Skill

Safely restructure code without changing behavior.

Golden Rule

Refactoring changes HOW code works, not WHAT it does.

All tests should pass before AND after refactoring.

Process

1. Ensure Test Coverage

Before refactoring, verify tests exist:

# Run existing tests
npm run test

# Check coverage for the code you're refactoring
npm run test -- --coverage src/module-to-refactor/

If coverage is low, add tests first before refactoring.

2. Make Small, Incremental Changes

Each step should be:

  • A single, focused change
  • Independently committable
  • Passing all tests

Bad: One massive commit that changes everything Good: Series of small commits, each improving the code

3. Run Tests After Each Change

# Quick feedback loop
npm run test -- --watch src/module/

4. Commit Frequently

git commit -m "refactor(auth): extract token validation to separate function"
git commit -m "refactor(auth): rename validateToken to verifyJWT"
git commit -m "refactor(auth): move JWT logic to dedicated service"

Common Refactoring Patterns

Extract Function

When: Code block does one specific thing

// Before
function processOrder(order) {
  // 20 lines calculating total
  // 15 lines validating inventory
  // 10 lines sending notifications
}

// After
function processOrder(order) {
  const total = calculateTotal(order);
  validateInventory(order.items);
  sendOrderNotifications(order);
}

Extract Module/Class

When: Related functions should be grouped

// Before: utils.ts with 50 functions

// After:
// date-utils.ts - date formatting functions
// string-utils.ts - string manipulation
// validation-utils.ts - validators

Rename for Clarity

When: Names don't describe purpose

// Before
const d = new Date();
function proc(x) { ... }

// After
const createdAt = new Date();
function processPayment(transaction) { ... }

Replace Conditionals with Polymorphism

When: Switch/if chains based on type

// Before
function calculateArea(shape) {
  if (shape.type === 'circle') return Math.PI * shape.radius ** 2;
  if (shape.type === 'rectangle') return shape.width * shape.height;
}

// After
class Circle {
  calculateArea() { return Math.PI * this.radius ** 2; }
}
class Rectangle {
  calculateArea() { return this.width * this.height; }
}

Remove Duplication (DRY)

When: Same code appears in multiple places

// Before: Same validation in 3 files

// After: Shared validation utility
import { validateEmail } from '@/utils/validation';

Simplify Conditionals

When: Complex boolean logic

// Before
if (user && user.subscription && user.subscription.active && !user.banned) {
  // ...
}

// After
function canAccessPremiumContent(user) {
  if (!user) return false;
  if (user.banned) return false;
  return user.subscription?.active ?? false;
}

if (canAccessPremiumContent(user)) {
  // ...
}

Refactoring Checklist

  • Tests pass before starting
  • Identified specific improvements to make
  • Making one change at a time
  • Running tests after each change
  • Committing after each successful change
  • Not adding new features (refactor only)
  • Not fixing bugs (separate concern)

Warning Signs to Stop

  • Tests start failing unexpectedly
  • Scope is growing beyond original plan
  • You're tempted to "also fix this bug"
  • Changes are getting hard to explain

When in doubt, commit what you have and reassess.

Code Smells to Look For

SmellRefactoring
Long functionExtract functions
Long parameter listIntroduce parameter object
Duplicate codeExtract shared function
Large classSplit into focused classes
Feature envyMove method to appropriate class
Primitive obsessionCreate domain types
Deep nestingExtract functions, early returns

スコア

総合スコア

45/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

0/5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です