跳到正文
cccskills
免费在 GitHub 公开

Simplifying Control Flow

Flatten nested conditionals with early returns or table-driven methods - keep nesting depth under 3 levels

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md3.7 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

Simplifying Control Flow

Overview

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.

Baseline Violation

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.

Three Techniques

1. Flatten with Combined Conditions

✅ 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

2. Table-Driven (Best for Data)

✅ 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.

3. Extract Complex Conditions

✅ 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.

Quick Reference

ProblemSolution
Nested if/elseFlatten with combined conditions OR table-driven
Deep nesting (>3)Extract inner logic to function
Complex booleanExtract to named function
Business rulesTable-driven method
Long if/elif chainTable lookup OR polymorphism

Guard Clauses

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.

Red Flags

  • Nesting depth > 3
  • Can't see matching braces without scrolling
  • Duplicate conditions in nested blocks
  • Adding new case touches multiple nesting levels

Fix: Flatten or use table-driven.

Real-World Impact

From baseline:

  • Agents created 2-level nesting for 4 discount tiers
  • With table-driven: All tiers visible, easy to add/modify

Integration with Other Skills

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

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Interactive idea refinement using Socratic method to develop fully-formed designs

中文简介正在准备中。当前显示原文说明。

obra/clank422025年10月10日 更新

Receive and act on code review feedback with technical rigor, not performative agreement or blind implementation

中文简介正在准备中。当前显示原文说明。

obra/clank422025年10月10日 更新

Comment WHY code exists and non-obvious decisions, not WHAT code does (mechanics)

中文简介正在准备中。当前显示原文说明。

obra/clank422025年10月10日 更新

Replace arbitrary timeouts with condition polling for reliable async tests

中文简介正在准备中。当前显示原文说明。

obra/clank422025年10月10日 更新

TDD for process documentation - test with subagents before writing, iterate until bulletproof

中文简介正在准备中。当前显示原文说明。

obra/clank422025年10月10日 更新

Validate at every layer data passes through to make bugs impossible

中文简介正在准备中。当前显示原文说明。

obra/clank422025年10月10日 更新

查看 obra 的全部技能

报告此技能的问题