Skills

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

  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 shapePrefer
    One classified valuewhen (subject)
    Unrelated boolean conditionsSubjectless when or if/else
    Primary case plus a branch-local predicateGuard condition
    Invalid input before the main pathEarly return, require, or check
    Closed value-returning domainExhaustive when expression
    Open input or deliberate fallbackExplicit 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)
    
Read the whole SKILL.md on GitHub

More in Mobile