Skip to content
Skills Library
Programming · Building & coding

kotlin-patterns

/kotlin-patterns

Idiomatic Kotlin patterns for JVM and Android: null-safety, sealed hierarchies, coroutines and structured concurrency, immutable data modeling, error handling, and Java interop. Use when writing or reviewing `.kt` code, porting Java into Kotlin, designing Android app architecture (ViewModel, repository, Flow), or auditing coroutine cancellation, `runBlocking`, and lifecycle-scoped work.

Repository
malskill ↗
Licence
MIT
Last repository change
13.11.2026

Assessment and security

90/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content25 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
6
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 13.11.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Idiomatic Kotlin patterns for JVM and Android: null-safety, sealed hierarchies, coroutines and structured concurrency, immutable data modeling, error handling, and Java interop. Use when writing or reviewing `.kt` code, porting Java into Kotlin, designing Android app architecture (ViewModel, repository, Flow), or auditing coroutine cancellation, `runBlocking`, and lifecycle-scoped work.

Original author description

Idiomatic Kotlin patterns for JVM and Android: null-safety, sealed hierarchies, coroutines and structured concurrency, immutable data modeling, error handling, and Java interop. Use when writing or reviewing `.kt` code, porting Java into Kotlin, designing Android app architecture (ViewModel, repository, Flow), or auditing coroutine cancellation, `runBlocking`, and lifecycle-scoped work.

How it works

  1. When to activate
  2. Core rules (high signal)
  3. Outcome expectations
  4. Recommended workflow
  5. Quick review checklist
  6. Common anti-patterns to reject
  7. Android-specific patterns
  8. Resources
Original SKILL.md headings · 95 lines, 938 words
SKILL.md preview
---
name: kotlin-patterns
description: "Idiomatic Kotlin patterns for JVM and Android: null-safety, sealed hierarchies, coroutines and structured concurrency, immutable data modeling, error handling, and Java interop. Use when writing or reviewing `.kt` code, porting Java into Kotlin, designing Android app architecture (ViewModel, repository, Flow), or auditing coroutine cancellation, `runBlocking`, and lifecycle-scoped work."
license: MIT
compatibility: "Kotlin 2.0+ (K2 compiler baseline). Targets: JVM 17+, Android SDK 26+ (guidance covers up to SDK 36). Tools: kotlinc, gradle, detekt, ktlint. Optional: kotlinx-coroutines 1.8+, kotlinx-serialization, Android Jetpack (Lifecycle, ViewModel, Compose)."
metadata:
  author: AeonDave
  version: "1.0"
---

# Kotlin Patterns

Day-to-day idiomatic Kotlin for JVM and Android code. Use `android-testing` for test infrastructure and `mobile-technique` for offensive audit of shipped APKs.

## When to activate

- Writing or refactoring `.kt` modules, libraries, or Android features
- Porting Java code to Kotlin without carrying over Java idioms
- Reviewing PRs for null-safety, coroutine misuse, or leaky lifecycle scopes
- Designing sealed state hierarchies, `Flow` pipelines, or repository APIs
- Deciding between `data class`, `value class`, `object`, and `sealed class`

---

## Core rules (high signal)

- **Nullability is a type**, not a runtime check. Never use `!!` outside test scaffolding or provably non-null bridges — model with `?`, `requireNotNull`, or a sealed result.
- **Prefer immutability**: `val`, `List`/`Map` (read-only interfaces), `data class` with `copy()`. Reach for `var` and `MutableList` only when local mutation is clearer.
- **Suspend, don't block**. In any `suspend` function, blocking calls (JDBC, `Thread.sleep`, blocking I/O) must be wrapped in `withContext(Dispatchers.IO)`; never call `runBlocking` in library, Android UI, or coroutine code.
- **Structured concurrency**: every coroutine runs in a scope with a defined lifetime. `GlobalScope` is a smell; use `viewModelScope`, `lifecycleScope`, or an injected `CoroutineScope` with `SupervisorJob`.
- **Model states with `sealed class`/`sealed interface`**, not boolean flags or nullable pairs. Compiler-enforced exhaustive `when` is the primary correctness tool.
- **Errors as data at boundaries**: return `Result<T>`, a sealed `Outcome`, or a domain-specific type from network/repository layers; reserve exceptions for programmer errors and truly exceptional infra failures.
- **`data class` for values, `value class` for zero-cost typed wrappers, `object` for singletons, `class` when identity or inheritance matters.**
- **Extension functions extend readability, not surface area.** Keep them `internal` or file-private unless the API is intentionally public.

