← 返回 Skills 市场
soponcd

Code Refactoring

作者 soponcd · GitHub ↗ · v1.0.0 · MIT-0
cross-platform ✓ 安全检测通过
383
总下载
0
收藏
0
当前安装
1
版本数
在 OpenClaw 中安装
/install code-refactoring
功能描述
Code refactoring patterns and techniques for improving code quality without changing behavior. Use for cleaning up legacy code, reducing complexity, or impro...
使用说明 (SKILL.md)

Code Refactoring

Refactoring Principles

When to Refactor

  • Before adding new features (make change easy, then make easy change)
  • After getting tests passing (red-green-refactor)
  • When you see code smells
  • During code review feedback

When NOT to Refactor

  • Without tests covering the code
  • Under tight deadlines with no safety net
  • Code that will be replaced soon
  • When you don't understand what the code does

Common Code Smells

Long Methods

// BEFORE: Method doing too much
function processOrder(order: Order) {
  // 100 lines of validation, calculation, notification, logging...
}

// AFTER: Extract into focused methods
function processOrder(order: Order) {
  validateOrder(order);
  const total = calculateTotal(order);
  saveOrder(order, total);
  notifyCustomer(order);
}

Deeply Nested Conditionals

// BEFORE: Arrow code
function getDiscount(user: User, order: Order) {
  if (user) {
    if (user.isPremium) {
      if (order.total > 100) {
        if (order.items.length > 5) {
          return 0.2;
        }
      }
    }
  }
  return 0;
}

// AFTER: Early returns (guard clauses)
function getDiscount(user: User, order: Order) {
  if (!user) return 0;
  if (!user.isPremium) return 0;
  if (order.total \x3C= 100) return 0;
  if (order.items.length \x3C= 5) return 0;
  return 0.2;
}

Primitive Obsession

// BEFORE: Primitives everywhere
function createUser(name: string, email: string, phone: string) {
  if (!email.includes('@')) throw new Error('Invalid email');
  // more validation...
}

// AFTER: Value objects
class Email {
  constructor(private value: string) {
    if (!value.includes('@')) throw new Error('Invalid email');
  }
  toString() { return this.value; }
}

function createUser(name: string, email: Email, phone: Phone) {
  // Email is already validated
}

Feature Envy

// BEFORE: Method uses another object's data extensively
function calculateShipping(order: Order) {
  const address = order.customer.address;
  const weight = order.items.reduce((sum, i) => sum + i.weight, 0);
  const distance = calculateDistance(address.zip);
  return weight * distance * 0.01;
}

// AFTER: Move method to where the data is
class Order {
  calculateShipping() {
    return this.totalWeight * this.customer.shippingDistance * 0.01;
  }
}

Refactoring Techniques

Extract Method

// Identify a code block that does one thing
// Move it to a new method with a descriptive name
// Replace original code with method call

function printReport(data: ReportData) {
  // Extract this block...
  const header = `Report: ${data.title}\
Date: ${data.date}\
${'='.repeat(40)}`;
  console.log(header);

  // ...into a method
  printHeader(data);
}

Replace Conditional with Polymorphism

// BEFORE: Switch on type
function getArea(shape: Shape) {
  switch (shape.type) {
    case 'circle': return Math.PI * shape.radius ** 2;
    case 'rectangle': return shape.width * shape.height;
    case 'triangle': return shape.base * shape.height / 2;
  }
}

// AFTER: Polymorphic classes
interface Shape {
  getArea(): number;
}

class Circle implements Shape {
  constructor(private radius: number) {}
  getArea() { return Math.PI * this.radius ** 2; }
}

class Rectangle implements Shape {
  constructor(private width: number, private height: number) {}
  getArea() { return this.width * this.height; }
}

Introduce Parameter Object

// BEFORE: Too many parameters
function searchProducts(
  query: string,
  minPrice: number,
  maxPrice: number,
  category: string,
  inStock: boolean,
  sortBy: string,
  sortOrder: string
) { ... }

