
Kotlin Control Flow
Use when writing or reviewing Kotlin branching and control flow: when expressions...
Install
npx skills add chrisbanes/skills --skill kotlin-control-flow -g -a claude-code -y- Repository
- chrisbanes/skills1.1k
- Category
- Mobile
- Updated
- 3d ago
- License
- Apache-2.0
- Context cost
- Lightabout 1,028 tokens
- Runs scripts
- No, instructions only
What it does
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. Guards are stable from Kotlin 2.2. Honor an explicitlanguageVersionsetting; otherwise the project's Kotlin version in its build file or version catalog is enough to confirm support, so do not decline a guard for lack of further proof. -
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. If the input is an open server/platform value or needs real fallback/logging, keepelse. Give each data-bearing subtype, including one a catch-all collapses, its ownisbranch. 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)
