Good Code
The good version checks the Java object and the Java-provided name before returning a Kotlin string.
Lesson 09
Treat Java platform types as nullable until Kotlin code proves the contract.
fun reviewerName(javaReview: JavaReview?): String? {
// Java values are checked before Kotlin treats the name as non-null.
val name = javaReview?.reviewerName ?: return null
return name.trim().takeIf { it.isNotEmpty() }
}fun reviewerName(javaReview: JavaReview): String {
// Platform types can still carry null from Java callers.
return javaReview.reviewerName.trim()
}The good version checks the Java object and the Java-provided name before returning a Kotlin string.
The bad version trusts a platform type. If Java returns null, Kotlin code fails away from the interop boundary.