Wassim Beltaief

Moving to Kotlin

Tuesday, April 11, 2017

I was skeptical at first. Another JVM language? We already have Java, it works fine.

Then I wrote my first data class:

data class User(val name: String, val age: Int)

Thats it. No getters, no setters, no equals, no hashCode, no toString. Just one line.

In Java this would be like 50 lines of boilerplate. And you know someone will forget to update equals when adding a new field.

Null safety

This is what sold me:

val name: String = null  // compiler error
val name: String? = null // ok, but now you have to handle it

No more NullPointerException at runtime. The compiler force you to think about nulls. Its annoying at first but then you realize your app crashes way less.

Extension functions

You can add methods to existing classes:

fun String.isEmail(): Boolean {
    return this.contains("@")
}

"test@gmail.com".isEmail() // true

This is so useful for Android. You can extend View, Context, anything really.

The switch

Our team migrated gradually. New files in Kotlin, old files stay Java. They work together fine.

After few months nobody wanted to write Java anymore. The code was shorter, safer, and more fun to write.

Google made it official in 2017. Now its the recommended language for Android. Good decision.