kotlin-control-flow

Refactors Kotlin branching code using when expressions, guard conditions, and exhaustive sealed type handling.

Updated May 21, 2025
One-click install
npx skills add https://github.com/albertmartorell1975/MeteoMartoCompose --skill kotlin-control-flow-albertmartorell1975
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: kotlin-control-flow
Source: https://github.com/albertmartorell1975/MeteoMartoCompose/tree/main/.agents/skills/kotlin-control-flow
Command: npx skills add https://github.com/albertmartorell1975/MeteoMartoCompose --skill kotlin-control-flow-albertmartorell1975

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Kotlin codebases often accumulate tangled if/else chains, nested branches, and repeated condition checks that hide the classified value, break smart casts, and miss compiler-verified exhaustiveness. This Skill provides a structured refactoring procedure to reshape branching logic into clear, compiler-checked control flow. ## Core Features & Use Cases - Subject identification and branch primitive selection: Decide between subject when, subjectless when, guard conditions, early returns, or explicit else using a decision table. - Guard condition refactoring: Move branch-local predicates into guarded branches, split comma-separated branches, and order guarded branches before unguarded fallbacks. - Exhaustiveness and smart cast verification: Keep closed-domain when expressions exhaustive without unnecessary else and confirm smart casts still work without as or !!. - Use Case: When reviewing a pull request containing a nested if inside a when branch over a sealed class, apply the rewrite recipes to convert it into guarded branches with compiler-proven exhaustiveness. ## Quick Start Ask the AI to review and refactor the branching logic in your Kotlin file using the kotlin-control-flow procedure, converting nested if/else chains into exhaustive when expressions with guard conditions.

Frequently Asked Questions about kotlin-control-flow

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I replace nested if/else chains with when expressions in Kotlin?▼

Identify the single value being classified and make it the when subject, then move branch-local predicates into guard conditions. Place guarded branches before their unguarded fallback for the same primary condition.

When should I use guard conditions in Kotlin when expressions?▼

Use guard conditions when a branch first matches a type or value and then checks an extra predicate belonging only to that branch. The when must have a subject, and a later branch must still handle the same primary condition or the expression must remain exhaustive.

Does Kotlin when require an else branch for sealed classes?▼

No, a when expression over a closed domain like a sealed class, enum, or Boolean is exhaustive without else when every case is handled explicitly. Reserve else for open domains such as server strings or integer status codes.

Can guard conditions be used with comma-separated when branches?▼

No, guard conditions do not apply to comma-separated branch conditions. If only one case needs an extra predicate, split the branch into separate guarded and unguarded branches.

Why does refactoring control flow break Kotlin smart casts?▼

Smart casts break when the rewritten shape forces the compiler to lose type narrowing, requiring as, !!, or duplicated casts. If that happens, keep the original shape or choose a smaller refactor that preserves the narrowed type.

When should I avoid flattening Kotlin code with early returns?▼

Avoid early returns when the nesting carries cleanup, transaction, or error-handling structure. Flatten only when it removes nullable or invalid state from the main path without obscuring those concerns.