Kotlin best practices is an AI agent skill from the GitHub repository HoangNguyen0403/agent-skills-standard. Core Kotlin patterns for scope functions, backing properties, and read-only collection interfaces. Use for let/apply/run/also/with choices or encapsulating mutable state; defer general language constructs, extension functions, coroutines/runCatching, Android/Room recipes, and testing/tooling setup.
Claude Code: save it as .claude/skills/kotlin-best-practices/SKILL.md
Cursor: save it as .cursor/rules/kotlin-best-practices.md
Codex: save it as .agents/skills/kotlin-best-practices/SKILL.md
ChatGPT, Gemini and other assistants: paste it into custom or project instructions
Skill file
Kotlin Best Practices
**Priority: P1 (HIGH)**
Implementation Guidelines
Scope Functions:
apply: Object configuration (returns object).
also: Side effects / validation / logging (returns object).
let: Null checks (?.let) or mapping (returns result).
run: Object configuration and mapping (returns result).
with: Grouping multiple method calls on object (returns result).
Backing Properties: Use _state (private mutable, e.g., private val _state = MutableStateFlow(initial)) exposed as val state = _state.asStateFlow() (public read-only). Pattern: _prop private, prop public. On Kotlin 2.4+ an explicit backing field (val state: StateFlow<…> field = MutableStateFlow(…)) is a stable alternative — see Kotlin 2.4 features. Keep the _prop idiom for older languageVersions.
Error Handling: Use runCatching for simple, non-suspending recovery over try/catch. In coroutines it also catches CancellationException — rethrow it before mapping a failure (see kotlin-coroutines).
Visibility: Default to private or internal. Minimize public surface area.
Top-Level: Prefer top-level functions/constants over implementation-less object singletons.
Anti-Patterns
No Deep Scope Nesting: Limit let/apply nesting to 2 levels; deeper destroys readability.
No Public var: Use private set or backing properties for encapsulation.
No Global Mutable State: Avoid mutable top-level variables.
Use this skill with your AI agent:Open on Skill Issue to copy, download, or add to your library.
Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.