One month with Kotlin

Four years ago when I started with Android Development I learned to write Java code and it was very amazing for me because I never had worked with true programming before and I was delighted with java especially when I’m writing Apps for Android smartphones, but today the times have changed and work with Java for Android while you see a lot of modern languages evaluate and change the way how code are written and you stay parked is a sad situation.

In the last month I and my co-workers at PorQueNão? started a new project and we decided to use an alternative to Java, this alternative was Kotlin. Kotlin is an Open Source JVM language created by JetBrains with a modern and concise syntax, it works with Java 7 so we can use almost all Android native APIs as well as the third party libraries without problems and it do the most of modern things that a language can do, also, JetBrains provides a good plugin for Android Studio, seeing these characteristics we decided to use it. Someone will ask “Why didn’t you choose Scala, Groovy, Clojure or any other JVM modern language?” the answer is Kotlin was just the first attempt, we needed start with one and we chose this one nothing prevent us to try these others too in the future.

Work this month was very exciting, learning cool things writing less and doing more getting a more beautiful and concise code somethings in Kotlin need to be improved it is true but in general was a beautiful month. I summarized some points that I see as more relevant if you are starting with the language and if you are currently a Java Android Developer, obviously I’ll not talk about all, I’ll only talk about the principal things in my point of view, so lets go.

Variables

Variables are different in Kotlin compared to Java and the first difference is that you can define two kinds of them, the var(variable) and the val(value), the var is used to variables that can have his content changed “mutable” this is a normal variable in Java whereas the val cannot have his content changed “immutable” in Java a val is a final variable. Moreover variables and values in Kotlin can have his type implicit or explicit, so normally you don’t need worry about the variable type because it is automatically assumed.

Variables and constants in Kotlin and in Java.

Class Fields

In Kotlin when you need to create a class with some attributes passed by constructor you don’t need to do a long job of transfer values from constructor to this.variable like in Java, you just need to declare it on class parentheses context and moreover you can define the default value of it, see the example below.

Class fields in Kotlin and the hard way in Java.

Nullables

Variables in Kotlin cannot receive null by default but you can explicitly allows it, for this you can assigning null at variable declaration time but this variable will becomes a Nothing type, so probably you will prefer the second way where you can specify the nullable class, for example a nullable String is a String?, see below the example:

Nullables in Kotlin, Java don’t have Non nulls variables, so it always can be null.

Lazy assignment

A common problem in Android is the dependence that some classes have to create his instances sometimes this dependencies only will be satisfied after some point of the life cycle, a good example is the dependence of the GOD CLASS Context, a lot of objects need the context reference but at the first moment we don’t have context reference to send and we need to wait, so to solve it you need to create a variable declaration with null value at class scope and initialize it on his constructor or create a getter to start the instance for the first time. Kotlin offers to us the Delegation pattern, I’ll not touch this topic in details, but one thing that we won with Delegation is the Delegates.lazy that solves for us that kind of problem, below an example to illustrate the difference.

Lazy load in Kotlin and his similar in Java.

Closure

Android’s Java don’t have closures, so when you have to implements an simple click listener you need to write big and confuse code based in anonymous classes, interfaces or abstract classes, with closure the unless code is removed and you get only the really useful code, below an example:

Closure in Kotlin and his similar in Java. The tweet metric is just an illustrative play.

Singletons

Another great feature of Kotlin is its singleton’s syntax, the singletons are really commons and create one should be simple but it isn’t simple in Java 7where you need to turns his constructor private, create a static field of own class and create a getter to it so in it getter you should verify if the static field is null and create the first instance when it is null to return it after all, it is really a big job, now with Kotlin we just need create an object type instead a class type and it is all, simple like you can see in the example below:

Singleton object in Kotlin and the pattern implementation in Java.

Lambdas

Java 8 already have Lambdas, but to Android developers that don’t use Java 8 its is only a distant dream, so look for an alternative that offers lambdas is a tempting offer and Kotlin has lambdas and uses it simplify very much the code, a simple task like a for each or a sort turns more legible and elegant with lambdas, moreover we have a lot of good opportunities of better implementation, a simple example:

Lambda in Kotlin and his similar in Java.

Conclusion

Kotlin is very new and is evolving constantly, it has a lot of amazing features and if one really specific thing cannot be made using Kotlin you can just use Java and integrate both together easily. So looking for all what was said, today Kotlin is one of the best options to write Android applications.