complexity-guardrails
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseComplexity Guardrails
复杂度管控准则
Design to keep complexity low from day one.
从项目第一天起就要在设计上保持低复杂度。
Targets
管控指标
- Cyclomatic complexity per function ≤ 7 (start splitting at 5)
- Function length ≤ 80 lines (aim for ≤ 30)
- One responsibility per function; one axis of variation per module
- 单个函数的圈复杂度 ≤ 7(达到5即可开始拆分)
- 函数长度 ≤ 80行(目标是≤ 30行)
- 每个函数仅承担一项职责;每个模块仅对应一个变化维度
Tactics
实现策略
- Use early returns and guard clauses; avoid deep nesting
- Extract branch bodies into named helpers
- Replace long if/else/switch with tables (maps) or strategies
- Separate phases: parse → validate → normalize → act
- 使用提前返回和守卫子句;避免深层嵌套
- 将分支主体代码提取为带命名的辅助函数
- 用表(映射)或策略模式替换冗长的if/else/switch语句
- 拆分处理阶段:解析 → 校验 → 归一化 → 执行
Signs to refactor now
需要立即重构的信号
- Hard-to-test code paths
- Repeated conditionals with subtle differences
- Mixed concerns (IO, validation, transformation) in one method
- 存在难以测试的代码路径
- 重复出现仅有细微差异的条件判断
- 同一个方法中混合了多种关注点(IO、校验、转换)