kotlin-control-flow
GitHub提供 Kotlin 分支与流程控制的最佳实践,涵盖 when 表达式、密封类穷举、智能转换及守卫条件等场景的编码与审查指南。
Trigger Scenarios
Install
npx skills add chrisbanes/skills --skill kotlin-control-flow -g -y
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
-
Name the value being classified. If every branch tests it, use
when (subject); otherwise keep a subjectlesswhenorifchain. -
Choose the branch shape:
Code shape Prefer One classified value when (subject)Unrelated boolean conditions Subjectless whenorif/elsePrimary case plus a branch-local predicate Guard condition Invalid input before the main path Early return, require, orcheckClosed value-returning domain Exhaustive whenexpressionOpen input or deliberate fallback Explicit else -
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. -
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 withis. When a data-bearing sealed case is collapsed by a catch-all, show the branch-level use in the review:is Outcome.Failed -> safeFailureLabel(outcome.reason)The
ischeck smart-castsoutcome, so the branch can usereasonwithout a cast. The helper name is illustrative; choose a safe mapping for the caller contract. Do not replace this branch-level explanation with a note that unspecified callers can inspect the original result. If the input is an open server/platform value or needs real fallback/logging, keepelse. -
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.
-
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. -
Compile and test. On failure, return to the smallest applicable earlier step or retain the prior shape. In a review of a closed sealed mapping, when a data-bearing subtype affects the mapping or its safe handling, put the typed branch and member access in the finding itself, not only in analysis or as a general note to add explicit cases. Use this pseudocode template with the actual subtype and payload member substituted:
is <DataSubtype> -> map(result.<payload>)This is a template, not literal Kotlin: the
istest smart-castsresult, and the member access shows how the branch uses its payload. If the public value stays generic, state how the branch handles the detail or why it is deliberately discarded. Naming cases alone or sending callers to inspect the original value is not complete branch guidance. Apply this only to closed, data-bearing cases; retainelsefor open-world values or real fallbacks. 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
- Kotlin concurrency and Flow — state/event primitives.
- Kotlin API design — explicit common-code branching.
Version History
-
2026.9.25
Current 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


