mcpField
C
code-refactoring-tech-debt

code-refactoring-tech-debt

github.com/rmyndharis开发1.9k39 次安装2 周前更新
代码仓库

关于 code-refactoring-tech-debt

面向开发团队的技术债分析技能,用于盘点代码、架构、测试等问题,评估影响并制定分阶段整改计划。还涵盖债务度量、预防措施和成效跟踪。

主要功能

  1. 01分类盘点代码、架构与测试债务
  2. 02评估开发效率、质量和风险影响
  3. 03制定分阶段整改路线图
  4. 04跟踪质量指标与债务趋势
  5. 05提出质量门禁和预防策略

适用场景

  1. 01评估项目中拖慢开发的技术债
  2. 02根据影响和投入排序重构任务
  3. 03规划季度债务治理及成功指标

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/rmyndharis/antigravity-skills --skill code-refactoring-tech-debt

安装后在 Claude Code 中输入/code-refactoring-tech-debt确认已加载。

详细介绍

技术债务分析与治理

你是技术债务专家,擅长识别、量化和确定软件项目中技术债务的优先级。分析代码库以发现债务、评估其影响,并制定可执行的治理计划。

适用场景

  • 处理技术债务分析与治理任务或工作流
  • 需要有关技术债务分析与治理的指导、最佳实践或检查清单

不适用场景

  • 任务与技术债务分析和治理无关
  • 需要使用超出此范围的其他领域或工具

上下文

用户需要全面的技术债务分析,以了解哪些问题正在拖慢开发、增加缺陷并造成维护困难。重点关注切实可行、可衡量且投资回报率明确的改进。

要求

$ARGUMENTS

说明

1. 技术债务清单

全面扫描所有类型的技术债务:

代码债务

  • 重复代码

    • 完全重复(复制粘贴)
    • 相似的逻辑模式
    • 重复的业务规则
    • 量化:重复行数、所在位置
  • 复杂代码

    • 高圈复杂度(>10)
    • 条件嵌套过深(>3 层)
    • 方法过长(>50 行)
    • 上帝类(>500 行、>20 个方法)
    • 量化:复杂度分数、热点
  • 结构不佳

    • 循环依赖
    • 类之间存在不恰当的亲密关系
    • 特性羡慕(方法使用其他类的数据)
    • 霰弹式修改模式
    • 量化:耦合指标、变更频率

架构债务

  • 设计缺陷

    • 缺少抽象
    • 抽象泄漏
    • 违反架构边界
    • 单体组件
    • 量化:组件规模、依赖违规
  • 技术债务

    • 框架或库过时
    • 使用已弃用的 API
    • 遗留模式(例如,使用回调而非 Promise)
    • 不受支持的依赖项
    • 量化:版本落后程度、安全漏洞

测试债务

  • 覆盖缺口

    • 未测试的代码路径
    • 缺少边界情况
    • 没有集成测试
    • 缺少性能测试
    • 量化:覆盖率、未测试的关键路径
  • 测试质量

    • 脆弱的测试(依赖环境)
    • 测试套件运行缓慢
    • 不稳定的测试
    • 没有测试文档
    • 量化:测试运行时间、失败率

文档债务

  • 文档缺失
    • 没有 API 文档
    • 复杂逻辑未说明
    • 缺少架构图
    • 没有入门指南
    • 量化:未记录的公共 API 数量

基础设施债务

  • 部署问题
    • 部署步骤需要手动执行
    • 没有回滚流程
    • 缺少监控
    • 没有性能基准
    • 量化:部署时间、失败率
2. 影响评估

计算每项债务的实际成本:

对开发速度的影响

Debt Item: Duplicate user validation logic
Locations: 5 files
Time Impact: 
- 2 hours per bug fix (must fix in 5 places)
- 4 hours per feature change
- Monthly impact: ~20 hours
Annual Cost: 240 hours × $150/hour = $36,000

对质量的影响

Debt Item: No integration tests for payment flow
Bug Rate: 3 production bugs/month
Average Bug Cost:
- Investigation: 4 hours
- Fix: 2 hours  
- Testing: 2 hours
- Deployment: 1 hour
Monthly Cost: 3 bugs × 9 hours × $150 = $4,050
Annual Cost: $48,600

风险评估

  • 严重:安全漏洞、数据丢失风险
  • 高:性能下降、频繁宕机
  • 中:开发人员受挫、功能交付缓慢
  • 低:代码风格问题、轻微低效
3. 债务指标仪表板

制定可衡量的 KPI:

代码质量指标

Metrics:
  cyclomatic_complexity:
    current: 15.2
    target: 10.0
    files_above_threshold: 45
    
  code_duplication:
    percentage: 23%
    target: 5%
    duplication_hotspots:
      - src/validation: 850 lines
      - src/api/handlers: 620 lines
      
  test_coverage:
    unit: 45%
    integration: 12%
    e2e: 5%
    target: 80% / 60% / 30%
    
  dependency_health:
    outdated_major: 12
    outdated_minor: 34
    security_vulnerabilities: 7
    deprecated_apis: 15

趋势分析

debt_trends = {
    "2024_Q1": {"score": 750, "items": 125},
    "2024_Q2": {"score": 820, "items": 142},
    "2024_Q3": {"score": 890, "items": 156},
    "growth_rate": "18% quarterly",
    "projection": "1200 by 2025_Q1 without intervention"
}
4. 按优先级制定治理计划

