The Case for Using Kotlin in the Cloud

Image for post
Image for post
Photo by Marc Reichelt on Unsplash

Background

Kotlin is a prime example of a self-made language. Since its official release in early 2016, programmers were doubting this new flavor of Java pointing at the fate of Groovy.

A year later, at Google I/O in 2017 Kotlin got first-class support on Android. Since then, Kotlin was almost exclusively associated with Android and mobile development.

Despite the efforts of JetBrains to highlight the versatility of the language; the misconception that Kotlin is only used for ‘making Android apps’ still holds.

Image for post
Image for post
Kotlin Website lists possible use-cases. Data science and Server-side mentioned. Android is last.

Me, myself and Kotlin

In 2017 I was working in a start-up where I wrote the Android app completely in Java. I wasn’t on-board the Kotlin hype train from the start. When news broke out about the support for Android, I let down my guard and started experimenting with it.

By far, the best feature of Kotlin — in my opinion — is it’s interoperability with Java. I didn’t need to re-write anything. I needed a new class (well actually a data class), and I just wrote in Kotlin instead of Java.

Measuring productivity in lines of code is ridiculous, but if we entertain the idea that the average programmer writes around 50 lines of code per day, the case for Kotlin gets stronger with its conciseness a clear advantage over Java’s infamous verbosity.

Consider this data class in Kotlin: No weird Lombok annotations needed, yet you get the handy equals()/hashCode() with a toString() and a copy() function.

data class User(val name: String, val age: Int)

I soon found myself high on Kotlin — not only writing in Kotlin, but converting existing Java code to it. With its host of features such as immutability, nullability checks and lambda functions, I saved a lot of time in debugging and started actually enjoying mobile development.

Why Kotlin makes sense

Kotlin’s biggest selling point is its interoperability with Java, giving you:

This also means that the learning curve for Kotlin is as steep/flat as you want it to be. Although, I highly recommend adopting Kotlin’s immutability and null-safety. Kotlin codes renders NullPointerException in theory a thing of the past. You are learning/working with a big Java framework like Spring? No problem, you can keep doing that but in Kotlin.

The concise yet powerful Kotlin code is ideal for moving fast and breaking things in the cloud. As a strongly statically typed language with an intuitive support for functional programming (Looking at you Java 8), makes Kotlin a safe choice for maintainability guiding you to an elegant implementation.

Running on the JVM and utilizing the familiar build systems maven and Gradle, means you can dockerize your pure or hybrid Kotlin apps the same way you used to do.

The first-party library of coroutines provides the highly sought after support for async calls, channels, mutexes and much more of what is at the core of any competent micro-service.

Modelling your business logic in a Domain Specific Language (DSL) is a good practice when planning. Well Kotlin let’s you DSL as code. Many projects in the Kotlin ecosystem take advantage of this to provide an easy-to-learn powerful framework as it is the case for Ktor. DSL’ing your micro-services — well that’s the future!

If you’re worried about Kotlin’s viability as an option with your cloud provider, rest assured as the language has earned itself a place at Amazon Web Services, Google Cloud Platform and even Heroku if you want to take a small project for a spin.

As for the Kubernetes-oriented, as long as it can be dockerized it can be transformed into a Helm chart, Kubernetes deployment resource etc. You can even compose Kubernetes config in a Kotlin DSL, how meta is that ?!

Final thoughts

Adopting a new language comes with its challenges, especially for something that brings a drastic mindset change. There will be times where you question the distinction between val and var or even insist on a nullable variable. Let alone the occasional in-my-opinion ugly @JvmStatic annotation for your static members. But with Kotlin you gain the agility and control needed to be competitive in the cloud all the while not having to abandon your favorite tools. Treat yourself to a Kotlin tutorial now.

Written by

Systems Developer and Open Source enthusiast

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