Token导航 LogoToken导航TokenDH.com
研究检索需要联网github未标认证来源可访问许可证需确认审计通过

refactoringrefactoring 工具

Agent Skill

refactoring 用于查找、检索和筛选相关信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要根据关键词、任务场景或来源线索快速定位候选结果时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

371

周安装

15

GitHub Stars

32

下载量

116
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:refactoring(refactoring 工具)
来源仓库:https://github.com/cosmix/claude-loom
仓库路径:skills/refactoring
安装命令:
npx skills add https://github.com/cosmix/claude-loom --skill refactoring
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/cosmix/claude-loom --skill refactoring

简介

refactoring 提供代码重构模式识别与实施策略,用于提升代码可维护性。

  • 适用于提取方法、拆分模块、消除重复等常见重构任务场景。
  • 支持局部重构与跨文件结构优化,保留原有功能不变。
  • 建议在本地预览变更效果,并配合构建检查验证重构结果。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Refactoring

Overview

This skill focuses on improving code quality through systematic refactoring techniques. It identifies code smells and applies proven refactoring patterns to enhance maintainability while preserving functionality.

Use this skill when the user requests:

  • Code restructuring or cleanup
  • Reducing technical debt
  • Improving code organization
  • Applying design patterns
  • Breaking up large files or functions
  • Removing duplication
  • Modernizing legacy code
  • Improving testability

Scope Selection

When to use this skill:

  • Local refactoring (single module/file)
  • Pattern application (extract method, introduce interface)
  • Test refactoring and test suite improvements
  • Data pipeline restructuring
  • Safe refactoring with test coverage

Agent selection: Use senior-software-engineer (Opus) for all refactoring work. Use software-engineer (Sonnet) ONLY for mechanical renames or boilerplate following established patterns.

Instructions

1. Identify Refactoring Opportunities

Code Smells to Search For:

  • Long methods (>50 lines) or large classes (>300 lines)
  • Duplicated code blocks
  • High cyclomatic complexity
  • Magic numbers and string literals
  • Deep nesting (>3 levels)
  • Feature envy (method using data from another class)
  • Shotgun surgery (one change requires many edits)
  • Data clumps (same group of parameters)
  • Primitive obsession (using primitives instead of objects)

Use Grep/Glob to find patterns:

# Find long functions (rough heuristic)
rg "^(\s*)(def|function|fn|func)\s+\w+" --after-context=60

# Find duplicated code
rg --multiline "pattern.*\n.*pattern"

# Find magic numbers
rg "\b\d{2,}\b" --type=py --type=js --type=rs

2. Plan the Refactoring

Before starting:

  1. Verify test coverage - Run tests to establish baseline
  2. List all changes - Document sequence of refactorings
  3. Identify dependencies - What code depends on what you're changing?
  4. Plan rollback points - Where can you safely commit?
  5. Check for impact - Grep for references to functions/classes being changed

Red flags that require escalation:

  • No test coverage exists
  • Changes affect public APIs with external consumers
  • Unclear ownership or multiple teams involved
  • Performance-critical hot paths

3. Apply Refactoring Patterns

Code Organization Patterns

Extract Method/Function:

  • Break down long functions into smaller, focused ones
  • Each function should do one thing
  • Improves readability and testability

Extract Class:

  • Split large classes with multiple responsibilities
  • Follow Single Responsibility Principle
  • Improves cohesion and reduces coupling

Move Method/Field:

  • Relocate methods to classes that use their data
  • Reduces feature envy
  • Improves encapsulation

Inline Method/Variable:

  • Remove unnecessary indirection
  • Simplify overly abstracted code
  • Use when abstraction doesn't add value

Rename:

  • Improve naming clarity
  • Use domain language
  • Make intent explicit

Structural Patterns

Replace Conditional with Polymorphism:

  • Replace type switches with subclass methods
  • Enables Open/Closed Principle
  • Improves extensibility

Introduce Parameter Object:

  • Group related parameters into objects
  • Reduces parameter lists
  • Makes data relationships explicit

Replace Magic Numbers with Constants:

  • Define named constants for literals
  • Improves readability and maintainability
  • Centralizes configuration

Decompose Conditional:

  • Extract complex conditions into named functions
  • Replace nested ifs with guard clauses
  • Improves readability

Test Refactoring Patterns

Extract Test Fixture:

  • Move common setup into fixture/factory
  • Reduces duplication in test files
  • Improves test maintainability

