Kotlin

Lesson 01

Null safety boundaries

Keep nullable values at input boundaries instead of spreading !! through service code.

Good Code

src/main/kotlin/reviews/ReviewService.kt
data class Review(val title: String)

fun createReview(title: String?): Review? {
    // Nullable input is checked before service code builds the Review.
    val normalized = title?.trim()?.takeIf { it.isNotEmpty() } ?: return null
    return Review(normalized)
}

Bad Code

ReviewService.kt
data class Review(val title: String)

fun createReview(title: String?): Review {
    // The crash moves request validation into a hidden runtime branch.
    return Review(title!!.trim())
}

Review Notes

What to review

Good Code

The good version turns a nullable title into a domain decision before the Review object exists.

Bad Code

The bad version uses !!, so a missing title becomes a runtime crash instead of a validation result.

Takeaways

  • Kotlin review should make nullable contracts visible where request or Java data enters the code.