code-simplifier

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Skill: 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.md

Overview

概述

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
rules/kiss-avoid-over-engineering.md
for comprehensive patterns and examples.

检测并简化过于复杂的代码。应用KISS原则(Keep It Simple, Stupid,即保持简单)。
理念: 最好的代码是无需编写的代码。少即是多。
完整指南: 阅读
rules/kiss-avoid-over-engineering.md
获取全面的模式和示例。

Quick Reference

快速参考

SignalAction
Deep nesting (>3 levels)Flatten with early returns
Long function (>30 lines)Extract smaller functions
Complex conditionalsUse lookup tables
Over-abstractionInline single-use code
Premature optimizationRemove unless profiled

信号操作
深层嵌套(超过3级)使用提前返回扁平化
过长函数(超过30行)提取为更小的函数
复杂条件判断使用查找表
过度抽象内联单次使用的代码
过早优化除非经过性能分析否则移除

Complexity Targets

复杂度目标

MetricTarget
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:
  1. Can I delete this? - Unused code, dead branches
  2. Can I inline this? - Single-use abstractions
  3. Can I flatten this? - Nested conditions, callbacks
  4. Can I use built-ins? - Array methods, standard library
  5. Is this needed now? - YAGNI (You Ain't Gonna Need It)
  6. Would a junior understand? - If not, simplify

在编写或评审代码前,自问:
  1. 我能否删除这段代码? - 未使用的代码、无效分支
  2. 我能否内联这段代码? - 仅单次使用的抽象
  3. 我能否扁平化这段代码? - 嵌套条件、回调函数
  4. 我能否使用内置功能? - 数组方法、标准库
  5. 现在是否需要这段代码? - YAGNI(You Ain't Gonna Need It,即你不会需要它)
  6. 初级开发者能理解吗? - 如果不能,就简化

Commands

命令

CommandPurpose
simplify <file>
Analyze and simplify a file
simplify:check
Check complexity metrics
quality:complexity
Full complexity report

命令用途
simplify <file>
分析并简化文件
simplify:check
检查复杂度指标
quality:complexity
生成完整复杂度报告

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