kotlin-control-flow
Skill1 starsChanged 40 days agoMITView on GitHub
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.
1 other project uses a version of this, 1 of them edited.
Adopted by
Most recently taken up by
- chrisbanes/skillsedited, first seen Jun 18, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 1
- chrisbanes/skills★ 1.2k82% same
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository34 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- android-benchmark-comparisonSkill
- build-and-verifySkill
- compose-animationsSkill
- compose-component-designSkill
- compose-focus-navigationSkill
- compose-performanceSkill
- compose-state-and-effectsSkill
- compose-ui-testing-patternsSkill
- deliver-specSkill
- diagnosing-bugsSkill
- domain-modelingSkill
- gradle-runSkill
- grillingSkill
- grounded-writingSkill
- implement-with-subagentsSkill
- kotlin-api-designSkill
- kotlin-architectureSkill
- kotlin-concurrency-and-flowSkill
- kotlin-skill-routerSkill
- new-featureSkill
- prototypeSkill
- release-kotlin-librarySkill
- researchSkill
- review-prSkill
- run-github-projectSkill
- run-on-deviceSkill
- setup-environmentSkill
- setup-issue-trackerSkill
- shepherdSkill
- tddSkill
- to-planSkill
- triageSkill
- wayfinderSkill
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 1 is distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 28
Use a guard only on a subject when, after a primary condition, when the extra predicate belongs to that branch and an unguarded branch …
- Distinctive: line 49
Use an early return only when it removes invalid or nullable state from the main path.
- Very specific: line 52
Verify smart casts still work without as, !!, mutable temporaries, or duplicate casts.
- Very specific: line 62
Use guarded branches to refine one case, rather than nesting an if:
- Very specific: line 72
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:
- Very specific: line 84
Do not introduce guards on unsupported Kotlin versions, force unrelated boolean checks into a subject when, remove an open-world fallback, …
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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. 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.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

