Skip to content
cccskills
FreePublished on GitHub

refactor

安全重构代码技能。当用户明确提到"重构"、"代码异味"或需要在TDD循环的Refactor阶段改进代码内部结构时使用。

The English summary is being prepared. Showing the original description.

See how to install

Files included (4)

  • SKILL.md6.8 KB
  • examples.md16.9 KB
  • references/code-smells.md8.3 KB
  • references/techniques.md9.4 KB

SKILL.md (original)

Review the instructions the agent will receive before you install.

安全重构

核心原则

重构不改变外部行为,只改进内部结构。测试必须始终通过。

重构是改善既有代码设计的过程,通过一系列小的、保持行为的变换来提高代码质量。每个变换都称为一个"重构"。

何时重构

在 TDD 循环中

  • 每个 Red-Green 循环后都应检查是否需要重构
  • 不要等到代码"完全烂掉"才重构
  • 小步重构,频繁重构

重构信号(Code Smells)

快速识别代码需要重构的信号:

1. 重复代码

相同的代码片段出现在多个地方 → 提取公共函数

2. 函数过长

函数超过 20-30 行,包含多个抽象层次 → 提取小函数

3. 魔法数字

代码中出现未命名的数字 → 使用命名常量

4. 参数过多

函数参数超过 3-4 个 → 引入参数对象

5. 深层嵌套

条件嵌套超过 3 层 → 使用卫语句(Guard Clauses)

💡 详细说明:参见 references/code-smells.md

重构技术速查

1. 提取函数(Extract Function)

何时使用:函数过长,代码片段可以被独立命名

// 重构前
func PrintOwing(invoice *Invoice) {
    printBanner()
    outstanding := 0.0
    for _, order := range invoice.Orders {
        outstanding += order.Amount
    }
    fmt.Printf("未付金额: %.2f\n", outstanding)
}

// 重构后
func PrintOwing(invoice *Invoice) {
    printBanner()
    outstanding := calculateOutstanding(invoice)
    fmt.Printf("未付金额: %.2f\n", outstanding)
}

2. 内联函数(Inline Function)

何时使用:函数体和函数名一样清晰

// 重构前
func getRating(driver *Driver) int {
    return moreThanFiveLateDeliveries(driver) ? 2 : 1
}

// 重构后
func getRating(driver *Driver) int {
    return driver.NumberOfLateDeliveries > 5 ? 2 : 1
}

3. 提取变量(Extract Variable)

何时使用:表达式难以理解

// 重构前
func Price(order *Order) float64 {
    return order.Quantity*order.ItemPrice -
        max(0, order.Quantity-500)*order.ItemPrice*0.05
}

// 重构后
func Price(order *Order) float64 {
    basePrice := order.Quantity * order.ItemPrice
    quantityDiscount := max(0, order.Quantity-500) * order.ItemPrice * 0.05
    return basePrice - quantityDiscount
}

4. 重命名(Rename)

何时使用:命名不能准确表达意图

// 重构前
func calc(u *User) float64 { ... }

// 重构后
func calculateTotalOrderAmount(user *User) float64 { ... }

5. 卫语句(Guard Clauses)

何时使用:深层嵌套的条件

// 重构前:嵌套条件
func GetPayAmount(employee *Employee) float64 {
    var result float64
    if employee.IsSeparated {
        result = 0
    } else {
        if employee.IsRetired {
            result = 0
        } else {
            result = employee.Salary
        }
    }
    return result
}

// 重构后:卫语句
func GetPayAmount(employee *Employee) float64 {
    if employee.IsSeparated {
        return 0
    }
    if employee.IsRetired {
        return 0
    }
    return employee.Salary
}

💡 详细技术:参见 references/techniques.md

标准重构流程

步骤 1:确保测试通过

go test ./... -v

所有测试必须是绿色的才能开始重构

步骤 2:进行小的重构

  • 一次只做一个小改动
  • 例如:只重命名一个变量,或只提取一个函数

步骤 3:运行测试

go test ./... -v

确保重构没有破坏功能

步骤 4:提交(可选)

git add .
git commit -m "refactor: 提取 validateEmail 函数"

步骤 5:重复步骤 2-4

继续下一个小的重构

重构检查清单

每次重构后检查:

  • 所有测试都通过
  • 代码更易读
  • 没有引入新的复杂性
  • 没有改变外部行为
  • 函数/变量命名更清晰
  • 消除了重复代码
  • 降低了耦合度

重构原则

DO(应该做)