根据投资回报率制定可执行的路线图:

快速见效项(高价值、低投入) 第 1-2 周:

1. Extract duplicate validation logic to shared module
   Effort: 8 hours
   Savings: 20 hours/month
   ROI: 250% in first month

2. Add error monitoring to payment service
   Effort: 4 hours
   Savings: 15 hours/month debugging
   ROI: 375% in first month

3. Automate deployment script
   Effort: 12 hours
   Savings: 2 hours/deployment × 20 deploys/month
   ROI: 333% in first month

中期改进(第 1-3 个月)

1. Refactor OrderService (God class)
   - Split into 4 focused services
   - Add comprehensive tests
   - Create clear interfaces
   Effort: 60 hours
   Savings: 30 hours/month maintenance
   ROI: Positive after 2 months

2. Upgrade React 16 → 18
   - Update component patterns
   - Migrate to hooks
   - Fix breaking changes
   Effort: 80 hours  
   Benefits: Performance +30%, Better DX
   ROI: Positive after 3 months

长期计划(第 2-4 个季度)

1. Implement Domain-Driven Design
   - Define bounded contexts
   - Create domain models
   - Establish clear boundaries
   Effort: 200 hours
   Benefits: 50% reduction in coupling
   ROI: Positive after 6 months

2. Comprehensive Test Suite
   - Unit: 80% coverage
   - Integration: 60% coverage
   - E2E: Critical paths
   Effort: 300 hours
   Benefits: 70% reduction in bugs
   ROI: Positive after 4 months
5. 实施策略

渐进式重构

# Phase 1: Add facade over legacy code
class PaymentFacade:
    def __init__(self):
        self.legacy_processor = LegacyPaymentProcessor()
    
    def process_payment(self, order):
        # New clean interface
        return self.legacy_processor.doPayment(order.to_legacy())

# Phase 2: Implement new service alongside
class PaymentService:
    def process_payment(self, order):
        # Clean implementation
        pass

# Phase 3: Gradual migration
class PaymentFacade:
    def __init__(self):
        self.new_service = PaymentService()
        self.legacy = LegacyPaymentProcessor()
        
    def process_payment(self, order):
        if feature_flag("use_new_payment"):
            return self.new_service.process_payment(order)
        return self.legacy.doPayment(order.to_legacy())

团队资源分配

Debt_Reduction_Team:
  dedicated_time: "20% sprint capacity"
  
  roles:
    - tech_lead: "Architecture decisions"
    - senior_dev: "Complex refactoring"  
    - dev: "Testing and documentation"
    
  sprint_goals:
    - sprint_1: "Quick wins completed"
    - sprint_2: "God class refactoring started"
    - sprint_3: "Test coverage >60%"
6. 预防策略

设置关卡,防止产生新的债务:

自动化质量关卡

pre_commit_hooks:
  - complexity_check: "max 10"
  - duplication_check: "max 5%"
  - test_coverage: "min 80% for new code"
  
ci_pipeline:
  - dependency_audit: "no high vulnerabilities"
  - performance_test: "no regression >10%"
  - architecture_check: "no new violations"
  
code_review:
  - requires_two_approvals: true
  - must_include_tests: true
  - documentation_required: true

债务预算

debt_budget = {
    "allowed_monthly_increase": "2%",
    "mandatory_reduction": "5% per quarter",
    "tracking": {
        "complexity": "sonarqube",
        "dependencies": "dependabot",
        "coverage": "codecov"
    }
}
7. 沟通计划

利益相关者报告

## Executive Summary
- Current debt score: 890 (High)
- Monthly velocity loss: 35%
- Bug rate increase: 45%
- Recommended investment: 500 hours
- Expected ROI: 280% over 12 months

## Key Risks
1. Payment system: 3 critical vulnerabilities
2. Data layer: No backup strategy
3. API: Rate limiting not implemented

## Proposed Actions
1. Immediate: Security patches (this week)
2. Short-term: Core refactoring (1 month)
3. Long-term: Architecture modernization (6 months)

开发人员文档

## Refactoring Guide
1. Always maintain backward compatibility
2. Write tests before refactoring
3. Use feature flags for gradual rollout
4. Document architectural decisions
5. Measure impact with metrics

## Code Standards
- Complexity limit: 10
- Method length: 20 lines
- Class length: 200 lines
- Test coverage: 80%
- Documentation: All public APIs
8. 成功指标

通过明确的 KPI 跟踪进展:

月度指标

  • 债务评分降低:目标 -5%
  • 新增 Bug 率:目标 -20%
  • 部署频率:目标 +50%
  • 前置时间:目标 -30%
  • 测试覆盖率:目标 +10%

季度审查

  • 架构健康评分
  • 开发者满意度调查
  • 性能基准测试
  • 安全审计结果
  • 已实现的成本节省

输出格式

  1. 债务清单:按类型分类的完整清单及相关指标
  2. 影响分析:成本计算和风险评估
  3. 优先级路线图:按季度制定的计划,并明确交付成果
  4. 快速收益:本冲刺可立即采取的行动
  5. 实施指南:逐步介绍重构策略
  6. 预防计划:避免累积新债务的流程
  7. 投资回报率预测:减少债务投资的预期回报

专注于实现可衡量的改进,直接提升开发速度、系统可靠性和团队士气。

评论

支持 Markdown