complexity
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseComplexity 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 -10Step 2: Detect Language
步骤2:检测编程语言
bash
undefinedbash
undefinedCheck 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"
undefinedls .go **/.go 2>/dev/null | head -1 && echo "Go detected"
undefinedStep 3: Run Complexity Analysis
步骤3:运行复杂度分析
For Python (using radon):
bash
undefined针对Python(使用radon):
bash
undefinedCheck 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):**
```bashradon mi <path> -s
**针对Go(使用gocyclo):**
```bashCheck 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>
undefinedgocyclo -over 10 <path>
undefinedStep 4: Interpret Results
步骤4:解读结果
Cyclomatic Complexity Grades:
| Grade | CC Score | Meaning |
|---|---|---|
| A | 1-5 | Low risk, simple |
| B | 6-10 | Moderate, manageable |
| C | 11-20 | High risk, complex |
| D | 21-30 | Very high risk |
| F | 31+ | Untestable, refactor now |
圈复杂度等级:
| 等级 | CC分数 | 说明 |
|---|---|---|
| A | 1-5 | 低风险,代码简单 |
| B | 6-10 | 中等风险,易于维护 |
| C | 11-20 | 高风险,代码复杂 |
| D | 21-30 | 极高风险 |
| F | 31+ | 无法测试,立即重构 |
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>.mdmarkdown
undefined报告保存路径:
.agents/complexity/YYYY-MM-DD-<target>.mdmarkdown
undefinedComplexity 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)
| Function | File | CC | Recommendation |
|---|---|---|---|
| <name> | file:line | <score> | <how to simplify> |
| Function | File | CC | Recommendation |
|---|---|---|---|
| <name> | file:line | <score> | <how to simplify> |
High (CC 11-20)
High (CC 11-20)
| Function | File | CC | Recommendation |
|---|---|---|---|
| <name> | file:line | <score> | <how to simplify> |
| Function | File | CC | Recommendation |
|---|---|---|---|
| <name> | file:line | <score> | <how to simplify> |
Refactoring Recommendations
Refactoring Recommendations
- <Function>: <specific suggestion>
- Extract: <what to extract>
- Simplify: <how to simplify>
- <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
undefinedStep 7: Report to User
步骤7:向用户反馈结果
Tell the user:
- Overall complexity summary
- Number of functions over threshold
- Top 3 refactoring targets
- Location of full report
需告知用户以下内容:
- 整体复杂度概况
- 超出阈值的函数数量
- 排名前三的重构目标
- 完整报告的存储位置
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
简化高复杂度代码的方法:
- 提取辅助函数
- 用多态替代条件判断
- 使用提前返回
- 拆分长函数
- 简化嵌套循环