Introduce Test Data Builder:

  • Replace complex object construction with builders
  • Makes test intent clearer
  • Simplifies test setup

Replace Assertion Roulette:

  • Use descriptive assertion messages
  • One logical assertion per test
  • Clear failure messages

Extract Test Helper:

  • Move repeated test logic into helpers
  • Keep tests focused on behavior
  • Improves test readability

Data Pipeline Patterns

Extract Transformation:

  • Isolate data transformation logic
  • Make transformations composable
  • Improves testability

Introduce Pipeline Interface:

  • Define standard input/output contracts
  • Enable stage composition
  • Simplifies testing and debugging

Replace Inline Processing with Stages:

  • Break monolithic processing into stages
  • Each stage has single responsibility
  • Enables parallelization and monitoring

4. Safe Refactoring Workflow

For each refactoring step:

  1. Make the change - One refactoring at a time
  2. Run tests - Verify behavior preserved
  3. Commit - Create checkpoint with clear message
  4. Repeat - Move to next refactoring

If tests fail:

  • Revert immediately (git checkout)
  • Analyze failure - is it a test issue or behavior change?
  • Fix or adjust approach

Commit message format:

refactor: extract calculate_discount from process_order

Improves testability by isolating discount logic.
No behavior change.

5. Verify Changes

Verification checklist:

  • All tests pass (existing + any new tests)
  • No regressions in functionality
  • Performance hasn't degraded (for hot paths)
  • Code coverage maintained or improved
  • Linting passes
  • Build succeeds

For large refactorings:

  • Run additional smoke tests
  • Check memory usage (if applicable)
  • Review error handling preservation

Best Practices

  1. Small Steps: Make incremental changes, not big bang rewrites
  2. Test First: Ensure tests exist before refactoring - write them if missing
  3. One Thing at a Time: Focus on single refactoring per commit
  4. Preserve Behavior: External behavior must remain unchanged
  5. Keep It Working: Code should pass tests after each step
  6. Document Intent: Explain why refactoring was needed in commits
  7. Refactor Tests Too: Keep tests clean and maintainable
  8. Use Type Safety: Leverage type systems to catch errors early
  9. Measure Don't Guess: Profile before optimizing performance
  10. Know When to Stop: Don't over-engineer or add unnecessary abstraction

Common Patterns with Examples

Pattern 1: Extract Method

# Before: Long method with multiple responsibilities
def process_order(order):
    # Validate order
    if not order.items:
        raise ValueError("Empty order")
    if order.total < 0:
        raise ValueError("Invalid total")

    # Calculate discount
    discount = 0
    if order.customer.is_premium:
        discount = order.total * 0.1
    if order.total > 1000:
        discount += order.total * 0.05

    # Apply discount and save
    order.final_total = order.total - discount
    order.save()

# After: Extracted methods with single responsibility
def process_order(order):
    validate_order(order)
    discount = calculate_discount(order)
    finalize_order(order, discount)

def validate_order(order):
    if not order.items:
        raise ValueError("Empty order")
    if order.total < 0:
        raise ValueError("Invalid total")

def calculate_discount(order) -> float:
    discount = 0
    if order.customer.is_premium:
        discount = order.total * 0.1
    if order.total > 1000:
        discount += order.total * 0.05
    return discount

def finalize_order(order, discount: float):
    order.final_total = order.total - discount
    order.save()

Pattern 2: Replace Magic Numbers with Constants

// Before
if (response.status === 200) {
  setTimeout(retry, 3000);
  if (attempts > 5) {
    throw new Error("Max retries exceeded");
  }
}

// After
const HTTP_OK = 200;
const RETRY_DELAY_MS = 3000;
const MAX_RETRY_ATTEMPTS = 5;

if (response.status === HTTP_OK) {
  setTimeout(retry, RETRY_DELAY_MS);
  if (attempts > MAX_RETRY_ATTEMPTS) {
    throw new Error("Max retries exceeded");
  }
}

Pattern 3: Replace Nested Conditionals with Guard Clauses

# Before
def get_payment_amount(employee):
    if employee.is_active:
        if employee.is_full_time:
            if employee.tenure > 5:
                return employee.salary * 1.1
            else:
                return employee.salary
        else:
            return employee.hourly_rate * employee.hours
    else:
        return 0

# After
def get_payment_amount(employee):
    if not employee.is_active:
        return 0

    if not employee.is_full_time:
        return employee.hourly_rate * employee.hours

    if employee.tenure > 5:
        return employee.salary * 1.1

    return employee.salary

