kotlin-conventions
Idiomatic Kotlin for JVM projects: null safety without !!, val and read-only collections, data/sealed/value classes, exhaustive when, scope functions (let/apply/also/run/with) used sparingly, extensio
SKILL.mdplugins/kotlin-plugin/skills/kotlin-conventions/SKILL.md
GitHubKotlin Conventions
Follow the official Kotlin coding conventions and whatever ktlint/detekt config the project has. Check the Kotlin version in the build before using newer features (data object ≥ 1.9, entries ≥ 1.9, guard conditions in when ≥ 2.1 stable in 2.2, context parameters are experimental).
Null safety
- Non-null by default; nullable only when absence is a real domain state.
- Handle nulls with
?.,?:, early return, orlet:
val user = repository.findById(id) ?: throw UserNotFound(id)
val city = user.address?.city ?: "unknown"
!!is a crash waiting to happen. Allowed only where a preceding line proves non-null and the compiler cannot see it — and then prefercheckNotNull(x) { "why" }/requireNotNullfor a useful message.- Java APIs return platform types (
String!). Declare the Kotlin type explicitly at the boundary (val name: String? = javaObj.getName()) so a null fails where it enters, not three calls later. lateinit varonly for framework/test injection; never for values that can legitimately be missing.
Immutability
valovervar;varonly in tight local scopes.- Public APIs expose
List/Set/Map(read-only interfaces), neverMutableList. Build withbuildList { }/buildMap { }ortoList(). data classproperties areval; produce changes withcopy(...).
Modelling types
@JvmInline value class OrderId(val value: UUID)
data class Order(val id: OrderId, val lines: List<OrderLine>, val status: OrderStatus)
sealed interface PaymentResult {
data class Succeeded(val receiptId: String) : PaymentResult
data class Declined(val reason: String) : PaymentResult
data object Timeout : PaymentResult
}
fun describe(r: PaymentResult): String = when (r) {
is PaymentResult.Succeeded -> "Paid (${r.receiptId})"
is PaymentResult.Declined -> "Declined: ${r.reason}"
PaymentResult.Timeout -> "Timed out"
}
whenover a sealed type or enum is an expression with noelsebranch, so adding a subtype breaks the build where it must be handled.value classfor typed IDs and units — zero runtime cost, prevents mixingUserIdandOrderId.enum classwith properties for fixed sets; iterate withentries(notvalues()) on 1.9+.objectfor stateless singletons only; anything with dependencies is a class injected by constructor.- Prefer
interface+ constructor injection overopenclasses and inheritance; classes are final by default — keep them that way.
Functions
- Default and named arguments instead of overloads and builders:
fun search(query: String, limit: Int = 20, includeArchived: Boolean = false). Call boolean/same-typed args by name. - Single-expression functions (
= …) when the body is one expression and stays readable. - Extension functions for helpers on types you do not own, placed near their use (
OrderMappers.kt), not in a globalUtils.kt. - Top-level functions are fine; no
XxxUtilsobjects. - Mark visibility deliberately:
internalfor module-private API,privatefor file-private helpers.
Scope functions
| Function | Use for |
|---|---|
let |
null-checked transform: x?.let { use(it) } |
apply |
configure an object you just created |
also |
side effect in a chain (logging) |
run / with |
rarely — a block that computes a value from one receiver |
Never nest scope functions or chain more than two; name the lambda parameter when it is ambiguous. A plain val + if is often clearer.
Errors
- Exceptions for unexpected failures and broken invariants:
require(x > 0) { "..." }for arguments,check(state)for state, custom exceptions for domain errors the caller handles. - Expected business outcomes (declined, not found, conflict) are often clearer as a sealed result type — follow the project. If the project uses Arrow
Either, use it consistently. runCatchingcatchesCancellationExceptionandErrors too — only use it at boundaries, never around suspend calls without rethrowing cancellation (see kotlin-coroutines).- Use
use { }for everyCloseable/AutoCloseable.
Collections
- Standard operators (
map,filter,associateBy,groupBy,partition,sumOf) for clarity. asSequence()only for large collections with several chained steps, or for lazy/infinite sources — not by default.firstOrNull { }instead offilter { }.firstOrNull();any { }instead offilter { }.isNotEmpty().associateBy/groupByinstead of manual map building.
Strings and formatting
- String templates (
"Order $id has ${lines.size} lines"), raw strings ("""…""".trimIndent()) for multi-line text. - Never build SQL, shell commands or HTML with templates from user input.
Layout
- Source in
src/main/kotlin, tests insrc/test/kotlin, package path mirrors directories. - One public class per file named after it; a file of related top-level functions/extensions named for its topic (
OrderMappers.kt). - Member order: properties, init blocks, secondary constructors, methods, companion object last.
Java interop
- Called from Java:
@JvmStaticon companion functions,@JvmOverloadson functions with defaults,@Throwsfor checked exceptions Java callers must catch,@JvmFieldonly for constants. - Calling Java: wrap nullable returns at the boundary; prefer Kotlin stdlib over Guava/Apache Commons equivalents.