// AFTER: Parameter object
interface SearchParams {
  query: string;
  priceRange: { min: number; max: number };
  category?: string;
  inStock?: boolean;
  sort?: { by: string; order: 'asc' | 'desc' };
}

function searchProducts(params: SearchParams) { ... }

Replace Magic Numbers with Constants

// BEFORE
if (user.age >= 18 && order.total >= 50) {
  applyDiscount(order, 0.1);
}

// AFTER
const MINIMUM_AGE = 18;
const DISCOUNT_THRESHOLD = 50;
const STANDARD_DISCOUNT = 0.1;

if (user.age >= MINIMUM_AGE && order.total >= DISCOUNT_THRESHOLD) {
  applyDiscount(order, STANDARD_DISCOUNT);
}

Safe Refactoring Process

  1. Ensure tests exist - Write tests if they don't
  2. Make small changes - One refactoring at a time
  3. Run tests after each change - Catch regressions immediately
  4. Commit frequently - Easy to revert if something breaks
  5. Review the diff - Make sure behavior hasn't changed

Refactoring Checklist

  • Tests pass before starting
  • Each change is small and focused
  • Tests pass after each change
  • No behavior changes (only structure)
  • Code is more readable than before
  • Commit message explains the refactoring
安全使用建议
This skill appears to be a safe, coherent collection of refactoring patterns and processes. Before applying any automated or agent-suggested refactorings, make sure you have a working test suite and review diffs locally (or in a protected branch) and run tests — the guidance is educational, not an automated code changer. If you plan to allow an agent to modify repositories automatically, restrict that capability and require human review to avoid unintended changes.
功能分析
Type: OpenClaw Skill Name: code-refactoring Version: 1.0.0 The skill bundle is purely educational, providing documentation and examples for code refactoring patterns such as 'Extract Method' and 'Guard Clauses'. It contains no executable code, shell commands, or instructions that could lead to data exfiltration or unauthorized access (SKILL.md).
能力评估
Purpose & Capability
The name and description describe code refactoring patterns and the skill contains only refactoring principles, examples, and a safe refactoring process — no unrelated capabilities or requests are present.
Instruction Scope
SKILL.md stays within scope: it provides examples, techniques, and a checklist for refactoring. It does not instruct the agent to read files, access environment variables, send data to external endpoints, or perform system operations.
Install Mechanism
There is no install specification and no code files — this is instruction-only, so nothing is written to disk or downloaded during install.
Credentials
The skill declares no required environment variables, credentials, or config paths, which is proportionate for a documentation/instruction skill.
Persistence & Privilege
The skill is not marked always:true and does not request elevated persistence or modify other skills or system settings. It is user-invocable and may be invoked by the agent (default behavior), which is expected for skills.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install code-refactoring
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /code-refactoring 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
Initial release of the Code Refactoring skill. - Provides practical patterns and techniques for improving code quality without changing behavior. - Covers key refactoring principles, common code smells, and safe refactoring processes. - Includes TypeScript examples for common problems and solutions. - Lists checklists to ensure safe and effective code refactoring.
元数据
Slug code-refactoring
版本 1.0.0
许可证 MIT-0
累计安装 0
当前安装数 0
历史版本数 1
常见问题

Code Refactoring 是什么?

Code refactoring patterns and techniques for improving code quality without changing behavior. Use for cleaning up legacy code, reducing complexity, or impro... 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 383 次。

如何安装 Code Refactoring?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install code-refactoring」即可一键安装,无需额外配置。

Code Refactoring 是免费的吗?

是的,Code Refactoring 完全免费,采用 MIT-0 许可证,可自由下载、安装和使用。

Code Refactoring 支持哪些平台?

Code Refactoring 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Code Refactoring?

由 soponcd(@soponcd)开发并维护,当前版本 v1.0.0。

💬 留言讨论