---

## Outcome expectations

- Public APIs make nullability, suspension, and threading obvious at call sites.
- Domain state is modeled with sealed hierarchies; `when` expressions are exhaustive without an `else` branch.
- No unscoped coroutines; cancellation propagates through the call graph.
- No `!!`, no `lateinit var` on shared state, no `runBlocking` in production paths.
- ktlint/detekt run clean; `-Werror` and `-Xexplicit-api=strict` on library modules.

---

## Recommended workflow

1. Sketch the domain: sealed states, value-typed IDs (`@JvmInline value class UserId(val raw: String)`), and repository contracts before implementation.
2. Choose the coroutine boundary. UI collects `StateFlow`; repositories return cold `Flow`; suspend functions declare their dispatcher intent (`withContext`) at the leaf, not the caller.
3. Implement with small `internal`/`private` helpers; split files by responsibility, not by class count.
4. Add null-safety and cancellation-awareness before adding features: every `suspend` fun must let `CancellationException` propagate (never swallow it in a broad `catch (e: Exception)`).
5. Run `./gradlew ktlintCheck detekt test` before review.

---

## Quick review checklist

- No `!!`; `lateinit` only for framework-injected non-null fields (`@Inject`, Android views)
- No `GlobalScope`, `runBlocking`, or `Thread.sleep` in production code paths
- `catch (e: Exception)` blocks re-throw `CancellationException` (or use `catch (e: Throwable)` with `if (e is CancellationException) throw e`)
- `Flow` collectors run in a lifecycle-aware scope (`repeatOnLifecycle(STARTED)` for UI); no `flow.collect { }` inside `lifecycleScope.launch` without a lifecycle state gate
- `sealed` state hierarchies use exhaustive `when` (no `else -> {}` catchall on domain states)
- Data classes representing domain state are `val`-only; mutation goes through `copy()`
- Coroutine builders (`launch`, `async`) attach to a named scope with a `SupervisorJob` when child failure must not cancel siblings
- Public API surface uses `internal` where possible; `expect`/`actual` reserved for genuine multiplatform boundaries

---

## Common anti-patterns to reject

