code-simplifier
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSkill: Code Simplifier
Skill:代码简化器
Category: Code Quality
Version: 1.0.0
Rule Reference:
rules/kiss-avoid-over-engineering.md分类: 代码质量
版本: 1.0.0
规则参考:
rules/kiss-avoid-over-engineering.mdOverview
概述
Detect and simplify overly complex code. Apply KISS principle (Keep It Simple, Stupid).
Philosophy: The best code is code you don't have to write. Less is more.
Full Guide: Read for comprehensive patterns and examples.
rules/kiss-avoid-over-engineering.md检测并简化过于复杂的代码。应用KISS原则(Keep It Simple, Stupid,即保持简单)。
理念: 最好的代码是无需编写的代码。少即是多。
完整指南: 阅读获取全面的模式和示例。
rules/kiss-avoid-over-engineering.mdQuick Reference
快速参考
| Signal | Action |
|---|---|
| Deep nesting (>3 levels) | Flatten with early returns |
| Long function (>30 lines) | Extract smaller functions |
| Complex conditionals | Use lookup tables |
| Over-abstraction | Inline single-use code |
| Premature optimization | Remove unless profiled |
| 信号 | 操作 |
|---|---|
| 深层嵌套(超过3级) | 使用提前返回扁平化 |
| 过长函数(超过30行) | 提取为更小的函数 |
| 复杂条件判断 | 使用查找表 |
| 过度抽象 | 内联单次使用的代码 |
| 过早优化 | 除非经过性能分析否则移除 |
Complexity Targets
复杂度目标
| Metric | Target |
|---|---|
| Cyclomatic complexity | ≤10 |
| Nesting depth | ≤3 |
| Function length | ≤30 lines |
| File length | ≤300 lines |
| Parameters | ≤3 |
| 指标 | 目标值 |
|---|---|
| 圈复杂度 | ≤10 |
| 嵌套深度 | ≤3 |
| 函数长度 | ≤30行 |
| 文件长度 | ≤300行 |
| 参数数量 | ≤3 |
Simplification Checklist
简化检查清单
Before writing or reviewing code, ask:
- Can I delete this? - Unused code, dead branches
- Can I inline this? - Single-use abstractions
- Can I flatten this? - Nested conditions, callbacks
- Can I use built-ins? - Array methods, standard library
- Is this needed now? - YAGNI (You Ain't Gonna Need It)
- Would a junior understand? - If not, simplify
在编写或评审代码前,自问:
- 我能否删除这段代码? - 未使用的代码、无效分支
- 我能否内联这段代码? - 仅单次使用的抽象
- 我能否扁平化这段代码? - 嵌套条件、回调函数
- 我能否使用内置功能? - 数组方法、标准库
- 现在是否需要这段代码? - YAGNI(You Ain't Gonna Need It,即你不会需要它)
- 初级开发者能理解吗? - 如果不能,就简化
Commands
命令
| Command | Purpose |
|---|---|
| Analyze and simplify a file |
| Check complexity metrics |
| Full complexity report |
| 命令 | 用途 |
|---|---|
| 分析并简化文件 |
| 检查复杂度指标 |
| 生成完整复杂度报告 |
Example Session
示例会话
User: This function is too complex, simplify it
Claude: Let me analyze the complexity...
Complexity Analysis:
- Cyclomatic complexity: 15 (target: ≤10)
- Nesting depth: 5 (target: ≤3)
- Lines: 87 (target: ≤30)
Simplification Plan:
1. Convert nested ifs to early returns (-3 nesting)
2. Extract validation logic to separate function (-20 lines)
3. Replace switch with lookup table (-15 lines, -5 complexity)
4. Remove unused error handling branch
Applying changes...
Result:
- Cyclomatic complexity: 6 ✓
- Nesting depth: 2 ✓
- Lines: 28 ✓用户:这个函数太复杂了,简化它
Claude:让我分析一下复杂度...
复杂度分析:
- 圈复杂度:15(目标值:≤10)
- 嵌套深度:5(目标值:≤3)
- 行数:87(目标值:≤30)
简化方案:
1. 将嵌套if转换为提前返回(减少3级嵌套)
2. 提取验证逻辑为独立函数(减少20行)
3. 用查找表替换switch语句(减少15行,降低5点复杂度)
4. 移除未使用的错误处理分支
应用修改中...
结果:
- 圈复杂度:6 ✓
- 嵌套深度:2 ✓
- 行数:28 ✓Related Resources
相关资源
- Full KISS Guide:
rules/kiss-avoid-over-engineering.md - Complexity Command:
commands/quality/complexity.md - Refactor Workflow:
commands/refactor.md - Code Reviewer:
skills/code-reviewer/SKILL.md
Remember: Simple code is not dumb code. It takes skill to write simple code.
Version: 1.0.0
- 完整KISS指南:
rules/kiss-avoid-over-engineering.md - 复杂度命令:
commands/quality/complexity.md - 重构工作流:
commands/refactor.md - 代码评审工具:
skills/code-reviewer/SKILL.md
谨记: 简洁的代码并非拙劣的代码。编写简洁的代码需要技巧。
版本: 1.0.0