agentleFS
rui66648rui66648/agents-dev-rules/skills/refactoring/SKILL.md

refactoring

Skill0 starsChanged 12 days agoApache-2.0View on GitHub

需要对现有代码进行重构时使用。遵循系统化的重构流程,确保重构过程安全可控,不引入回归问题。触发词: 重构、整理代码、refactor、优化结构。

69 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use ci-cd, logging and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • JohnJBorenSkill

    ci-cd

    CI/CD pipeline design, GitHub Actions, testing automation, and deployment strategies

    Also in 5 projects that use this

  • diego-tobalinaSkill

    logging

    Logging best practices for Java (SLF4J/Logback) and Node.js (Winston/Pino). Use for structured logging, log levels, and avoiding sensitive data exposure.

    Also in 4 projects that use this

  • AWSSkill

    planning

    Discovers user intent and generates a structured, step-by-step plan for model customization workflows. This skill must always be activated alongside any other skill when the user's request relates to model customization — including fine-tuning, training, building, customizing, reviewing data, or getting advice on approach, regardless of domain. Do not skip this skill even if the immediate ask is narrow (e.g., reviewing data format or a single workflow step), because planning discovers the full scope of work needed. Also activate when the user wants to resume, continue, or modify an existing plan.

    Also in 8 projects that use this

  • dkprogSkill

    dependency-management

    Apply when wiring dependencies, integrating external services, or designing interfaces between modules. Ensures loose coupling through dependency injection, interface segregation, and proper abstraction boundaries.

    Also in 3 projects that use this

  • javalenciacaiSkill

    multi-agent-orchestration

    Design and coordinate multi-agent systems where specialized agents work together to solve complex problems. Covers agent communication, task delegation, workflow orchestration, and result aggregation. Use when building coordinated agent teams, complex workflows, or systems requiring specialized expertise across domains.

    Also in 3 projects that use this

  • harrissohail6Skill

    database-design

    Database schema design and SQL best practices. Use when designing tables, writing queries, or optimizing database performance.

    Also in 5 projects that use this

The file

Files
SKILL.md

Refactoring

负责代理

architect — 代码重构由架构师代理统一推进,覆盖后端、前端、数据库等多层;各层开发代理在其领域内执行重构,架构师负责方案与回归控制。

何时使用

  • 需要改善代码质量、可读性或性能时
  • 需要消除代码重复(DRY 原则)时
  • 需要提高代码可测试性时
  • 需要调整代码结构以适应新需求时
  • 代码圈复杂度过高需要拆分时

触发关键词: "重构"、"优化代码"、"简化"、"消除重复"、"提高可读性"

执行流程

Phase 1: 评估与规划

目标: 理解当前代码状态,制定重构计划。

  1. 阅读代码: 完整理解待重构代码的功能、依赖关系和测试覆盖
  2. 识别问题: 使用以下标准评估代码质量:
    • 代码重复度(DRY 原则)
    • 圈复杂度(Cyclomatic Complexity)
    • 命名清晰度
    • 函数长度和职责单一性
    • 测试覆盖率
  3. 制定计划: 输出重构计划,包含:
    • 重构目标和范围
    • 具体变更项(按优先级排序)
    • 风险评估和回滚策略
    • 测试验证方案

Phase 2: 准备测试

目标: 确保重构过程中有足够的测试保护。

  1. 检查现有测试: 确认现有测试覆盖待重构代码的核心逻辑
  2. 补充测试: 为未覆盖的路径添加测试用例
  3. 运行测试: 确保所有测试通过,作为重构的基线

Phase 3: 执行重构

目标: 逐步实施重构,每一步都保持代码可运行。

遵循以下原则:

原则说明
小步快跑每次只做一个小改动,确保测试通过后再进行下一步
保持功能不变重构不应改变任何外部可见的行为
先抽象后重构对于复杂重构,先提取抽象层再修改实现
保留测试重构过程中测试必须始终通过

常用重构模式:

  1. 提取函数: 将重复代码或复杂逻辑提取为独立函数
  2. 提取类: 将相关函数和数据封装为类
  3. 参数化: 将硬编码值改为参数
  4. 拆分函数: 将过长的函数拆分为多个职责单一的函数
  5. 合并重复代码: 将相似的函数合并为通用函数
  6. 引入接口: 使用抽象接口解耦依赖

Phase 4: 验证

目标: 确认重构后代码质量提升且无回归问题。

  1. 运行完整测试套件: 确保所有测试通过
  2. 运行 Lint: 确保代码符合规范
  3. 运行类型检查: 确保类型安全
  4. 代码审查: 邀请其他开发者审查重构结果
  5. 性能测试: 如果涉及性能优化,验证性能改善

Phase 5: 文档更新

目标: 更新相关文档以反映代码变更。

  1. 更新函数/类的 Docstring
  2. 更新 API 文档(如适用)
  3. 添加重构说明(如适用)

硬性规则

  • ✅ 重构前必须运行测试 — 确保基线稳定
  • ✅ 每次改动后必须运行测试 — 防止引入回归
  • ✅ 重构不应改变外部行为 — 保持接口兼容性
  • ✅ 测试覆盖率不应下降 — 重构后覆盖率应保持或提高
  • ✅ 必须有回滚计划 — 准备好回滚方案以防重构失败
  • ❌ 禁止一次性重构所有代码 — 分阶段逐步进行
  • ❌ 禁止在无测试保护的情况下重构 — 测试是安全网
  • ❌ 禁止跳过验证直接提交重构 — 必须验证后才能合并
  • ❌ 禁止重构过程中添加新功能 — 专注于质量改善

反模式

  • ❌ "一次性重构整个模块" — 改动过大,风险过高
  • ❌ 重构时同时添加新功能 — 难以定位问题来源
  • ❌ 忽略测试覆盖直接重构 — 引入回归后难以定位
  • ❌ 不保留历史版本直接修改 — 无法回滚
  • ❌ 重构后不更新文档 — 文档与代码不一致

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.