| name | kotlin-conventions |
| description | Kotlin code conventions for this project. Use when writing, editing, or reviewing Kotlin code (.kt files). Apply these rules for all Kotlin code generation. |
Code Conventions
Explicit API Mode
All modules use Kotlin explicit API mode (-Xexplicit-api=strict). This requires:
- All public declarations must have explicit visibility modifiers (
public, internal, private)
- All public declarations must have explicit return types
Formatting Rules
1. Named Parameters Required
Always use named parameters when calling functions, class constructors, and enum entries.
Person(
name = "Alice",
age = 30,
)
enum class Color(val hex: String) {
Red(hex = "#FF0000"),
Blue(hex = "#0000FF"),
}
Person("Alice", 30)
Red("#FF0000")
2. One Parameter Per Line
Each parameter must be on its own line for readability.
Address(
street = "Main St",
city = "Springfield",
zipCode = "12345",
)
Address(street = "Main St", city = "Springfield", zipCode = "12345")
3. No Single-Expression Functions
Always use block body with explicit return statement.
public fun getData(): String {
return "data"
}
public fun getData(): String = "data"
4. Chain Calls on Separate Lines
When chaining function calls, break the line on each call.
listOf(1, 2, 3)
.filter { it > 1 }
.map { it * 2 }
.toSet()
listOf(1, 2, 3).filter { it > 1 }.map { it * 2 }.toSet()
5. Elvis Operator on Separate Line
The elvis operator (?:) must be on a new line.
val name = map["key"]
?: return null
val value = getValue()
?: defaultValue
val name = map["key"] ?: return null
val value = getValue() ?: defaultValue
6. No Implicit it in Lambdas
Always use named parameters in lambdas instead of the implicit it.
list.filter { item ->
item.isValid
}
list.associateBy { entry ->
entry.id
}
list.filter { it.isValid }
list.associateBy { it.id }
7. Separate when Subject Declaration
Always declare the when subject variable on a separate line before the when expression.
val result = getData()
when (result) {
is Success -> handleSuccess()
is Error -> handleError()
}
when (val result = getData()) {
is Success -> handleSuccess()
is Error -> handleError()
}