Avatar ·

Best way to check for null in Kotlin: comparison with null, safe call, or Elvis operator?

Hello, colleagues! I recently started learning Kotlin after Java and encountered a problem choosing the right way to check for null. In Kotlin, as I understand, there are many different approaches, and I'm confused about which one is considered "best practice." For example, I have this code: ```kotlin data class User(val name: String?, val email: String?) fun printUserName(user: User?) { // Option 1: if check if (user != null) { println(user.name) } // Option 2: Using ?.let user?.let { println(it.name) } // Option 3: Using !! println(user!!.name) } ``` The first option seems too verbose to me, the second is unfamiliar, and the third I know should not be used because it can throw NPE. But what cho