Kotlin — Much More Than Just a Better Java

Introduction

This Kotlin version of my exercise server “Simple Server” is now the sixth language that I have used to implement the same server as an exercise (previous implementations: Clojure, Javascript, Java, Python and Go, see: my Medium blog post regarding these exercises: Five Languages — Five Stories). I’m planning to implement the Simple Server also using Typescript — maybe after that implementation I update that blog post to “Seven Languages — Seven Stories :-).

You can find the project in Github.

Once again I tried to replicate the file/namespace/class names so that it is easy to compare the implementations (e.g. server.ktserver.pyserver.jsserver.cljServer.javaserver.go).

Image for post
Image for post
Kotlin Code in IntelliJ IDEA.

Tools

I used the following tools in this exercise:

Kotlin vs. Java

I actually started to learn Kotlin already some four years ago. But after a short introduction I thought that “Kotlin is just Java done right” and decided to deep dive into Clojure instead (which actually was a good decision — Clojure is an excellent language and very different when compared to Java and Kotlin). But this autumn I was “forced” to deep dive to Kotlin as well: I worked this autumn with an interesting customer which had a team of extremely competent developers who were also a bunch of real language enthusiastics. They had implemented many microservices using different languages, one of the languages being Kotlin. In that assignment I had a chance to convert one old Java application to Kotlin. Therefore I had a good reason to learn Kotlin and I also decided to implement my exercise server using Kotlin.

After doing some diving into Kotlin I realized that Kotlin actually is much more than just “Java done right”. Kotlin is a very well designed language and you can use Kotlin quite naturally either using object-oriented paradigm or using functional paradigm. I decided to implement this Kotlin version of Simple Server using functional paradigm to compare this side also to Clojure.

My main impressions regarding Kotlin as a language are:

  • Kotlin is really easy. If you have used Java you have absolutely no issues to learn Kotlin. Kotlin has actually simplified creating backend systems using a JVM language quite a bit compared to Java.
sealed class ProductsResult
data class ProductsFound(val data: List<Product>) : ProductsResult()
object ProductsNotFound : ProductsResult()

Then you can use these sealed classes quite nicely to process various happy-day scenarios and exceptional scenarios, example:

val ret = when (val products = getProducts(pgId)) {
is ProductsNotFound -> ProductNotFound
is ProductsFound -> {
when (val p = products.data.firstOrNull { it.pId == pId }) {
null -> ProductNotFound
p -> ProductFound(p)
else -> ProductNotFound
}
}
}

This makes code pretty readable: “If you didn’t find any products you cannot find an individual product. If you found products you can try to find an individual product by pId field.” Because when is an expression you finally return some value to variable ret.

Kotlin vs. Clojure

When comparing Kotlin to Clojure my first impression is however: “Kotlin is pretty good but compared to Clojure Kotlin is just Java done right”. With this statement I mean that Clojure being a Lisp has all the power of Lisp including a real REPL and a real functional language with immutability by default, and excellent data oriented language structures and standard library to process data. If I started a new project with competent developers who already know Clojure or are willing to learn it my choice of language would definitely be Clojure since with competent developers Clojure (Lisp) is like a secret weapon when considering developer productivity. But if Clojure is not an option and I had to choose either Java or Kotlin I would definitely choose Kotlin. So, considering JVM development I’m starting to think that there is no going back to Java. Sorry Java, you had your glory days but now it is time for new guys to continue the JVM journey.

Written by

I’m a Software architect and developer. Currently implementing systems on AWS / GCP / Azure / Docker / Kubernetes using Java, Python, Go and Clojure.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store