How Kotlin Solves the “Billion-Dollar Mistake”!

Estefania Cassingena Navone
Techmacademy
Published in
4 min readAug 10, 2018

🔥 Billion Dollar Mistake?!

When I first read about it I was very surprised… You’ll see how Kotlin is the right solution to this problem!

It all started when Tony Hoare, a British computer scientist who developed the Quicksort algorithm, invented the null reference.

Speaking at a software conference called QCon London in 2009, he apologized for inventing it:

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

👌 Kotlin to the Rescue!

If you’ve ever had a NullPointerException, then you know how important it is to check if a reference is not null before trying to access its properties.

Luckily, Kotlin solves this problem by introducing new features to help us with null safety.

Nullability

In Kotlin we can decide if we want our variables to be nullable by adding a question mark after the data type declaration.

As you can see right here:

Null Safety in Kotlin
  • Diagram A declares an integer variable that cannot take the value null because we didn’t add a question mark after the data type (Int).
  • Diagram B declares a variable that can take the value null because we added a question mark after the data type (Int).

Let’s see what happens if we try to assign null to a variable declared without the question mark.

Error

Can you see how powerful this is? We will never have to worry about Null Pointer Exceptions ever again!

✅ Lists

Let’s see how we can apply these principles to lists!

If we want to enable the possibility that the list itself can be null, we need to add a question mark after the data type, exactly as before:

var listOfPatients : List<String>? = null

Let’s see the error we get if we don’t enable nullability and then assign a value null:

List Error

List Elements

If you’d like to enable the possibility that the individual elements contained in the list could be null you need to add a question mark after the data type declaration of the elements:

var listOfPatients : List<String?> = null

Both

You can also indicate that you want both the list itself or its elements to be nullable by adding a question mark on both data types:

var listOfPatients : List<String?>? = null

❓ Handling Null

In case we do want our variable to be nullable, we need to check its value with an if statement before we can operate with it:

val patientName : String? = "Nora"var nameLength = if (patientName != null) patientName.length else -1

As you can see, Kotlin is much more strict than other programming languages when it comes to nullability and this is a great advantage to us as developers because it avoids crashes, errors and bugs in our code. 🎆

📚 To Learn More about Nullability and Kotlin…

👋 Now…

I would like to personally invite you to follow Techmacademy to find articles specially written for awesome learners like you.

If you found this article helpful, your claps are very much appreciated to help others who are also embarking on this journey. 😃

I would love to read your comments and thoughts.

Follow me on Medium 😃 👍

--

--

Estefania Cassingena Navone
Techmacademy

Udemy Instructor | Developer | MITx 6.00.1x Community TA | Writer @Medium | CS & Mathematics Student | WTM Udacity Scholar