帮助开发者用早返回或表驱动方式简化嵌套条件分支,提升代码可读性。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "Simplifying Control Flow" 技能: 1. 下载 https://raw.githubusercontent.com/obra/clank/main/skills/coding/simplifying-control-flow/SKILL.md 2. 保存为 ~/.claude/skills/simplifying-control-flow/SKILL.md 3. 装好后重载技能,告诉我可以用了
请将这段包含 4 层嵌套 if-else 的 Python 代码重构为早返回风格,并确保逻辑不变,同时解释每一步简化原因。
输出更扁平、可读性更高的代码,并附上重构说明。
把这段根据状态和角色执行不同操作的 JavaScript 条件分支改写成表驱动结构,减少重复判断,并给出最终代码。
输出使用映射表或配置对象实现的简洁分支代码。
请检查下面这段 TypeScript 代码的控制流复杂度,找出嵌套超过 3 层的部分,并提出可执行的简化方案与重构示例。
指出问题位置,给出优化建议,并提供简化后的代码示例。
Nested conditionals are hard to understand and error-prone. Flatten them.
Core principle: Nesting depth < 3 levels. Use early returns, table-driven methods, or extracted conditions.
Agents create nested if/else for multi-condition logic:
❌ Nested (baseline):
def calculate_discount(order_amount, is_vip):
if is_vip:
if order_amount > 1000:
return 0.20
elif order_amount > 500:
return 0.15
else:
if order_amount > 1000:
return 0.10
elif order_amount > 500:
return 0.05
return 0.0
Problems: Duplicated logic, hard to see all tiers, adding tier requires finding nesting spot.
✅ All at same level:
def calculate_discount(order_amount, is_vip):
if is_vip and order_amount > 1000: return 0.20
if is_vip and order_amount > 500: return 0.15
if order_amount > 1000: return 0.10
if order_amount > 500: return 0.05
return 0.0
✅ Business rules as data:
DISCOUNT_TIERS = [
(1000, 0.20, 0.10), # min_amount, vip_rate, regular_rate
(500, 0.15, 0.05),
]
def calculate_discount(order_amount, is_vip):
for min_amount, vip_rate, regular_rate in DISCOUNT_TIERS:
if order_amount > min_amount:
return vip_rate if is_vip else regular_rate
return 0.0
When to use: Pricing tiers, status transitions, configuration-driven logic.
✅ Named boolean for clarity:
def is_eligible(user, minimum):
return (user.age >= 18 and user.verified_email and
user.balance > minimum and not user.suspended)
if is_eligible(user, minimum_purchase):
allow_purchase()
When to use: Complex boolean expressions, reused conditions.
| Problem | Solution |
|---|---|
| Nested if/else | Flatten with combined conditions OR table-driven |
| Deep nesting (>3) | Extract inner logic to function |
| Complex boolean | Extract to named function |
| Business rules | Table-driven method |
| Long if/elif chain | Table lookup OR polymorphism |
Baseline showed agents already use these well:
def validate(data):
if not data:
return False, "data required" # Early return
if data.amount <= 0:
return False, "amount must be positive" # Early return
# Main logic here (no nesting)
Keep using this pattern for validation and error cases.
Fix: Flatten or use table-driven.
From baseline:
For complex functions: See skills/keeping-routines-focused - extract when nesting gets deep
For reducing complexity: See skills/architecture/reducing-complexity - simpler control flow = less complexity
先用伪代码梳理方案与迭代思路,再高效转成可执行代码。
帮助你为变量选择清晰准确、易维护的命名,提升代码可读性。
帮助开发者保持类接口抽象一致,避免混杂序列化、持久化等无关职责。
帮助你撰写不过时的代码注释,聚焦做什么与为什么而非时序背景。
帮助用户检索过往 Claude Code 对话,快速找回事实、决策与上下文线索。
为工程师生成分步实施计划,帮助在陌生代码库中快速落地任务。
帮助你识别职责混杂的例程,并拆分为单一职责的清晰结构。
帮助用户用接口封装实现细节,从业务层面设计与理解系统
帮助开发者用单一职责变量提升代码可读性、可维护性与调试效率
帮助开发者用先写测试再实现代码的方式提升质量与可维护性。
用多角色代码审查提前发现边界条件、竞态与业务逻辑漏洞
帮助开发者识别并避免常见测试反模式,提升单元测试可维护性与可靠性。