chrisbanes/skills

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.

Voir la source
Document Skill original

Rendu depuis le dépôt source en conservant titres, exemples, code, tableaux, liens et images.

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.

  1. 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
  1. 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.

  1. 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; retain the smart-cast payload when the mapping needs it. If the input is an open server/platform value or needs real fallback/logging, keep else.

  1. 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.

  1. 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.

  1. Compile and test. On failure, return to the smallest applicable earlier step

or retain the prior shape. Finish when the subject, fallbacks, and 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:

kotlin
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:

kotlin
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

du même dépôt

Autres Skills

Tous les Skills
chrisbanes
Communauté

compose-ui-testing-patterns

Use when writing or reviewing Jetpack Compose UI tests, screenshot tests, previews, semantics assertions, fake image loading, keyboard input, focus assertions, interaction state (hover/pressed/focused), or tests for plain state-driven UI composables.

installations
4
GitHub Stars
1 k
Mis à jour
21 sept.
chrisbanes
Communauté

compose-animations

Use when writing or reviewing Jetpack Compose motion: visibility enter/exit, animating one property toward a target, color or size transitions, multiple properties from one state, switching composable content, or choosing between AnimatedVisibility, animateAsState, rememberTransition, AnimatedContent, and Crossfade.

installations
3
GitHub Stars
1 k
Mis à jour
21 sept.
chrisbanes
Communauté

compose-component-design

Use when designing or reviewing reusable Jetpack Compose component APIs with modifier parameters, root layout placement, caller-provided variable content, primitive content parameters, optional content, or boolean shape flags.

installations
3
GitHub Stars
1 k
Mis à jour
21 sept.
chrisbanes
Communauté

compose-focus-navigation

Use when writing or reviewing Jetpack Compose UI for TV, keyboard, desktop, accessibility focus, D-pad navigation, FocusRequester, focusProperties, key events, or initial focus behavior.

installations
3
GitHub Stars
1 k
Mis à jour
21 sept.