スキル一覧に戻る
rileyhilliard

refactoring-code

by rileyhilliard

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

SKILL.md


name: refactoring-code description: Improves code structure while preserving behavior through test verification. Use when cleaning up code, reducing duplication, simplifying complexity, or reorganizing modules.

Refactoring Code

Core principle: Refactoring changes structure, not functionality. If behavior changes, you're rewriting.

The Five Phases

Refactoring Progress:
- [ ] Phase 1: Understand current behavior
- [ ] Phase 2: Verify behavior-driven tests exist
- [ ] Phase 3: Identify issues
- [ ] Phase 4: Plan incremental steps
- [ ] Phase 5: Execute with continuous verification

Phase 1: Understand Current Behavior

  • Read code to understand purpose, not just implementation
  • Use Grep to find all call sites and consumers
  • Document inputs, outputs, side effects, edge cases

Phase 2: Verify Test Coverage (CRITICAL)

Tests must verify BEHAVIOR, not implementation:

// ✅ Behavior-driven - survives refactoring
test('displays error when API returns 404', async () => {
  server.use(http.get('/api/users', () => new HttpResponse(null, { status: 404 })));
  render(<UserList />);
  expect(await screen.findByText(/not found/i)).toBeInTheDocument();
});

// ❌ Implementation-detail - breaks during refactoring
test('sets error state', () => {
  wrapper.instance().handleError(new Error('404'));
  expect(wrapper.state('error')).toBe('404');
});

If tests are missing: Add behavior-driven tests first using Skill(ce:writing-tests).

Phase 3: Identify Issues

IssueIndicatorsFix
ComplexityDeep nesting, >50 line functionsExtract smaller functions
DuplicationCopy-pasted codeExtract shared utility
Poor namingx, data, tempRename to intent
Type gapsany types, assertionsAdd proper types

Phase 4: Plan Refactoring

  • Break into small, independently testable steps
  • High impact + low risk first (e.g., renames)
  • Defer high-risk changes (algorithm rewrites)

Phase 5: Execute & Verify

  1. Make one change at a time
  2. Run tests after each change
  3. Check TypeScript compilation
  4. Verify behavior unchanged

If something breaks: STOP. Use Skill(ce:systematic-debugging). Don't proceed until understood.

Red Flags - STOP and Reassess

  • Changing behavior while refactoring (separate concerns)
  • Skipping tests ("just this once")
  • Adding complexity to remove complexity
  • "This is clever!" (clarity > cleverness)
  • Abstracting before seeing pattern 3 times

Quick Reference

SmellRefactoring
Long functionExtract smaller functions
Duplicate codeExtract to shared utility
Deep nestingEarly returns, guard clauses
Magic numbersNamed constants
Large componentSplit into smaller components
Long parameter listParameter object

When to Stop

Stop when code is clear, duplication eliminated, types explicit, tests pass.

Don't continue if:

  • Making code "elegant" but harder to understand
  • Abstracting before 3 occurrences
  • Optimizing without measuring

For React/TypeScript patterns, see references/react-typescript.md.

スコア

総合スコア

65/100

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

SKILL.md

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

+20
LICENSE

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

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

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

+5
Issue管理

オープンIssueが50未満

+5
言語

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

+5
タグ

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

0/5

レビュー

💬

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