One-line summary
A reference catalog of Kotlin-specific anti-patterns — language and Coroutines smells that aren't about design patterns but about misusing Kotlin's features.
When to use this skill
- Code review on Kotlin code, especially code by developers transitioning from Java.
- Spotting smells during refactoring.
- Onboarding teammates to the team's Kotlin conventions.
- Suspecting a specific pattern of misuse (
!!,lateinit, scope functions, Coroutines).
When NOT to use this skill
- Domain-modeling smells →
ddd-anti-patterns. - Design-pattern misuse →
gof-reviewand the per-pattern skills. - General code quality issues that aren't Kotlin-specific (long methods, poor naming) — handled by general code review, not this catalog.
Core content
Null-safety smells.
!!operator abuse. Force-unwrap anOptional<T>?because "I know it's not null". Wrong because: silent bugs become NullPointerExceptions in production, defeats Kotlin's null safety. Fix: handle the null case explicitly (?.,?:,let), or fix the type to be non-nullable upstream.lateinit varfor everything that's declared late. Wrong when: the field has a sensible default, or when it's not actually populated before use. Fix: use a nullable type with?.orlazy { … }if it should be initialized on first access.lateinitis for DI lifecycle; not for "I haven't decided when to initialize this".- Platform types treated as non-null. Calling a Java API that returns
String(no annotation), assuming non-null, getting NPE at runtime. Fix: assume nullable unless the Java API has@NonNullannotations.
Smart cast smells.
- Explicit
ascast where smart cast would do. Wrong because:asthrows on mismatch; smart casts are guaranteed correct because the compiler tracks the type. Fix: structure code soischecks let the compiler smart-cast for you. - Smart cast bypass via
var. Smart casts don't work across mutating boundaries.var s: String? = …; if (s != null) { val len = s.length }fails becausescould change between check and use. Fix: assign tovalafter the null check.
Scope-function misuse.
applywhere the return value is wanted.applyreturns the receiver;let/runreturn the lambda result. Mixing them up returns the wrong value.with(x) { … }where method-chain style would read better. Style — pick one.- Nested scope functions of different kinds.
obj.let { … }.also { … }.apply { … }is unreadable. Pick one style, name intermediate variables.
Coroutines smells.
GlobalScope.launch { … }— coroutine leaks; no lifetime tied to anything. Fix: use an appropriate scope (viewModelScope,lifecycleScope, a manager-owned scope). Seekp-coroutine-scope.runBlockingin production HTTP handler / server code — blocks a request worker thread. Fix: usesuspend funall the way down; let the framework's coroutine runner orchestrate.- Catching
CancellationExceptionand swallowing it — breaks cancellation propagation. Fix: if you must catch, rethrow. - Blocking I/O on
Dispatchers.Default— starves the CPU dispatcher. Fix:withContext(Dispatchers.IO) { … }around the blocking call. async { compute() }.await()immediately — no parallelism; just callcompute(). Seekp-launch-vs-async.- Mutable shared state across coroutines without synchronization — same race conditions as in any concurrent code. Fix: use confinement (single dispatcher),
Mutex, or actor-style isolation.
Java-flavored Kotlin smells.
- Singleton with
getInstance()+ private constructor whenobject Foo { }would do. Seegof-singleton,gof-kotlin-idioms. - Strategy as
interface Strategy { fun execute() }+ concrete classes when(Input) -> Outputfunction type would do. data classfor entities where identity-only equality is needed.data classcompares all fields. Use a regularclasswith overriddenequalsfor entities.- Long telescoping
Builderwhen named arguments + default values would do. Seegof-builder,gof-kotlin-idioms.
DSL smells.
- DSL without
@DslMarker— outer scope leaks into inner scopes; subtle bugs from accidentally-resolved unqualified names. Seekp-type-safe-builders.
Decision heuristics
- Severity scales with reach: a
!!in a hot path is worse than the same in a one-off script. - Pattern-name the smell so the fix is referenceable (e.g., "Coroutines: GlobalScope abuse — see kp-coroutine-scope").
- Don't grep for
!!literally — it appears in legitimate contexts (test fixtures, where the alternative is clutter). Look at the kind of code. - Some "anti-patterns" are acceptable with comments explaining why — flag-only, don't auto-reject.
Anti-patterns about catching anti-patterns
- Treating every
!!as a defect — sometimes it's the right answer (compile-time invariant the compiler can't see). - Renaming a smell rather than fixing it.
- Reviewing only for these smells while missing larger design problems.
See also
kp-coroutine-scope,kp-launch-vs-async,kp-dispatchers,kp-structured-concurrency— Coroutines smells map to these.kp-type-safe-builders— DSL smells.gof-singleton,gof-kotlin-idioms— Java-flavored Kotlin smells.ddd-anti-patterns— domain-modeling smells (orthogonal).
References
- Kotlin Design Patterns and Best Practices (Alexey Soshin), Ch. 9 (Idioms and Anti-Patterns).
- Kotlin language docs — null safety, smart casts, scope functions.
- Kotlin coroutines official guide — common pitfalls.