Lesson 1 of 9 · 24m
Null Safety as a Design Tool, Not a Compiler Feature
Use Kotlin's type system to make absence explicit at boundaries, keep domain models honest, and avoid both null checks and unsafe non-null assertions.
I inherited a Kotlin service where the previous team had "fixed" null safety by sprinkling !! anywhere the compiler complained. The service compiled beautifully and then died in production because a payment webhook omitted a field that the Java integration library had modeled as nullable. Kotlin's null safety is not there to make the compiler quieter. It is there to make absence a design decision.
Nullability belongs at the boundary
The most valuable question is not "can this variable be null?" It is "where do we first learn whether this value exists?" Validate external input once, then move inward with narrower types:
data class WebhookPayload(
val customerId: String?, // raw transport truth
val invoiceId: String?,
)
data class RefundCommand(
val customerId: CustomerId, // validated domain truth
val invoiceId: InvoiceId,
)
fun WebhookPayload.toCommand(): RefundCommand? {
val customer = customerId?.takeIf { it.isNotBlank() } ?: return null
val invoice = invoiceId?.takeIf { it.isNotBlank() } ?: return null
return RefundCommand(CustomerId(customer), InvoiceId(invoice))
}The transport model can admit messy reality. The domain model should not. Once you have a RefundCommand, downstream code should not keep re-litigating whether the invoice ID exists.
Prefer explicit absence to nullable meaning overload
null often means several different things: not provided, not applicable, redacted, unknown, or deleted. Those meanings matter. Model them when behavior differs:
sealed interface ProfileImage {
data object NotSet : ProfileImage
data object Removed : ProfileImage
data class Set(val objectKey: String) : ProfileImage
}This is more code than String?, but it prevents the classic bug where a client sends null intending "remove the image" and the server interprets it as "leave unchanged."
Java interop weakens the compiler's guarantee
Platform types from Java can be treated as nullable or non-null. Decide immediately:
val name: String? = javaCustomer.name // explicit and safe
val email: String = javaCustomer.email // promise you must justifyWhen a Java API is under your control, annotate it with @Nullable and @NotNull from JetBrains or Jakarta annotations. When it is not, wrap it behind a Kotlin adapter. Do not let platform types spread through the domain like unlabeled radiation.
Field notes
lateinitis dependency-injection glue, not a domain-model shortcut; uninitialized access becomes a runtime crash.- Jackson needs
jackson-module-kotlin; otherwise constructor validation and nullability behavior can surprise you. - Treat
nullin JSON differently from a missing key only when your API contract actually distinguishes them. - Do not map database nullability blindly into the domain. A nullable column often reflects schema history, not business meaning.
- Null safety works best when validation happens once at the boundary and types become stronger inward.
Was this lesson helpful?