complexity

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Complexity Skill

Complexity Skill

YOU MUST EXECUTE THIS WORKFLOW. Do not just describe it.
Analyze code complexity to identify refactoring targets.
你必须执行此工作流,而不只是描述它。
分析代码复杂度以确定重构目标。

Execution Steps

执行步骤

Given
/complexity [path]
:
当输入指令
/complexity [路径]
时:

Step 1: Determine Target

步骤1:确定目标路径

If path provided: Use it directly.
If no path: Use current directory or recent changes:
bash
git diff --name-only HEAD~5 2>/dev/null | grep -E '\.(py|go)$' | head -10
若提供路径: 直接使用该路径。
若未提供路径: 使用当前目录或最近的变更文件:
bash
git diff --name-only HEAD~5 2>/dev/null | grep -E '\.(py|go)$' | head -10

Step 2: Detect Language

步骤2:检测编程语言

bash
undefined
bash
undefined

Check for Python files

Check for Python files

ls .py **/.py 2>/dev/null | head -1 && echo "Python detected"
ls .py **/.py 2>/dev/null | head -1 && echo "Python detected"

Check for Go files

Check for Go files

ls .go **/.go 2>/dev/null | head -1 && echo "Go detected"
undefined
ls .go **/.go 2>/dev/null | head -1 && echo "Go detected"
undefined

Step 3: Run Complexity Analysis

步骤3:运行复杂度分析

For Python (using radon):
bash
undefined
针对Python(使用radon):
bash
undefined

Check if radon is installed

Check if radon is installed

which radon || pip install radon
which radon || pip install radon

Run cyclomatic complexity

Run cyclomatic complexity

radon cc <path> -a -s
radon cc <path> -a -s

Run maintainability index

Run maintainability index

radon mi <path> -s

**For Go (using gocyclo):**
```bash
radon mi <path> -s

**针对Go(使用gocyclo):**
```bash

Check if gocyclo is installed

Check if gocyclo is installed

which gocyclo || go install github.com/fzipp/gocyclo/cmd/gocyclo@latest
which gocyclo || go install github.com/fzipp/gocyclo/cmd/gocyclo@latest

Run complexity analysis

Run complexity analysis

gocyclo -over 10 <path>
undefined
gocyclo -over 10 <path>
undefined

Step 4: Interpret Results

步骤4:解读结果

Cyclomatic Complexity Grades:
GradeCC ScoreMeaning
A1-5Low risk, simple
B6-10Moderate, manageable
C11-20High risk, complex
D21-30Very high risk
F31+Untestable, refactor now
圈复杂度等级:
等级CC分数说明
A1-5低风险,代码简单
B6-10中等风险,易于维护
C11-20高风险,代码复杂
D21-30极高风险
F31+无法测试,立即重构

Step 5: Identify Refactor Targets

步骤5:确定重构目标

List functions/methods that need attention:
  • CC > 10: Should refactor
  • CC > 20: Must refactor
  • CC > 30: Critical, immediate action
列出需要关注的函数/方法:
  • CC > 10:应当重构
  • CC > 20:必须重构
  • CC > 30:严重问题,立即处理

Step 6: Write Complexity Report

步骤6:撰写复杂度报告

Write to:
.agents/complexity/YYYY-MM-DD-<target>.md
markdown
undefined
报告保存路径:
.agents/complexity/YYYY-MM-DD-<target>.md
markdown
undefined

Complexity Report: <Target>

Complexity Report: <Target>

Date: YYYY-MM-DD Language: <Python/Go> Files Analyzed: <count>
Date: YYYY-MM-DD Language: <Python/Go> Files Analyzed: <count>

Summary

Summary

  • Average CC: <score>
  • Highest CC: <score> in <function>
  • Functions over threshold: <count>
  • Average CC: <score>
  • Highest CC: <score> in <function>
  • Functions over threshold: <count>

Refactor Targets

Refactor Targets

Critical (CC > 20)

Critical (CC > 20)

FunctionFileCCRecommendation
<name>file:line<score><how to simplify>
FunctionFileCCRecommendation
<name>file:line<score><how to simplify>

High (CC 11-20)

High (CC 11-20)

FunctionFileCCRecommendation
<name>file:line<score><how to simplify>
FunctionFileCCRecommendation
<name>file:line<score><how to simplify>

Refactoring Recommendations

Refactoring Recommendations

  1. <Function>: <specific suggestion>
    • Extract: <what to extract>
    • Simplify: <how to simplify>
  1. <Function>: <specific suggestion>
    • Extract: <what to extract>
    • Simplify: <how to simplify>

Next Steps

Next Steps

  • Address critical complexity first
  • Create issues for high complexity
  • Consider refactoring sprint
undefined
  • Address critical complexity first
  • Create issues for high complexity
  • Consider refactoring sprint
undefined

Step 7: Report to User

步骤7:向用户反馈结果

Tell the user:
  1. Overall complexity summary
  2. Number of functions over threshold
  3. Top 3 refactoring targets
  4. Location of full report
需告知用户以下内容:
  1. 整体复杂度概况
  2. 超出阈值的函数数量
  3. 排名前三的重构目标
  4. 完整报告的存储位置

Key Rules

核心规则

  • Use the right tool - radon for Python, gocyclo for Go
  • Focus on high CC - prioritize 10+
  • Provide specific fixes - not just "refactor this"
  • Write the report - always produce artifact
  • 使用正确工具 - Python用radon,Go用gocyclo
  • 聚焦高CC值 - 优先处理CC≥10的代码
  • 提供具体修复方案 - 不只是说“重构此处”
  • 撰写报告 - 必须生成报告文件

Quick Reference

快速参考

Simplifying High Complexity:
  • Extract helper functions
  • Replace conditionals with polymorphism
  • Use early returns
  • Break up long functions
  • Simplify nested loops
简化高复杂度代码的方法:
  • 提取辅助函数
  • 用多态替代条件判断
  • 使用提前返回
  • 拆分长函数
  • 简化嵌套循环