Skip to content
Mobile
Skill

/kotlin-control-flow

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.

From plugin
skills
1.1k21 skills

Claude Code

$ npx -y skills add chrisbanes/skills --skill kotlin-control-flow --agent claude-code

SKILL.md

kotlin-control-flow.SKILL.md
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. Guards are stable from Kotlin 2.2. Honor an explicit `languageVersion` setting; 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. 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](../gradle-run/SKILL.md) 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](../kotlin-concurrency-and-flow/SKILL.md) — state/event primitives.
  • [Kotlin API design](../kotlin-api-design/SKILL.md) — explicit common-code branching.
Read more
@chrisbanes
Ships withskills

A set of skills for Kotlin, Jetpack Compose, Android development, and grounded writing. The repository is also a portable Agent Plugins and the immediate skill directories under skills/.

Stars on GitHub

Momentum

stars in total
1.1k
Star history of chrisbanes/skills on GitHub, a chart by star-history.com

Other skills on skills.

changelog

changelog

Use when updating or reviewing Unreleased changelog entries, reconciling a changelog against Git history and issue or PR…

@chrisbanes@chrisbanesInstall Skill
compose-animations

compose-animations

Use when writing or reviewing Jetpack Compose motion: visibility enter/exit, animating one property toward a target, color or…

@chrisbanes@chrisbanesInstall Skill
compose-performance

compose-performance

Use when investigating Jetpack Compose recomposition cost, compiler stability reports, skippability, unstable parameters,…

@chrisbanes@chrisbanesInstall Skill

More skills from other plugins.

deep-code-review

deep-code-review

In-depth design-focused code review - understands codebase context before evaluating PR changes, posts structured feedback to…

expo5d53k
@expo@expoInstall Skill
modlens

modlens

Plug-in vision for text-only models. Hard rule: when a file path or URL with an image extension (.png, .jpg, .jpeg, .webp, .gif,…

modlens3w4.6k
@liustack@liustackInstall Skill
appllama-app-design-skill

appllama-app-design-sk…

Build native-feeling, benchmark-quality mobile app screens (Expo / React Native). Use when designing or implementing any mobile…

appllama2mo3.9k
@appllama@appllamaInstall Skill

Questions about kotlin-control-flow.

Which coding agents can use kotlin-control-flow?
kotlin-control-flow installs in Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, OpenCode and Windsurf with the skills CLI, using each agent's flag shown on this page. Flowy's auto-invocation, which fires a skill when your prompt matches, runs in Claude Code only.