スキル一覧に戻る
vanman2024

refactoring-patterns

by vanman2024

0🍴 0📅 2026年1月25日
GitHubで見るManusで実行

SKILL.md


name: Refactoring Patterns description: Common refactoring patterns and code improvement techniques. Use when refactoring code, improving structure, or when user mentions refactoring, code smells, or code quality improvements. allowed-tools: Read, Write, Edit, Bash

Refactoring Patterns

This skill provides common refactoring patterns, code smell detection, and structural improvement techniques.

What This Skill Provides

1. Code Smell Detection

  • Identify duplicate code
  • Detect complex functions
  • Find poor naming
  • Spot structural issues

2. Refactoring Patterns

  • Extract Method
  • Extract Class
  • Rename for Clarity
  • Simplify Conditionals
  • Remove Duplication

3. Structural Improvements

  • Module organization
  • Dependency management
  • Interface clarity
  • Separation of concerns

4. Refactoring Scripts

  • detect-duplicates.sh - Find duplicate code
  • complexity-check.sh - Measure function complexity
  • naming-analysis.sh - Analyze naming quality

Instructions

Detecting Code Smells

When analyzing code for refactoring opportunities:

  1. Long Methods (> 50 lines)

    • Break into smaller functions
    • Use Extract Method pattern
  2. Duplicate Code

    • Identify repeated patterns
    • Extract to shared utility
  3. Complex Conditionals

    • Simplify with guard clauses
    • Use polymorphism or strategy pattern
  4. Poor Naming

    • Use descriptive names
    • Follow naming conventions

Common Refactoring Patterns

Extract Method

Before:

function processOrder(order) {
  // validate
  if (!order.items) return false;
  if (!order.customer) return false;

  // calculate
  let total = 0;
  for (let item of order.items) {
    total += item.price * item.quantity;
  }

  // save
  db.save(order);
}

After:

function processOrder(order) {
  if (!validateOrder(order)) return false;
  const total = calculateTotal(order.items);
  saveOrder(order);
}

function validateOrder(order) {
  return order.items && order.customer;
}

function calculateTotal(items) {
  return items.reduce((sum, item) =>
    sum + (item.price * item.quantity), 0);
}

function saveOrder(order) {
  db.save(order);
}

Simplify Conditionals

Before:

if user.is_active and user.has_permission and not user.is_banned:
    allow_access()
else:
    deny_access()

After:

def can_access(user):
    return user.is_active and user.has_permission and not user.is_banned

if can_access(user):
    allow_access()
else:
    deny_access()

Extract Class

Before:

class User {
  name;
  email;
  street;
  city;
  zip;

  getAddress() {
    return `${this.street}, ${this.city} ${this.zip}`;
  }
}

After:

class Address {
  constructor(street, city, zip) {
    this.street = street;
    this.city = city;
    this.zip = zip;
  }

  toString() {
    return `${this.street}, ${this.city} ${this.zip}`;
  }
}

class User {
  name;
  email;
  address;

  constructor(name, email, address) {
    this.name = name;
    this.email = email;
    this.address = address;
  }
}

Refactoring Checklist

When refactoring code:

  • Preserve existing functionality (no behavior changes)
  • Run existing tests after each change
  • Improve readability and maintainability
  • Reduce complexity where possible
  • Maintain backward compatibility
  • Update documentation if structure changes
  • Add tests for new extracted methods

Complexity Metrics

Cyclomatic Complexity - Number of paths through code

  • 1-10: Simple, low risk
  • 11-20: Moderate, medium risk
  • 21-50: Complex, high risk
  • 50+: Very complex, very high risk

Target: Keep functions under complexity of 10

Best Practices

  • Refactor in small, incremental steps
  • Run tests after each refactoring
  • Commit refactorings separately from features
  • Don't change functionality during refactoring
  • Use descriptive names for extracted methods
  • Keep functions focused on single responsibility

Success Criteria

  • ✅ Code smells are identified and addressed
  • ✅ Functions are small and focused
  • ✅ Duplication is eliminated
  • ✅ Naming is clear and descriptive
  • ✅ Complexity is reduced
  • ✅ Tests still pass after refactoring

Plugin: 04-iterate Skill Type: Patterns + Analysis Auto-invocation: Yes (via description matching)

スコア

総合スコア

50/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
言語

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

+5
タグ

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

0/5

レビュー

💬

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