Pattern 4: Introduce Parameter Object

// Before
function createUser(
  firstName: string,
  lastName: string,
  email: string,
  phone: string,
  street: string,
  city: string,
  state: string,
  zip: string,
) {
  // ...
}

// After
interface UserDetails {
  name: PersonName;
  contact: ContactInfo;
  address: Address;
}

interface PersonName {
  first: string;
  last: string;
}

interface ContactInfo {
  email: string;
  phone: string;
}

interface Address {
  street: string;
  city: string;
  state: string;
  zip: string;
}

function createUser(details: UserDetails) {
  // ...
}

Pattern 5: Replace Type Code with Polymorphism

// Before
enum ShapeType { Circle, Rectangle, Triangle }

struct Shape {
    shape_type: ShapeType,
    radius: f64,
    width: f64,
    height: f64,
    base: f64,
}

impl Shape {
    fn area(&self) -> f64 {
        match self.shape_type {
            ShapeType::Circle => 3.14159 * self.radius * self.radius,
            ShapeType::Rectangle => self.width * self.height,
            ShapeType::Triangle => 0.5 * self.base * self.height,
        }
    }
}

// After
trait Shape {
    fn area(&self) -> f64;
}

struct Circle { radius: f64 }
struct Rectangle { width: f64, height: f64 }
struct Triangle { base: f64, height: f64 }

impl Shape for Circle {
    fn area(&self) -> f64 {
        3.14159 * self.radius * self.radius
    }
}

impl Shape for Rectangle {
    fn area(&self) -> f64 {
        self.width * self.height
    }
}

impl Shape for Triangle {
    fn area(&self) -> f64 {
        0.5 * self.base * self.height
    }
}

Pattern 6: Extract Data Pipeline Stage

# Before: Monolithic processing
def process_data(raw_data):
    # Validate
    if not raw_data:
        raise ValueError("Empty data")

    # Clean
    cleaned = []
    for item in raw_data:
        if item.get('status') == 'valid':
            cleaned.append(item)

    # Transform
    transformed = []
    for item in cleaned:
        transformed.append({
            'id': item['id'],
            'value': item['raw_value'] * 100,
            'timestamp': item['ts']
        })

    # Aggregate
    total = sum(item['value'] for item in transformed)
    return {'items': transformed, 'total': total}

# After: Pipeline with composable stages
def validate_input(raw_data):
    if not raw_data:
        raise ValueError("Empty data")
    return raw_data

def filter_valid(data):
    return [item for item in data if item.get('status') == 'valid']

def transform_items(data):
    return [
        {
            'id': item['id'],
            'value': item['raw_value'] * 100,
            'timestamp': item['ts']
        }
        for item in data
    ]

def aggregate_results(transformed):
    total = sum(item['value'] for item in transformed)
    return {'items': transformed, 'total': total}

def process_data(raw_data):
    return (raw_data
            |> validate_input
            |> filter_valid
            |> transform_items
            |> aggregate_results)

Pattern 7: Simplify Test with Builder

// Before
func TestUserRegistration(t *testing.T) {
    user := &User{
        FirstName: "John",
        LastName: "Doe",
        Email: "john@example.com",
        Phone: "555-0100",
        Address: Address{
            Street: "123 Main St",
            City: "Springfield",
            State: "IL",
            Zip: "62701",
        },
        Preferences: Preferences{
            NewsletterEnabled: true,
            Theme: "dark",
        },
        CreatedAt: time.Now(),
    }

    err := RegisterUser(user)
    assert.NoError(t, err)
}

// After
func TestUserRegistration(t *testing.T) {
    user := NewUserBuilder().
        WithName("John", "Doe").
        WithEmail("john@example.com").
        Build()

    err := RegisterUser(user)
    assert.NoError(t, err)
}

// Test builder focuses on what matters for each test
func TestUserWithNewsletter(t *testing.T) {
    user := NewUserBuilder().WithNewsletter(true).Build()
    // ...
}

When to Stop Refactoring

Stop if:

  • Tests start failing unexpectedly
  • Scope is expanding beyond initial plan
  • Unsure about architectural implications
  • Performance concerns arise
  • Security implications unclear

In these cases:

  • Commit current progress
  • Document remaining work
  • Use senior-software-engineer or relevant specialist to continue

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

37.62%
按下载量换算44

Claude

30.39%
按下载量换算35

Cursor

19.38%
按下载量换算22

Gemini CLI

9.88%
按下载量换算11

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills