languages/kotlin.md
language: kotlin extensions: [".kt", ".kts"]
Kotlin — Language-Specific Review Notes
Load this file alongside rules/universal.md. Universal rules are not repeated here — only Kotlin-specific rules and idioms.
PR Analyzer — Kotlin Risk Signals
println()statements left in production code@Suppressannotations — verify they are justified!!(not-null assertion) used broadly without justification- Hardcoded credentials or API keys in source
Code Quality — Kotlin Checks
!!used broadly — prefer?.let,?:, orrequireNotNull()lateinit varaccessed before initialization- Coroutines launched with
GlobalScope— prefer scoped coroutines runBlockingused outside of tests or top-level entry points
Security
- Flag Room / SQLite queries built with string concatenation — require parameterized queries
- Flag
WebView.loadUrl()with user-controlled input without validation - Flag credentials stored in
SharedPreferences— requireEncryptedSharedPreferencesor Keychain
Async / Coroutines
- Flag
GlobalScope.launch/GlobalScope.asyncin production code — use a structured scope - Flag
runBlockingoutside of tests or top-level main functions - Flag
launch/asyncwithout aCoroutineExceptionHandlerorsupervisorScopewhere individual failures should not cancel siblings - Flag
Dispatchers.Mainused for CPU-bound work — useDispatchers.Default - Flag coroutine cancellation not respected — long loops should check
isActiveor callyield()
Resource Management
- Flag
Closeable/AutoCloseablenot wrapped in.use { }(Kotlin's try-with-resources equivalent) - Flag
OkHttpClient/Retrofitinstantiated per-request — share a singleton - Flag
BroadcastReceiverregistered without a correspondingunregisterReceiver— memory / battery leak - Flag coroutines that hold a resource across a
suspendpoint without structured cleanup infinally
Exception Handling
- Flag
runCatching { }.getOrNull()used broadly — silently swallows all exceptions - Flag
catch (e: Exception)in coroutines without re-throwingCancellationException— breaks structured concurrency - Flag empty
catchblocks - Flag
throw RuntimeException(e)without a descriptive message - Prefer typed
sealed classerror hierarchies over raw exceptions for domain errors in coroutine flows
Performance
- Flag
buildString/StringBuildernot used for multi-step string construction in loops - Flag
Listused for frequentcontainschecks — preferSet - Flag
flow.collect {}re-subscribing on every recomposition in Jetpack Compose — usecollectAsStateWithLifecycle - Flag
Dispatchers.IOused for CPU-bound work — useDispatchers.Default - Flag
suspendfunctions calling non-suspend blocking APIs directly — wrap withwithContext(Dispatchers.IO)
Idioms and Best Practices
Null Safety
- Prefer safe call (
?.) and Elvis operator (?:) over!! - Use
requireNotNull()/checkNotNull()with a descriptive message when null means a programming error - Prefer
valovervar— immutability by default
Modern Kotlin
- Prefer
data classfor value carriers - Prefer
sealed class/sealed interfacefor exhaustivewhenexpressions - Prefer extension functions over utility classes
- Prefer
objectdeclarations for singletons