帮助开发者用早返回或表驱动方式简化嵌套条件分支,提升代码可读性。
该技能材料表现为纯提示词/文档型代码风格指南,不要求密钥、无远程端点,也未声明任何执行或数据访问能力。结合其开源来源,整体属低风险;主要不确定性仅在于仓库社区采用度低、许可证与维护状态未明确。
材料明确标注无需密钥或环境变量,未见账号授权、token 收集或凭证传递设计,凭证泄露与滥用风险低。
未声明任何远程端点或联网行为;内容仅为控制流重构建议,未见将用户数据发送到外部服务的描述。
系统检查项标明为 prompt-only,README 也仅包含示例代码与风格原则,未描述本机起进程、执行脚本或调用系统能力。
未声明读取或写入本地文件、数据库、剪贴板或其他资源;从材料看属于静态开发指导,不涉及数据面访问。
来源为 GitHub 开源仓库,源码原则上可审计,这是正面因素;但社区采用度为 0 star,许可证未声明,维护状态未知,供应链可信度仍有一定不确定性。
复制安装指令,让 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
帮助你撰写不过时的代码注释,聚焦做什么与为什么而非时序背景。
帮助开发者按业务领域为代码命名,提升可读性与协作一致性。
系统性沿调用链逆向排查缺陷,快速定位问题的最初触发点
帮助开发者为代码补充设计意图注释,聚焦原因与关键取舍而非表面功能。
并行分派多个智能体,同时调查并修复彼此独立的问题
帮助开发者规范完成开发分支收尾,支持合并、提 PR 或清理流程。
帮助你识别职责混杂的例程,并拆分为单一职责的清晰结构。
帮助用户用接口封装实现细节,从业务层面设计与理解系统
帮助开发者用单一职责变量提升代码可读性、可维护性与调试效率
帮助开发者用先写测试再实现代码的方式提升质量与可维护性。
用多角色代码审查提前发现边界条件、竞态与业务逻辑漏洞
帮助开发者识别并避免常见测试反模式,提升单元测试可维护性与可靠性。