From Java to Kotlin

For a long time, Java was the most important programming language used at INNO. However, due to the recent development of this language, it was time for us to look for alternatives. We found what we were looking for in Kotlin.

As a JVM language, Kotlin is fully compatible with the Java ecosystem. This of course clears the first important hurdle: New developments in Kotlin can be smoothly integrated into the existing Java code base, and there is no need for time-consuming reimplementation in the new language.

Advantages of Kotlin

From a developer’s point of view, Kotlin offers significant advantages over Java. Written source code in Kotlin is significantly shorter and more concise than in Java, but is no less readable. Some things might take some getting used to for someone with Java background. For example, by convention, it is recommended to avoid semicolons. Also, variables are declared per val (final) or var (not final), functions are introduced with fun , if/else constructs can be used as expressions and much more.
Besides such rather superficial language features, Kotlin also has more substantial things to offer. There are so called Data Classes, which save the author from writing out getters, setters, hashCode(), equals() and toString() when creating data classes. In many cases, such classes consist of only one line of code, such as

data class FooBar(var foo: String, var bar: Int)

A further important property of language, which is often emphasized, is its handling of the value null, or more precisely its representation in the language’s own type system. Assuming two variables s0 and s1 are declared as follows:

var s0: String? = "this is nullable" var s1: String = "this is not"

The difference is obvious: s0 can have the value null, but s1 cannot. In both cases, the compiler is able to enforce appropriate protection measures, so that NullPointerExceptions are usually avoided at compile time and do not cause unpleasant surprises at runtime. For example, the assignment s1 = null would lead to a compilation error, since it is not permitted according to the type declaration. The same applies to the expression println(s0.length). Here are some ways to deal with the case that s0 is actually null. As shown below, you can use the Safe-Call-Operator ?, the Elvis-Operator ?:, an if/else construct or even the Not-Null-Assertion-Operation !! 

println(s0?.length) // print the length or "null" println((s0 ?: "").length) // use empty string in case s0 is null println(if (s0 == null ) -1 else s0.length) println(s0!!.length) // asserts that s0 is in fact not null

The latter operator should be used with caution, because it again opens the possibility for NullPointerExceptions, if s0 in this example, despite the author’s assurance, should actually be null.
A last feature Kotlin benefits from and which should be emphasized here is the way the language handles functions. Basically, these are “first class”, so they can be used like values of any other type and can therefore be stored on package level (independent of classes) and can be passed to other functions as parameters (Higher Order Functions). There are also other mechanisms, such as the so-called Extension Functions, which make everyday programming easier. They allow you to extend a class to include statically resolved functions without modifying the class itself. In this way, you can implement classic utility functions in an elegant way:

fun String.shorten(): String = if (this.length > 3) this.substring(0..2) + "..." else this println("foobar".shorten()) // "foo..."

For these and other reasons, Kotlin has established itself at INNO as the better solution for server-side development. But now Kotlin is cross-platform and can be used for Android and web projects. In addition, JetBrains, the designers of the language, are expanding the possibility of so-called multiplatform programming. Here, code can be shared between different platforms. Because of all this, we hope to be able to use Kotlin even more extensively in the future.

Until then, we encourage everyone interested to try out the language, for example via Kotlin Koans. We promise a very flat learning curve.

Have fun. Enjoy coding.
Your INNO coding team.