DEV Community

Cover image for 6 benefits of Kotlin for building server-side applications
Romaric P. for Qovery

Posted on • Originally published at qovery.com

6 benefits of Kotlin for building server-side applications

Kotlin is a programming language over the JVM (like Java). It is well known for being the official programming language for Android. But Kotlin is a reliable and powerful programming language that can also be used for server-side applications.

I have been using Kotlin for 4 years to develop server-side applications. Here are the 6 reasons why it is a great choice for building your next backend:

Java Ecosystem

Java is one of the vastest ecosystems in the programming world. Kotlin is 100% interoperable with Java, which allows it to benefit from all its libraries/frameworks without having to recreate new ones (which was difficult with Scala).

Interoperability is what made its great success on Android, the interface with existing APIs is painless. It is the same observation on the backend side. Would you like to use a framework like Spring Boot in Kotlin? Not a problem, it's even officially supported.

Then, Kotlin starts with the advantages of thousands of existing Java libraries, frameworks and potential dev adopters.

Concise

There's no need to argue the point, it makes sense. You just have to see the declaration of a class in Kotlin vs Java

Kotlin example

data class User(val firstName: String? = null, val lastName: String? = null)
Enter fullscreen mode Exit fullscreen mode

Java example

class User {

    private String firstName;
    private String lastName;

    public User() {
    }

    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(firstName, user.firstName) &&
                Objects.equals(lastName, user.lastName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(firstName, lastName);
    }

    @Override
    public String toString() {
        return "User{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                '}';
    }

}
Enter fullscreen mode Exit fullscreen mode

In these two examples, the bytecode will be the same. Except that in Kotlin the compiler does much more than in Java. This improves code maintainability and readability, meaning engineers can write, read, and change code more effectively and efficiently. Features such as type inference, smart casts, data classes, and properties help achieve conciseness.

Note: Java 14 includes the concept of Record - Which is more or less the concept of data class in Kotlin. Let's keep an eye out to see how it evolves over the next few years.

Null Safety

Kotlin brings the "optional" concept used in functional languages with the "?" operator. It prevents crashes when a value can be potentially null and therefore takes into account the case where this would be the case.

When designing a RESTful API we can receive null data, if the case has not been handled, then it is a possible crash. These errors are greatly reduced with Kotlin. Goodbye NullPointerException (the billion dollar mistake)

See the null safety page

Data Transformation

The functional programming concepts are present natively in Kotlin. This makes it possible to chain actions intuitively. Do you need to transform a list of X objects into Y objects? It' s as simple as...

listOf(X(1), x(2), X(3)).map { x -> x.toY() }
Enter fullscreen mode Exit fullscreen mode

Sometimes I can chain up to 10 operations without having to create a single intermediate variable. When you get a taste for it, it's tough to live without it.

Reliability and performance

Kotlin is Open Source and was initially developed by Jetbrains (the company behind the IntelliJ IDE). Its success is due to its performance and reliability close to what you would have in Java. The final code is also ByteCode and contrary to Groovy which had a very bad reputation for performance, here no magic, no "dynamic invoke" in all directions.

Growing community

Kotlin conf'19

The community around Kotlin keeps growing month over month. Companies such as Kodein Koders and Jetbrains are doing their best to animate it and make Kotlin one of the top 5 programming languages used by developers in the coming years.

Top comments (0)