X-Team Blog - The Most-Loved Company for Engineers

Here's Why Backend Developers Love Kotlin

Written by Nicolás Gómez Solano | Feb 10, 2022 5:00:00 AM

Something magical happens whenever I tell someone that I program in Kotlin. “Oh, so you're an Android developer?” they almost always say. You might have thought the same, and it wouldn't be your fault! Kotlin's tight integration with Android Studio makes it virtually unavoidable if you want to program an Android app. And it's awesome! I love how Kotlin makes Android development easier and faster.

But there's much more to Kotlin than Android. I can tell, because I've been creating production-ready microservices with Kotlin since 2018. But don't take my word for it. Have a look at these numbers from Syk's 2021 JVM Ecosystem Report: In 2020, one in twenty developers used Kotlin for their main application. In 2021, that had grown to one in six, making it the second-most popular language on the JVM.

This blog post will dive into five reasons why Kotlin is a great programming language for backend development too. If you're a backend developer unfamiliar with Kotlin, read on...

5 Kotlin Features that Backend Developers Love

Null Safety

Kotlin turned one of the biggest problems of Java into a powerful feature that allows a programmer to easily deal with the absence of value. There's a reason Tony Hoare apologized for inventing the null reference (which he called his billion-dollar mistake). I prefer not to deal with NullPointerException anymore. If you want to learn how Kotlin handles nullity, have a look at this page.

Concurrency

Compared to other JVM-based languages, Kotlin stands out for its minimal yet powerful non-blocking programming API that can launch coroutines. Kotlin coroutines are usually defined as lightweight threads, i.e. small programs that can be started and paused based on several factors. With simple primitives such as launch, async, or suspended, you can implement basic concurrency in Kotlin. If you want to learn more about Kotlin coroutines, take a look at this page.

Interoperability

Kotlin is compatible with all frameworks written in Java. So you can use the libraries you love while also using all Kotlin features. For example, you can implement an interface behind a framework/library that ensures null safety. If it's open-source, you can even send a pull request that adds Kotlin extensions (Spring has lots of them).

Functional Programming

One of the great things about Kotlin is that it supports both object-oriented and functional programming. Have a look at this example:

fun main() {
    // Who sent the most messages?
    val frequentSender = messages
        .groupBy(Message::sender)
        .maxByOrNull { (_, messages) -> messages.size }
        ?.key                                                 // Get their names
    println(frequentSender) // [Ma]

    // Who are the senders?
    val senders = messages
        .asSequence()                                         // Make operations lazy (for a long call chain)
        .filter { it.body.isNotBlank() && !it.isRead }        // Use lambdas...
        .map(Message::sender)                                 // ...or member references
        .distinct()
        .sorted()
        .toList()                                             // Convert sequence back to a list to get a result
    println(senders) // [Adam, Ma]
}

data class Message(                                          // Create a data class
    val sender: String,
    val body: String,
    val isRead: Boolean = false,                              // Provide a default value for the argument
)

val messages = listOf(                                       // Create a list
    Message("Ma", "Hey! Where are you?"),
    Message("Adam", "Everything going according to plan today?"),
    Message("Ma", "Please reply. I've lost you!"),
)
Extracted from the Try Kotlin section of the Kotlin Programming Language website

This example explores many interesting features: data classes with default parameters, lambdas as expressions, concise functions for working with data (instead of the imperative counterpart), etc. Try it out here or find more examples here.

Integrations With Well-Known Frameworks

Spring is one of the most popular frameworks for building modern and scalable web applications for Java. Spring has officially supported Kotlin for over five years now, and has added many features since, from Kotlin coroutines support for Spring Webflux to Spring Data support (Redis, MongoDB, R2DBC, etc.). You can even generate a Spring project from scratch using Kotlin with the Spring Initializr.

If you've ever been involved in a reactive programming project, you've probably heard about Micronaut. According to its official page, “Micronaut is a modern JVM-based full-stack framework for building modular, easily testable microservices and serverless applications.” It's the perfect candidate for Kotlin and the Micronaut Kotlin Integration was released in 2019.

Vert.x is a native framework for building reactive applications on the JVM. Kotlin support was added in Vertx 3.4.0, but it got even better in 3.5.0 when Vert.x added Kotlin coroutines support (have a look at this page to see how you can use it). Today, Vert.x has dedicated support and documentation for Kotlin developers.

What Still Needs to Improve

While I'm a huge fan of Kotlin, there is still one thing in particular that I believe needs to improve. It's not a huge dealbreaker, but the Kotlin ecosystem could really use better IDE support.

When it comes to IntelliJ IDEA, and in particular IntelliJ IDEA Ultimate and its community, Kotlin is compatible without effort using JetBrain tools (of course). But you need a license if you want to have full support for frameworks like Spring Boot or Vert.x, and that's not great.

When it comes to VSCode, you can write pure Kotlin code using this or this extension, but there is currently limited or no support for a framework such as Spring Boot. The only available extensions are exclusively Java-compatible. There's a reason Spring's sts4 has this and this issue open, asking for Kotlin support. Not great either.

To Conclude

So Kotlin could do with better IDE support. That's my only complaint, but it pales in comparison to features such as null safety, concurrency, interoperability, functional programming, and its integration with great frameworks, which all make Kotlin an excellent choice for backend developers.