| name | swe-programming-kotlin |
| description | Kotlin coding standards from authoritative docs/explanation/software-engineering/programming-languages/kotlin/ documentation |
Kotlin Coding Standards
Purpose
Progressive disclosure of Kotlin coding standards for agents writing Kotlin code.
Authoritative Source: docs/explanation/software-engineering/programming-languages/kotlin/README.md
Usage: Auto-loaded for agents when writing Kotlin code. Provides quick reference to idioms, best practices, and antipatterns.
Prerequisite Knowledge
IMPORTANT: This skill provides demo-specific style guides, not educational tutorials.
You MUST understand Kotlin fundamentals before using these standards. Complete the demo Kotlin learning path first:
What this skill covers: demo naming conventions, framework choices, repository-specific patterns, how to apply Kotlin knowledge in THIS codebase.
What this skill does NOT cover: Kotlin syntax, language fundamentals, generic patterns (those are in crud-fs-ts-nextjs).
Quick Standards Reference
Naming Conventions
Classes/Types: PascalCase - ZakatCalculator, MurabahaContract
Functions/Variables: camelCase - calculateZakat(), totalAmount
Constants: UPPER_SNAKE_CASE - MAX_NISAB_THRESHOLD, ZAKAT_RATE
Files: PascalCase matching primary class - ZakatCalculator.kt
Null Safety
val length = text?.length ?: 0
if (contract != null) {
println(contract.id)
}
val value = nullableValue!!
Coroutines
suspend fun processPayments(payments: List<Payment>): List<Result<BigDecimal>> =
coroutineScope {
payments.map { payment ->
async { processPayment(payment) }
}.awaitAll()
}
fun zakatCalculations(): Flow<ZakatResult> = flow {
repository.getAllContracts().forEach { contract ->
emit(calculateZakat(contract))
}
}
suspend fun badExample() {
Thread.sleep(1000)
delay(1000)
}
Data Classes and Sealed Classes
data class ZakatCalculation(
val wealth: BigDecimal,
val nisab: BigDecimal,
val amount: BigDecimal,
val calculationDate: LocalDate = LocalDate.now()
)
sealed class ZakatResult {
data class Due(val amount: BigDecimal) : ZakatResult()
data object BelowNisab : ZakatResult()
data class Error(val message: String) : ZakatResult()
}
fun handleResult(result: ZakatResult): String = when (result) {
is ZakatResult.Due -> "Zakat due: ${result.amount}"
is ZakatResult.BelowNisab -> "Below nisab threshold"
is ZakatResult.Error -> "Error: ${result.message}"
}
Error Handling
suspend fun calculateZakat(wealth: BigDecimal, nisab: BigDecimal): Result<BigDecimal> =
runCatching {
require(wealth >= BigDecimal.ZERO) { "Wealth cannot be negative" }
if (wealth >= nisab) wealth.multiply(BigDecimal("0.025"))
else BigDecimal.ZERO
}
sealed class ZakatError {
data class ValidationError(val field: String, val message: String) : ZakatError()
data class CalculationError(val reason: String) : ZakatError()
}
Testing with MockK
@Test
fun `calculateZakat returns 2_5 percent when above nisab`() = runTest {
val mockRepo = mockk<ZakatRepository>()
coEvery { mockRepo.getNisabThreshold() } returns BigDecimal("5000")
val calculator = ZakatCalculator(mockRepo)
val result = calculator.calculate(BigDecimal("10000"))
assertThat(result.getOrThrow()).isEqualByComparingTo("250.00")
coVerify { mockRepo.getNisabThreshold() }
}
Comprehensive Documentation
Authoritative Index: docs/explanation/software-engineering/programming-languages/kotlin/README.md
Mandatory Standards (All Kotlin Code MUST Follow)
- Coding Standards - Naming conventions, Effective Kotlin idioms
- Testing Standards - JUnit 5, Kotest, MockK, coroutines-test
- Code Quality Standards - ktlint, Detekt, compiler warnings
- Build Configuration - Gradle KTS, version catalogs
Context-Specific Standards (Apply When Relevant)
- Error Handling Standards - Result, sealed error hierarchies
- Concurrency Standards - Coroutines, Flow, structured concurrency
- Type Safety Standards - Null safety, sealed classes, data classes
- Performance Standards - Inline functions, lazy, sequences
- Security Standards - Spring Security, JWT, input validation
- API Standards - Ktor routing, REST conventions
- DDD Standards - Domain-Driven Design with sealed classes
- Framework Integration - Ktor, Spring Boot, Android
Related Skills
- docs-applying-content-quality
- repo-practicing-trunk-based-development
References