kotlin-patterns

Guide idiomatic Kotlin patterns for null safety, immutability, and coroutines.

Updated May 24, 2023
One-click install
npx skills add https://github.com/Kimjiman/basic-arch --skill kotlin-patterns-kimjiman
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-patterns
Source: https://github.com/Kimjiman/basic-arch/tree/main/.claude/skills/kotlin-patterns
Command: npx skills add https://github.com/Kimjiman/basic-arch --skill kotlin-patterns-kimjiman

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill helps developers write more robust, efficient, and maintainable Kotlin applications by enforcing idiomatic patterns and best practices.

Core Features & Use Cases

  • Null Safety: Leverage Kotlin's type system to prevent null pointer exceptions.
  • Immutability: Promote predictable state management using val and immutable collections.
  • Structured Concurrency: Utilize coroutines and Flow for efficient asynchronous operations.
  • DSL Builders: Create type-safe Domain Specific Languages.
  • Use Case: Refactor existing Java code to idiomatic Kotlin, ensuring better safety and conciseness, or onboard new team members to Kotlin best practices.

Quick Start

Apply idiomatic Kotlin patterns to refactor the provided Java code snippet for improved null safety and immutability.

Frequently Asked Questions about kotlin-patterns

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

FAQPage Schema
How do I refactor Java code to idiomatic Kotlin for better null safety?

Idiomatic Kotlin leverages the type system for null safety and uses `val` with immutable collections to prevent null pointer exceptions. Refactoring Java code with these patterns promotes predictable state management and improves overall code conciseness.

What is the best way to manage asynchronous operations in Kotlin?

The best way to manage asynchronous operations in Kotlin is through structured concurrency. Utilizing coroutines and `Flow` allows you to build efficient asynchronous operations, ensuring robust and maintainable application performance.

How do I create type-safe DSL builders in Kotlin?

Creating type-safe DSL builders in Kotlin involves using idiomatic patterns like extension functions and function types with receivers. This approach allows you to build domain-specific languages that prevent common errors and improve code readability.

When do I need sealed classes in Kotlin?

You need sealed classes in Kotlin when representing restricted class hierarchies, enabling exhaustive `when` expressions. This idiomatic pattern improves code quality by ensuring all possible subtypes are handled at compile time, preventing common errors.

Can I use Gradle Kotlin DSL to improve my build configurations?

Yes, you can use Gradle Kotlin DSL to write type-safe build scripts. Applying idiomatic Kotlin patterns to Gradle configurations leverages the type system and extension functions, resulting in more robust and maintainable build logic.

Why should I prefer immutability and `val` over `var` in Kotlin?

You should prefer immutability and `val` over `var` to achieve predictable state management in Kotlin. Using `val` and immutable collections prevents accidental modifications, reducing common errors and enhancing long-term code maintainability.