Blog Infos
Author
Published
Topics
, , , ,
Published
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?
  1. 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.
  2. It’s a hack that will work for short scripts but not production-ready apps.
  3. It’s an unnecessary risk. There are multiple better ways to handle nullability in Kotlin with ?. , ?: , smart casting and even requireNotNull (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

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

No results found.

Jobs

No results found.

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.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
Hi, today I come to you with a quick tip on how to update…
READ MORE
blog
Automation is a key point of Software Testing once it make possible to reproduce…
READ MORE
blog
Drag and Drop reordering in Recyclerview can be achieved with ItemTouchHelper (checkout implementation reference).…
READ MORE
Menu