Kotlin in the real world
A few weeks ago we started a new Android app here at Imdex. We spent quite a while deciding whether to try the new programming language Kotlin, or just stick to Java.
We were skeptical. There is just so much hype around Kotlin right now. I’ve been programming for long enough to know that not everything that glitters is gold. I’ve been burned too many times by hot new technologies looked great in tutorials but had horrible flaws when you tried to use them in the real world.
Well now we’ve been using Kotlin for a few weeks on a real app and I can say with certainty: the hype is right, Kotlin is amazing.
It’s easy to learn. It’s easy write. It’s easy to read. It works seamlessly with Java. It works seamlessly with Android Studio. It works seamlessly with every Java library I’ve used. It’s just amazing.
In this post I’m going to show you the 12 parts of Kotlin that we’ve found the most useful.
1 — Copy Java, Paste Kotlin
This was a pleasant surprise. Android Studio will automatically convert Java to Kotlin if you paste it into a Kotlin file. Take a look at the example below.
I love watching this. It’s like all of the old cruft of Java is just melting away.
This feature is very useful when you’re first learning Kotlin. You can take a piece of Java that you understand and see what it looks like when it’s converted to Kotlin.
2 — Lambdas
Lambdas give you a powerful tool to write expressive and elegant code. If you haven’t used them before they’re functions that aren’t declared but are passed immediately as an expression. They’re awesome for things like callbacks or transforming collections. Take a look at these examples.
Of course you could write this code with
for loops using an imperative procedural style. It would just take you longer, be more error prone and harder to read.
3 — String templates
No more manual string concatenation. Kotlin has an elegant string templating feature that allows you to concatenate variables with a
$ or arbitrary code with
4 — Extension functions
Kotlin allows you to add your own functions to any class. It’s a niche feature but it’s surprisingly useful when you need it.
For a silly example, say that I found myself wanting to double items in List’s all the time, I could add an extension function to the List class like this.
5 — Shrinking Android APIs
One of my favourite things about Kotlin is how it reduces the amount of boilerplate and ceremony involved in using the Android APIs.
Here is just some random Java code getting a reference to a button and doing some typical Android-y stuff with it.
When you write the equivalent code in Kotlin it’s just shorter and simpler.
6 — Inferred types
I love inferred types, they give you the best of both worlds. You get the safety of strongly typed languages and the concise code of weakly typed languages.
7 — Literals
List and map literals read nicely.
8 — Fields
I am so so sick of writing getters, setters and backing fields. It’s just so much boilerplate. In Kotlin fields have a concise declaration syntax.
And you can always add getters and setters later if you need them.
9 — Higher order functions
Kotlin’s support for higher order functions opens the door to some powerful functional programming techniques. A higher order function is a function that can itself take functions as arguments or return a function as a result. That sounds a little confusing and abstract, but it’s an amazingly powerful technique. Here I’ve created a function that creates functions.
10 — Constructors
Kotlin has so many nice little features that help you avoid writing repetitive boilerplate code. This one is small but it’s of my favourites. Constructors allow you to automatically define fields and assign them.
In the code above, the
val in front of
name means that the class
User has an immutable field called
name that is automatically initialised in the constructor.
11 — Null safety
One of the biggest selling points of Kotlin is that it helps you write code that avoids dreaded null pointer exceptions. Theres lots of ways to safely use an object that may be null and not exist.
12 — Refactoring
Android Studio refactoring works seamlessly with Kotlin. This really is an amazing achievement by JetBrains and a huge help when you’re writing code in the real world and need to safely change a name or extract a method. When Apple released Swift they didn’t have refactoring in Xcode for years and it was a real pain.
We’ve been using Kotlin in the real world on some real work and we really haven’t found any nasty surprises. All we’ve actually found is more and more amazing features. Using Kotlin is helping us write better code more quickly and we’re really happy we made the switch.