- `!!` sprinkled to satisfy the compiler on nullable receivers
- `runBlocking { }` inside Android code (blocks the main thread) or library code (blocks the caller's thread)
- `GlobalScope.launch { }` — no lifecycle, leaks on config change
- `lateinit var` on shared mutable state instead of `val` + constructor injection
- Swallowing `CancellationException` in a generic `try/catch`
- `when` with `else -> {}` on a sealed hierarchy (silently ignores states added later)
- Extension functions on `Any?` in public APIs (pollutes autocompletion project-wide)
- Java-style getters/setters via `@JvmField`/`@get:JvmName` when the Kotlin property already works for Kotlin consumers

---

## Android-specific patterns

- **ViewModel** owns UI state (`StateFlow<UiState>`); it never touches `Context`, views, or navigation directly. Inject an application-scoped context only when strictly needed.
- **`viewModelScope`** for work tied to the ViewModel; **`lifecycleScope` + `repeatOnLifecycle(Lifecycle.State.STARTED)`** for UI-tied collection. Without the state gate, background work continues on stopped screens.
- **Repository returns cold `Flow`**; caching via `stateIn(scope, SharingStarted.WhileSubscribed(5_000), initial)` at the ViewModel boundary. `SharingStarted.Eagerly` leaks work; `Lazily` never stops.
- **Do not hold `Context`/`View`/`Activity` references across suspend points.** Use `applicationContext` for long-lived scopes; capture what you need before `withContext`.
- **Room DAOs** expose `suspend` for one-shots and `Flow<T>` for queries; never call blocking DAO methods from the main thread.
- **Compose**: state hoisting; `remember` for local UI state; `derivedStateOf` for computed state; `LaunchedEffect(key)` for side effects; never mutate state during composition.

---

## Resources

Load on demand (progressive disclosure):

- [references/coroutines.md](references/coroutines.md) — structured concurrency, dispatchers, cancellation, `Flow` operators, `StateFlow`/`SharedFlow`, testing with `runTest` and virtual time; load when writing or reviewing `suspend` code or diagnosing coroutine leaks
- [references/nullability-and-types.md](references/nullability-and-types.md) — null-safety strategies, platform types from Java, `value class`, `sealed` modeling, delegated properties; load when designing domain types or auditing `!!` and `lateinit` usage
- [references/errors-and-flow-control.md](references/errors-and-flow-control.md) — `Result<T>` vs sealed `Outcome`, exception hygiene, cancellation-safe try/catch, `require`/`check`/`error`; load when designing repository/API error contracts
- [references/java-interop.md](references/java-interop.md) — nullability annotations, `@JvmStatic`/`@JvmOverloads`/`@JvmField`, SAM conversions, checked-exception boundary, calling Kotlin from Java without pain; load when publishing a Kotlin API consumed by Java or wrapping a Java library
- [references/android-architecture.md](references/android-architecture.md) — ViewModel/repository/Flow shape, Hilt DI patterns, lifecycle-aware collection, Compose state hoisting; load when scaffolding a feature module or reviewing lifecycle/leak bugs
Files in this package (6)
  • SKILL.md 8 KB
  • references/android-architecture.md 8 KB
  • references/coroutines.md 6 KB
  • references/errors-and-flow-control.md 6 KB
  • references/java-interop.md 7 KB
  • references/nullability-and-types.md 6 KB

Versions

RepositoryScoreLicence
This page · aeondave/malskill90MIT
affaan-m/ECC95MIT
affaan-m/ECC95MIT
SHRIJANAGAIN/Nemo-Cli95Apache-2.0
aaione/everything-claude-code-zh91MIT

Database snapshot: 13.11.2026 · package version 3301ed4506fa (SHA-256 digest of the entire package)

Downloads in the last 30 days: 2 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 90kotlin-engineering
    Kotlin engineering standards for production Android development — null safety, immutable state, sealed types, coroutines, structured concurrency, Flow/StateFlow, error handling and testing. Use when writing or reviewing Kotlin code.
  • 95kotlin-coding-standards
    Master Kotlin coding standards with null safety, coroutines, and idiomatic patterns. Use when developing JVM/Android applications requiring type-safe async programming.
  • 95lang-kotlin
    Use this skill when the user says 'Kotlin', 'coroutines', 'Flow', 'StateFlow', 'suspend function', 'structured concurrency', 'null safety Kotlin', 'data class', 'sealed interface', 'extension function', 'K2 compiler', 'KSP', 'Gradle Kotlin DSL', 'MockK', 'kotlinx.serialization'.…
  • 100java-kotlin
    Use when writing modern Java (17+) or Kotlin on the JVM. Covers records, sealed interfaces, pattern matching, coroutines, null safety, and JVM performance and memory tuning. Trigger on "migrate this off java 8 style", "convert this to kotlin coroutines", "jvm keeps running out…
  • 100kotlin
    Kotlin language fundamentals. Covers null safety, coroutines, flow, sealed classes, data classes, scope functions, and Kotlin 2.x features (K2 compiler, context parameters). Use for Kotlin/JVM, Kotlin/Native, Kotlin/JS work. USE WHEN: user mentions "Kotlin", "coroutines",…
  • 90kotlin-idioms
    Write Kotlin that uses null safety, data classes, and coroutines as designed, with clean Java interop. Use when writing Kotlin or migrating Java code and habits to it.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.