> kotlin-best-practices

Core patterns for robust Kotlin code (Scope Functions, Backing Properties). Use when writing Kotlin code using scope functions, extension functions, or data classes. (triggers: **/*.kt, apply, let, run, also, with, backing property, collection)

fetch
$curl "https://skillshub.wtf/HoangNguyen0403/agent-skills-standard/kotlin-best-practices?format=md"
SKILL.mdkotlin-best-practices

Kotlin Best Practices

Priority: P1 (HIGH)

Engineering standards for clean, maintainable Kotlin systems.

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 an object (returns result).
  • Backing Properties: Use _prop (private mutable) and prop (public immutable) pattern for encapsulation.
  • Collections: Expose List/Map (read-only interfaces) publicly, keep MutableList internal.
  • Single Expression: Use runCatching for simple error handling over try/catch blocks.
  • Visibility: Default to private or internal. Minimize public surface area.
  • Top-Level: Prefer top-level functions/constants over implementation-less object singletons.

Anti-Patterns

  • Nesting Scope Functions: Avoid nesting let/apply more than 2 levels deep. It destroys readability.
  • Mutable Public Props: Avoid public var. Use private set or backing properties.
  • Global Mutable State: Avoid top-level mutable variables.

Code

// Backing Property Pattern
class ViewModel {
    private val _uiState = MutableStateFlow(UiState.Loading)
    val uiState: StateFlow<UiState> = _uiState.asStateFlow() // Read-only

    fun load() {
        // apply for config
        val user = User().apply {
            name = "John"
            age = 30
        }

        // runCatching
        runCatching { api.fetch() }
            .onSuccess { _uiState.value = UiState.Success(it) }
            .onFailure { logger.error(it) }
    }
}

Related Topics

language | coroutines

┌ stats

installs/wk0
░░░░░░░░░░
github stars341
██████████
first seenMar 17, 2026
└────────────

┌ repo

HoangNguyen0403/agent-skills-standard
by HoangNguyen0403
└────────────

┌ tags

└────────────