✓ 小步重构 - 每次只改一个地方 ✓ 频繁测试 - 每次改动后都运行测试 ✓ 保持绿灯 - 重构过程中测试必须始终通过 ✓ 改善命名 - 好的命名是最好的文档 ✓ 消除重复 - DRY (Don't Repeat Yourself) ✓ 简化逻辑 - 能用简单方法就不用复杂方法

DON'T(不应该做)

✗ 不要同时重构和添加功能 - 一次只做一件事 ✗ 不要在红灯时重构 - 测试失败时先让测试通过 ✗ 不要大规模重构 - 避免一次改动太多代码 ✗ 不要盲目重构 - 确保重构有明确目的 ✗ 不要过度设计 - 不要为未来可能不会发生的需求重构

输出格式

重构时输出:

♻️  重构:[重构内容简述]
   原因:[为什么需要重构]
   技术:[使用的重构技术]

✓ 运行测试
   结果:PASS(X个测试,耗时 Yms)

✓ 重构完成
   改进:[具体改进说明]

示例输出

♻️  重构:提取邮箱验证逻辑
   原因:RegisterUser 和 UpdateUserEmail 中存在重复的验证代码
   技术:Extract Function

   提取前:2 处重复,共 8 行代码
   提取后:1 个函数 validateEmail,被 2 处调用

✓ 运行测试
   命令:go test ./internal/user -v
   结果:PASS
   覆盖率:85.2%

✓ 重构完成
   改进:
   - 消除了 8 行重复代码
   - 提高了可维护性(邮箱验证逻辑集中在一处)
   - 测试覆盖率保持不变

何时停止重构

满足以下条件即可停止当前重构:

  1. ✓ 代码清晰易读,意图明确
  2. ✓ 没有明显的代码异味
  3. ✓ 函数职责单一,长度适中(< 30 行)
  4. ✓ 没有重复代码
  5. ✓ 命名准确描述了意图
  6. ✓ 所有测试通过

记住:重构是持续的过程,不要追求一次性完美。每个 TDD 循环做一点改进即可。

更多资源

📚 完整示例

真实场景的重构案例:

📖 详细参考

深入理解重构:

📖 参考资料

  • 《重构:改善既有代码的设计》- Martin Fowler
  • TDD 循环 (tdd-cycle skill)
  • 测试优先 (test-first skill)
  • SOLID 原则

Reviews

No reviews yet. Tell others what you thought.

Comments

No login needed. Share how it worked for you, ask a question, or note where it did not run.

No comments yet. Be the first to write one.

Comments are published right away. Do not include personal information such as email addresses or phone numbers. Up to 2 URLs.

0 / 1,000 characters

This form is protected by reCAPTCHA, and the Google Privacy Policy and Terms of Service apply.

More skills from this repository

arch

Free

架构师工作技能 - 架构设计、文档管理、语义化版本控制、设计审查。当用户提到架构、设计文档、版本管理、技术选型、系统设计、数据模型、API设计、文档审查、设计规范、架构决策、或需要创建/更新设计文档时,必须使用此技能。确保所有设计文档遵循语义化版本规范和命名约定。

The English summary is being prepared. Showing the original description.

linview/agent-team-archetype22Updated Aug 9, 2026

commit

Free

代码提交与 MR 创建技能 - 自动生成语义化 commit message、创建符合规范的 GitLab Merge Request、验证飞书工作项关联。当用户提到 Git 提交、commit、push、推送代码、创建 MR、创建 PR、合并请求、或需要提交代码、推送代码、创建 MR/PR 时,必须使用此技能。支持交互式(对话)和非交互式(参数)两种模式。

The English summary is being prepared. Showing the original description.

linview/agent-team-archetype22Updated Aug 9, 2026

dev

Free

开发工作流程指导 - 编码、测试、代码质量、MR/PR 创建和 CI/CD。用于开发任务、编码、功能实现、Bug 修复、单元测试、代码覆盖率、代码审查、CI/CD 流水线和 Git 操作。

The English summary is being prepared. Showing the original description.

linview/agent-team-archetype22Updated Aug 9, 2026

devops

Free

DevOps 工作技能 - CI/CD 流程、容器化构建、Kubernetes 部署、基础设施即代码、监控告警。当用户提到部署、容器化、K8s、Helm、ArgoCD、CI/CD、监控、日志、或需要执行部署、排查线上问题时,必须使用此技能。

The English summary is being prepared. Showing the original description.

linview/agent-team-archetype22Updated Aug 9, 2026

pm

Free

PM 编排技能 — 具备意图识别与动态路由能力的项目管理中枢。除了 pm 的全部 Story/Epic/Sprint 管理能力外,pm 能分析用户 prompt 的多领域意图,动态匹配所需的专业 skill(arch/dev/ued/qa/devops 等),生成编排计划并在用户确认后依次唤起各 skill 协同工作。当用户的请求涉及多个专业领域、需要跨 skill 协调、或者用户希望用一个 prompt 驱动完整的「设计→实现→验证」流程时,使用此技能。纯 Story 管理/迭代规划等单领域任务,pm 会直接处理而不路由。

The English summary is being prepared. Showing the original description.

linview/agent-team-archetype22Updated Aug 9, 2026

qa

Free

QA 工作技能 — 测试分层架构、UT/API/SIT/E2E/UAT 测试、交叉验证策略、测试数据管理、测试报告管理。当用户提到测试、QA、质量保证、回归测试、单元测试、集成测试、验收测试、测试覆盖率、pytest、go test、E2E、Playwright、monkey test、fuzz test、RPC 测试、测试用例设计、TDD、测试框架、测试策略审查、问题排查、测试环境、测试数据、SIT 交叉验证、或需要设计/执行/增强测试策略时,必须使用此技能。确保所有测试活动遵循分层架构和业务正确性验证原则。

The English summary is being prepared. Showing the original description.

linview/agent-team-archetype22Updated Aug 9, 2026

See all skills by linview

Report a problem with this skill