Kotlin Control Flow
Communitychrisbanes
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.
Overview
- Repository
- chrisbanes/skills
- Publisher
- chrisbanes
- Stars
- 1.2K
- Forks
- 58
- Bundled files
- 0
- License
- Apache-2.0
- Updated
- Nov 14, 2026
Installation
tmp=$(mktemp -d)
git clone --depth 1 https://github.com/chrisbanes/skills.git "$tmp"
mkdir -p ~/.claude/skills
cp -R "$tmp/skills/kotlin-control-flow" ~/.claude/skills/kotlin-control-flow
rm -rf "$tmp"tmp=$(mktemp -d)
git clone --depth 1 https://github.com/chrisbanes/skills.git "$tmp"
mkdir -p ~/.codex/skills
cp -R "$tmp/skills/kotlin-control-flow" ~/.codex/skills/kotlin-control-flow
rm -rf "$tmp"Install the Kotlin Control Flow agent skill from https://github.com/chrisbanes/skills/tree/main/skills/kotlin-control-flow into this project's skills folder.Instructions
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 elseUse 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)The
ischeck smart-castsoutcome, so the branch usesreasonwithout 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 thatisbranch; do not invent a use of the data.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 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
- Kotlin concurrency and Flow — state/event primitives.
- Kotlin API design — explicit common-code branching.
FAQ
What does Kotlin Control Flow do?
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.
Who made Kotlin Control Flow?
chrisbanes (a community member) publishes it in the chrisbanes/skills repository on GitHub.
Is Kotlin Control Flow free?
Yes. Kotlin Control Flow is open source, published by chrisbanes under the Apache-2.0 license.
What comes with Kotlin Control Flow?
Just its instructions. Everything the agent needs is in one SKILL.md file.
Which agents can use Kotlin Control Flow?
Any coding agent that reads SKILL.md files, including Claude Code, Codex, Cursor, GitHub Copilot and Gemini CLI. The agent loads the skill when a task matches its description.
What are agent skills?
An agent skill is a folder with a SKILL.md file: instructions that teach a coding agent how to do one kind of task, and optionally files it can read while it works. The agent keeps only the skill's name and description in mind and reads the rest when a task calls for it.
How are agent skills different from MCP servers?
An MCP server gives an agent new tools to call, code that runs somewhere and returns results. A skill gives it knowledge: how to approach a task with the tools it already has. Many agents use both.
How can my agent test my Android app?
Robomori lets coding agents create, start and control Android emulators and devices from one command, so they can install your app, take screenshots and check dark mode, font sizes and languages.