There is a new light past the horizon and it shines
Here at Make Helsinki we’ve decided to give Kotlin a go just few weeks before Google announced they would make it an official Android language, together with Java. We would have kept with it anyway, but the announcement cemented it. So far I gave it a go with few of our Android projects and… well it’s awesome. Here is how I got hooked.
It’s easy to get into
If you know Java already, you should be able to start easily. Just learn learn from the converter baked into Android Studio and/or IntelliJ. The converted code works without your input about 50% of the time. The other 50% of the time you should be able to get it working with just a few tweaks, such as updating optional references. Kotlin works well side by side with Java so you don’t even have to wait until starting your next project to try it out. It even converts the dreaded Java getter and setter methods into it’s own properties for you.
Null checks at compile time, similar to the ones found in Swift (but even better), are really cool. When declaring a variable, append a question mark to the type and you’ve made a nullable reference. Now each time you access that variable, you are forced to take into account that there is a possibility it’s value is null. If you don’t take steps to check it’s not null, the app won’t compile. How is this better than swift you ask? In two words: smart casts. Say you do this:
Since it’s checked beforehand, calls to the nullableVariable in that if scope will be treated as if it was a non nullable.
Learn more about null safety in Kotlin here: https://kotlinlang.org/docs/reference/null-safety.html
Finally you can get rid of the billion utility classes littering your codebase. Never again do this
Do this instead:
Extension functions and properties are declared with easy to remember syntax.
Here is an example:
Or, if you want to make it even shorter…
Here is some more info about Kotlin extensions: https://kotlinlang.org/docs/reference/extensions.html
That’s not nearly all
There are many things to love in Kotlin. The language is a treasure trove, like a cult classic movie you can watch ten times and notice new things each time. I have a feeling I’ll find myself writing a follow up article on the topic. But at least at first, this is what I liked.