| name | kotlin-patterns |
| description | Use when writing or refactoring Kotlin code, implementing coroutines/Flow, designing DSLs, or integrating Kotlin with Spring Boot. Do NOT use for JPA (use jpa-patterns), SQL optimization (use sql-optimization-patterns), or Java patterns (use java-modern-patterns). |
| paths | **/*.kt, **/*.kts, **/build.gradle* |
Kotlin Development Patterns
Idiomatic Kotlin patterns for building robust, type-safe, and concurrent applications.
Quick Start
When to Activate
- Writing new Kotlin code
- Reviewing or refactoring Kotlin code
- Implementing coroutines or Flow-based async operations
- Designing type-safe DSLs or builders
- Using Kotlin with Spring Boot
- Choosing between sealed classes, enums, data classes
CRITICAL Rules
MUST DO
- Null safety โ Use
?, ?., ?: consistently. Prefer safe calls over !!
- Structured concurrency โ Always use scoped coroutines (
viewModelScope, coroutineScope, supervisorScope)
- Immutability โ Prefer
val over var, List over MutableList in public APIs
- Sealed types for state โ Use
sealed class/sealed interface for UI state, results, errors
- Flow for reactive streams โ Use
Flow for cold streams, StateFlow/SharedFlow for hot streams
- Scope functions correctly โ
let for null check + transform, apply for configuration, also for side effects
- Explicit API mode for libraries โ Use
explicit API mode in library modules
- KDoc for public APIs โ Document public functions and classes
MUST NOT DO
GlobalScope.launch โ Leaks coroutines, no structured concurrency
!! without justification โ Prefer ?., ?:, or requireNotNull with message
runBlocking in production โ Blocks the thread, defeats coroutines purpose
- Block in Flow operators โ
Thread.sleep() in map/filter โ use delay() + flowOn
- Ignore cancellation โ Never catch
CancellationException (rethrow it)
- Forget
awaitClose in callbackFlow โ Causes resource leaks
- Mix platform code in common modules (KMP) โ Use
expect/actual
Reference Guide
Load detailed guidance based on context:
| Topic | Reference | Load When |
|---|
| Coroutines & Flow | references/coroutines-flow.md | Async operations, structured concurrency, Flow operators, backpressure, testing coroutines |
| Spring Boot + Kotlin | references/kotlin-springboot.md | Spring Boot Kotlin compiler plugins, constructor DI, Router DSL, coroutines in Spring |
| DSL & Idioms | references/dsl-idioms.md | Type-safe builders, scope functions, extension functions, delegated properties, inline/reified |
Pattern Hints
- Value class ์ ๊ทน ํ์ฉ: UserId, Email ๊ฐ์ primitive wrapper๋
@JvmInline value class๋ก ๊ฐ์ธ์ ํ์
์์ ์ฑ ํ๋ณด
- SharedFlow for one-shot events: ํ ์คํธ, ๋ค๋น๊ฒ์ด์
๋ฑ ์ผํ์ฑ ์ด๋ฒคํธ๋
MutableSharedFlow๋ก (StateFlow๋ ์ํ ์ ์ง์ฉ)
- Multiple sealed interfaces: UiState ์ธ์ Result, Event ๋ฑ ๋ณ๋ sealed interface๋ก ๊ด์ฌ์ฌ ๋ถ๋ฆฌ
Decision Tree
Writing Kotlin code?
โโ Async operation needed?
โ โโ Simple suspend function โ references/coroutines-flow.md
โ โโ Reactive stream โ Flow API (references/coroutines-flow.md)
โ โโ Callback to Flow โ callbackFlow (references/coroutines-flow.md)
โ โโ Multiple concurrent ops โ supervisorScope (references/coroutines-flow.md)
โโ Spring Boot project?
โ โโ Kotlin-specific setup โ references/kotlin-springboot.md
โ โโ JPA entities โ jpa-patterns skill
โโ Building DSL or API?
โ โโ Type-safe builder โ references/dsl-idioms.md
โ โโ Scope functions โ references/dsl-idioms.md
โ โโ Operator overloading โ references/dsl-idioms.md
โโ Data modeling?
โโ Fixed variants โ sealed class/interface
โโ Plain data holder โ data class
โโ Type-safe ID/wrapper โ value class (@JvmInline)
โโ Platform-specific โ expect/actual (KMP)
Output Template
When implementing Kotlin features, provide:
- Data models (sealed classes, data classes, value classes)
- Implementation (suspend functions, Flow, extension functions)
- Test file with coroutine test support
- Brief explanation of Kotlin-specific patterns used