Write better and more robust code
One of Kotlin’s best features is its handling of nullability, one of the most common sources of bugs in Java and other programming languages. However, in Kotlin, you’re able to break this contract by using !!
, which results in NullPointerException
without details on why and what happened.
Understanding !!
Operator
In short, it forcibly asserts that a given type is non-null. If our value is null, we’ll get a NPE
. Here’s an example:
val name: String? = null val length = name!!.length // Throws NPE(NullPointerException)
So why is it such a big deal?
- It’s unreadable, but it’s hard to maintain if a new programmer looks at a code with
!!
, they’ll ask themselves, “What the hell is happening here??” and there won’t be an answer. - It’s a hack that will work for short scripts but not production-ready apps.
- It’s an unnecessary risk. There are multiple better ways to handle nullability in Kotlin with
?.
,?:
, smart casting and evenrequireNotNull
(which gives a representative message).
Alternatives to !!
1. Safe operator ?.
If it’s okay for the value to be still null or avoid doing work if it’s null, then use the ?.
:
val name: String? = null val length = name?.length // Safe, returns null if name is null
2. Elvis operator ?:
If you need to do something differently in case of the value being null, feel free to use Elvis ?:
:
val name: String? = null val length = name?.length ?: 0 // Returns 0 if name is null
3. Smart casting
One of the simplest ways to reduce nullability is to avoid it all. You can limit your methods not to accept nullable values:
fun doSomething(name: String) { // name will always be non-nullable ... }
4. requireNotNull
with descriptive message
If you cannot reduce the nullability and the value must be present, you might think it’s a good idea to use !!
, but in Kotlin, you should use requireNotNull
in such case, this way you’ll be able to have a descriptive message in your crash report instead of an NPE
without much details on what happened (especially if it’s nested in a lambda):
val name: String? = null val length = requireNotNull(name) { "Name cannot be null" }.length
Job Offers
Summary
Using !!
is a bad practice; multiple alternatives cover every other possible case. Using it is a hack and a shortcut that will backfire while not saving you much time at the time of writing using it. You shouldn’t use it unless you’re writing a short personal script.
fun thanksForReading() { | |
if (learnedSomething) { | |
followForMoreContent() | |
clap() | |
} else { | |
commentHowToImprove() | |
} | |
} |
This article is previously published on proandroiddev.com.