refactoring-specialist

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Refactoring Specialist

重构专家

Purpose

目标

Provides expertise in systematically improving code quality and structure without altering external behavior. Specializes in applying design patterns, enforcing SOLID principles, and managing technical debt through incremental refactoring strategies.
提供系统化提升代码质量与结构的专业能力,同时不改变代码的外部行为。擅长应用设计模式、遵循SOLID原则,并通过增量式重构策略管理技术债务。

When to Use

使用场景

  • Improving code readability and maintainability
  • Reducing code duplication and complexity
  • Applying design patterns to solve structural problems
  • Breaking apart monolithic classes or functions
  • Introducing proper abstraction layers
  • Preparing codebase for new feature development
  • Migrating legacy code to modern patterns
  • Establishing anti-corruption layers between systems
  • 提升代码的可读性与可维护性
  • 减少代码重复与复杂度
  • 应用设计模式解决结构问题
  • 拆分臃肿的类或函数
  • 引入合适的抽象层
  • 为新功能开发准备代码库
  • 将遗留代码迁移至现代模式
  • 在系统间建立防腐层

Quick Start

快速开始

Invoke this skill when:
  • Improving code readability and maintainability
  • Reducing code duplication and complexity
  • Applying design patterns to solve structural problems
  • Breaking apart monolithic classes or functions
  • Introducing proper abstraction layers
Do NOT invoke when:
  • Adding new features (refactor first, then add) → use appropriate domain skill
  • Debugging runtime errors → use debugger
  • Reviewing code for security issues → use security-auditor
  • Optimizing performance bottlenecks → use performance-engineer
在以下场景调用此技能:
  • 提升代码的可读性与可维护性
  • 减少代码重复与复杂度
  • 应用设计模式解决结构问题
  • 拆分臃肿的类或函数
  • 引入合适的抽象层
请勿在以下场景调用:
  • 添加新功能(应先重构,再添加)→ 使用相应的领域技能
  • 调试运行时错误 → 使用调试工具
  • 审查代码安全问题 → 使用安全审计技能
  • 优化性能瓶颈 → 使用性能工程师技能

Decision Framework

决策框架

Code Smell Detected?
├── Duplication → Extract Method/Class, Template Method pattern
├── Long Method → Extract Method, Decompose Conditional
├── Large Class → Extract Class, Single Responsibility
├── Feature Envy → Move Method to appropriate class
├── Primitive Obsession → Introduce Value Objects
├── Shotgun Surgery → Move related changes together
└── Divergent Change → Split by responsibility
检测到代码异味?
├── 重复代码 → 提取方法/类、模板方法模式
├── 过长方法 → 提取方法、分解条件
├── 过大类 → 提取类、单一职责原则
├── 特性依恋 → 将方法移至合适的类
├── 基本类型偏执 → 引入值对象
├── 霰弹式修改 → 集中相关修改
└── 发散式变化 → 按职责拆分

Core Workflows

核心工作流程

1. Safe Refactoring Cycle

1. 安全重构周期

  1. Ensure comprehensive test coverage exists
  2. Identify specific code smell to address
  3. Apply smallest possible refactoring step
  4. Run tests to verify behavior unchanged
  5. Commit the change
  6. Repeat until smell eliminated
  1. 确保存在全面的测试覆盖
  2. 确定需要解决的具体代码异味
  3. 应用最小的重构步骤
  4. 运行测试以验证行为未改变
  5. 提交变更
  6. 重复直至消除代码异味

2. Pattern Introduction

2. 模式引入流程

  1. Identify recurring structural problem
  2. Select appropriate design pattern
  3. Create new structure alongside existing code
  4. Migrate consumers incrementally
  5. Remove old implementation
  6. Document pattern usage for team
  1. 识别重复出现的结构问题
  2. 选择合适的设计模式
  3. 在现有代码旁创建新结构
  4. 逐步迁移使用者
  5. 移除旧实现
  6. 为团队记录模式使用方式

3. Anti-Corruption Layer Implementation

3. 防腐层实现流程

  1. Identify boundary between systems/domains
  2. Define clean interface for internal domain
  3. Create adapter/translator layer
  4. Route all cross-boundary calls through ACL
  5. Evolve internal model independently
  1. 识别系统/领域间的边界
  2. 为内部领域定义清晰的接口
  3. 创建适配器/转换层
  4. 所有跨边界调用均通过ACL路由
  5. 独立演进内部模型

Best Practices

最佳实践

  • Always refactor with tests as a safety net
  • Make small, incremental changes with frequent commits
  • Refactor before adding new features, not during
  • Use IDE refactoring tools for mechanical transformations
  • Document why patterns were applied, not just what
  • Prioritize refactoring by business value and risk
  • 始终以测试作为安全网进行重构
  • 进行小而增量的变更,频繁提交
  • 在添加新功能前重构,而非期间
  • 使用IDE重构工具完成机械性转换
  • 记录应用模式的原因,而非仅记录内容
  • 根据业务价值和风险确定重构优先级

Anti-Patterns

反模式

  • Big Bang refactoring → Use incremental strangler pattern
  • Refactoring without tests → Add characterization tests first
  • Over-engineering → Apply patterns only when needed
  • Refactoring during feature work → Separate refactoring commits
  • Ignoring team conventions → Align with existing codebase style
  • 大爆炸式重构 → 使用增量式扼杀者模式替代
  • 无测试重构 → 先添加特征测试
  • 过度设计 → 仅在需要时应用模式
  • 在功能开发期间重构 → 将重构提交与功能提交分离
  • 忽视团队约定 → 与现有代码库风格保持一致