kotlin-patterns

Enforces idiomatic Kotlin patterns for null safety, coroutines, sealed classes, and DSL builders.

Updated Mar 12, 2026
One-click install
npx skills add https://github.com/RavitejaKarra24/dotfiles --skill kotlin-patterns-ravitejakarra24
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-patterns
Source: https://github.com/RavitejaKarra24/dotfiles/tree/main/pi/.pi/agent/skills/kotlin-patterns
Command: npx skills add https://github.com/RavitejaKarra24/dotfiles --skill kotlin-patterns-ravitejakarra24

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Kotlin that compiles is easy, but writing idiomatic Kotlin that fully leverages the type system, immutability, and structured concurrency requires deep knowledge of language conventions. This Skill provides a comprehensive reference of Kotlin best practices so code reviews, refactors, and new implementations consistently follow idiomatic patterns instead of Java-style habits. ## Core Features & Use Cases - Null Safety & Immutability: Enforces non-nullable types by default, safe-call and Elvis operators, val over var, and copy()-based updates on data classes. - Structured Concurrency: Covers coroutineScope, supervisorScope, async/await, Flow operators, cancellation handling, and cleanup with NonCancellable. - Type-Safe Design: Guides sealed classes and interfaces for exhaustive when expressions, @JvmInline value class wrappers, extension functions, delegation with by, and @DslMarker DSL builders. - Use Case: When reviewing a pull request that uses GlobalScope.launch and force-unwraps nullable values with !!, apply this Skill to refactor toward structured concurrency and safe-call chains. ## Quick Start Ask the assistant to review or write Kotlin code following the kotlin-patterns skill, for example to refactor a suspend function to use structured concurrency with coroutineScope.

Frequently Asked Questions about kotlin-patterns

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

FAQPage Schema
How do I write idiomatic Kotlin code with coroutines?

Use structured concurrency with coroutineScope or supervisorScope instead of GlobalScope, combine parallel work with async and await, and expose streams as cold Flow with operators like debounce, mapLatest, and catch. Always check cancellation with ensureActive before expensive work.

What is the best way to handle null safety in Kotlin?

Declare non-nullable types by default and use safe-call operators with the Elvis operator for defaults, such as user?.email ?: fallback. Avoid force-unwrapping with !! since it throws NullPointerException, and use require or check for precondition validation.

When should I use sealed classes vs sealed interfaces in Kotlin?

Use sealed classes for restricted hierarchies with shared state, such as a Result type with Success, Failure, and Loading variants. Use sealed interfaces when implementations need multiple inheritance, such as modeling API error types that each expose a message property.

Does Kotlin Flow support debounce and error handling?

Yes, Flow supports debounce, distinctUntilChanged, filter, and mapLatest for transforming streams, plus catch for error handling where you can emit fallback values. Flows are cold, so collection starts only when a terminal operator is invoked.

Why should I avoid GlobalScope in Kotlin coroutines?

GlobalScope launches coroutines outside any structured lifecycle, so they leak when the parent operation is cancelled or fails. Use coroutineScope or supervisorScope so child coroutines are cancelled together and errors propagate predictably.

How do I build a type-safe DSL in Kotlin?

Define a builder class with lambda-with-receiver parameters and annotate it with a custom @DslMarker annotation to restrict implicit receiver access. Expose a top-level factory function that applies the lambda and returns the built object, as shown in the HTML and server configuration examples.