

V erdict Well, the OG if checks are straight up a better fit for the use case of a null check.

For nullable (and mutable) vars with the scope functions, while adding things which are not needed, do improve the code quality, with being a slightly more efficient than others. (Although you generally shouldn’t need to access so deeply into a class! Still, reducing the number of safe-calls can help readability). In Kotlin, nullability is intentionally treated to achieve null safety. One of the major benefits of Kotlin as a JVM language is that you can use any and all Java libraries natively. And the Kotlin code is still cleaner compared to Approach 1. Let’s get on with it, shall we? For 2 or more constructor arguments, prefer not to keep the properties on the same line as the class name in the constructor definition class M圜lass ( val a: A, val b: B, ): MyParentClass(a, b), MyInterface Hopefully, the following tips and style recommendations (in no particular order) will help you write better Kotlin! The definitive guide to handling null by Anirban Chatterjee The Startup Medium 500 Apologies, but something went wrong on our end. Kotlins variables are all non-nullable by default. I’ve been meaning to write this article in a while. Checking for null in conditions The most common way of checking null reference is using if-else expression. The goal of Kotlin is to eliminate the possibility of a NullPointerException.
