Agent Skills › chrisbanes/skills › kotlin-control-flow

kotlin-control-flow

GitHub

提供 Kotlin 分支与控制流的最佳实践,指导使用 when 表达式、守卫条件、密封类穷尽性检查及智能转换,优化代码可读性与安全性。

skills/kotlin-control-flow/SKILL.md chrisbanes/skills

Trigger Scenarios

编写或审查 Kotlin 分支逻辑 重构复杂的 if/else 链 处理密封类或枚举的穷尽性检查

Install

npx skills add chrisbanes/skills --skill kotlin-control-flow -g -y
More Options

Use without installing

npx skills use chrisbanes/skills@kotlin-control-flow

指定 Agent (Claude Code)

npx skills add chrisbanes/skills --skill kotlin-control-flow -a claude-code -g -y

安装 repo 全部 skill

npx skills add chrisbanes/skills --all -g -y

预览 repo 内 skill

npx skills add chrisbanes/skills --list

SKILL.md

Frontmatter
{
    "name": "kotlin-control-flow",
    "description": "Use when writing or reviewing Kotlin branching and control flow: when expressions, guard conditions, sealed type exhaustiveness, smart casts, nullable branching, early returns, or replacing complex if\/else chains."
}

Kotlin control flow

Core principle

Make the classified value obvious, keep branch-local predicates on their branch, and let the compiler prove closed-domain coverage.

Procedure

  1. Name the value being classified. If every branch tests it, use when (subject); otherwise keep a subjectless when or if chain.

  2. Choose the branch shape:

    Code shape Prefer
    One classified value when (subject)
    Unrelated boolean conditions Subjectless when or if/else
    Primary case plus a branch-local predicate Guard condition
    Invalid input before the main path Early return, require, or check
    Closed value-returning domain Exhaustive when expression
    Open input or deliberate fallback Explicit else
  3. Use a guard only on a subject when, after a primary condition, when the extra predicate belongs to that branch and an unguarded branch still handles the primary condition. Put the guarded branch first. Split comma-separated conditions instead of guarding one of them.

  4. For a closed enum, Boolean, sealed type, or nullable closed type, name every case and omit else. Match objects by value and class/data-class subtypes with is. If the input is an open server/platform value or needs real fallback/logging, keep else. Give each data-bearing subtype, including one a catch-all collapses, its own is branch. When its data affects the mapping or its safe handling, the finding or change must show the smart-cast member access, even if the public value stays constant:

    is Outcome.Failed -> safeFailureLabel(outcome.reason)
    

    The is check smart-casts outcome, so the branch uses reason without a cast; the helper name is illustrative. Naming cases alone, or sending callers to inspect the original value, is not complete branch guidance. When the data does not affect the result, keep the current value in that is branch; do not invent a use of the data.

  5. Use an early return only when it removes invalid or nullable state from the main path. Keep nesting that expresses cleanup, transaction, or error handling.

  6. Verify smart casts still work without as, !!, mutable temporaries, or duplicate casts. If they do not, keep the original shape or take a smaller refactor.

  7. Compile and test with the project's own build tool; in a Gradle project use the Gradle run workflow, never raw Gradle. On failure, return to the smallest applicable earlier step or retain the prior shape. Finish when the subject, fallbacks, and relevant branch data are obvious to a reader and the resulting shape is easier to scan.

Recipes

Use guarded branches to refine one case, rather than nesting an if:

return when (event) {
    is Event.Message if event.isUnread -> Row.Highlighted(event.message)
    is Event.Message -> Row.Normal(event.message)
    Event.Empty -> Row.Empty
}

Use a subject when when repeated conditions classify one value, and include null as a branch when it is one case in a larger classification:

return when (val selected = selection) {
    null -> SelectionUi.None
    is Selection.Single if selected.item.isArchived -> SelectionUi.Archived(selected.item)
    is Selection.Single -> SelectionUi.Active(selected.item)
    is Selection.Multiple -> SelectionUi.Count(selected.items.size)
}

Do not introduce guards on unsupported Kotlin versions, force unrelated boolean checks into a subject when, remove an open-world fallback, or flatten code that obscures cleanup, transactions, or errors.

Related

Version History

  • 2026.10.18 Current 2026-10-19 03:05

    细化了 sealed type 和 nullable closed type 的智能转换规则,明确展示数据成员访问;修正早期版本中关于 catch-all 合并时的审查要求描述。

  • 2026.9.25 2026-09-27 20:24

    细化了密封类型分支中数据载荷的处理规范,强调在审查发现中直接展示分支级使用而非仅作为通用注释。

  • 2026.8.24 2026-08-27 17:15

    减少指令冗长度,重新认证Kotlin和Gradle技能评估

  • 2026.8.5 2026-08-16 02:45
  • 2026.7.21 2026-07-24 12:25

Same Skill Collection

skills/android-benchmark-comparison/SKILL.md
skills/compose-animations/SKILL.md
skills/compose-component-design/SKILL.md
skills/compose-focus-navigation/SKILL.md
skills/compose-modifier-and-layout-style/SKILL.md
skills/compose-performance/SKILL.md
skills/compose-recomposition-performance/SKILL.md
skills/compose-side-effects/SKILL.md
skills/compose-slot-api-pattern/SKILL.md
skills/compose-stability-diagnostics/SKILL.md
skills/compose-state-and-effects/SKILL.md
skills/compose-state-authoring/SKILL.md
skills/compose-state-deferred-reads/SKILL.md
skills/compose-state-hoisting/SKILL.md
skills/compose-state-holder-ui-split/SKILL.md
skills/compose-ui-testing-patterns/SKILL.md
skills/deliver-spec/SKILL.md
skills/gradle-run/SKILL.md
skills/grounded-writing/SKILL.md
skills/implement-issue/SKILL.md
skills/implement-with-subagents/SKILL.md
skills/kotlin-api-design/SKILL.md
skills/kotlin-concurrency-and-flow/SKILL.md
skills/kotlin-coroutines-structured-concurrency/SKILL.md
skills/kotlin-flow-state-event-modeling/SKILL.md
skills/kotlin-functions/SKILL.md
skills/kotlin-multiplatform-expect-actual/SKILL.md
skills/kotlin-types-value-class/SKILL.md
skills/release-kotlin-library/SKILL.md
skills/run-github-project/SKILL.md
skills/shepherd/SKILL.md
skills/to-plan/SKILL.md
skills/using-chrisbanes-skills/SKILL.md

Metadata

Files
0
Version
2026.10.18
Hash
b99af66a
Indexed
2026-07-24 12:25

Accueil - Wiki
Copyright © 2011-2026 iteam. Current version is 2.155.2. UTC+08:00, 2026-10-29 18:32
浙ICP备14020137号-1