Kotlin for backend, is it that good ?

Hila Elkayam
Israeli Tech Radar
Published in
5 min readMay 6, 2020

TL;DR yes ! 🙂

I shared my thought about Kotlin in “On the Radar” podcast — it’s a great podcast , you’re welcome to listen !

What is Kotlin?

Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference, initially designed for the JVM , but also compiles to JavaScript or native code .

Kotlin combines object-oriented and functional programming features.

Kotlin was developed by JetBrains, the company behind IntelliJ IDEA which also used Kotlin in many of its products, including IntelliJ IDEA.

Why is it important that JetBrains developed Kotlin?

  • JetBrains developed a successful IDE, they know how developers are developing, they know their needs. Whom better than them can develop something that developers will like.
  • JetBrains has also used Kotlin to develop its tools, including Intellij. While developing they probably encountered problems with the language and had the chance to fix them.
  • IntelliJ has great support for Kotlin, the warning and error light bulb has great suggestions.It helped me several times when i didn’t know how to write my Kotlin code and wrote it in a “java” way, “light bulbs” fixed my code!
  • You can copy java code to the editor area and IntelliJ will ask you whether you want to rewrite to Kotlin. It is good since Java is a well supported language, there are lots of examples from all kinds of areas that you can use due to this feature.

Why prefer Kotlin over Java?

First of all you need to understand, you don’t have to re-write all your Java code in Kotlin, you can just start writing in Kotlin in the same project. Kotlin has very good interoperability with Java and its frameworks.

I’ve been developing in Java for more than 15 years, fell in-love with Kotlin after 15 minutes. I loved the elegancy of the code, loved the way the documentation is written, making it very easy to learn the language. I enjoyed looking at the code I had just finished writing.

There are two things you hear all the time about the differences between Java and Kotlin. The first thing is that it’s more concise. Any problem in java can be written in Kotlin with much less code. Many times I looked at code I have written in Kotlin and was amazed — comparing it with my old java code , Kotlin code was much more readable, easier to maintain and with less bugs (the credit for that is totally Kotlin’s not mine).

The second thing is the exceptions , less null pointer exceptions and no checked Exceptions. I will explain later on each feature but basically you get safer code. I took my code to production with the certainty of not getting a call in the middle of the night.

I heard people saying that Java with Lombok is the same.It’s not. By adding Lombok to java you get some features that Kotlin is proud of, but it isn’t good enough.
Lombok takes AST(Abstract Syntax Tree) and manipulates it, rewriting an in-memory representation of the java program before compilation ends. Some claim this is dangerous and also, if you’re using other annotation framework there might be issues.

Does it compiles faster the Java?

Java compiles about 17% faster then Kotlin on clean builds BUT that is a rare scenario, in most of the cases we change only several files and only those need to be compiled, i mean incremental builds. In this case Kotlin compiles as fast as Java and even slightly faster.

I found this post which performed some tests.

Some cool Kotlin features:

Inline:

In Kotlin, functions are first-class citizens, which means we pass them as arguments to functions, return them as values from functions and assign them to variables. The higher order functions or lambda expressions, all stored as an object which takes some memory and might increase the memory overhead.

Sometimes we have a very basic function that doesn’t do a lot but its functionality is being used in several places therefore we would want to create a function for it. To reduce the memory allocation we can use “inline” which will tell the compiler just copy the code “inline” instead of a call to the function and by doing that we prevent the memory allocation.

This feature needs to be handled carefully as we wouldn’t want to use it with long functions.

Inheritance:

Kotlin, unlike Java, doesn’t rely on defaults and prefers to have things defined explicitly.

By default, Kotlin classes are final, if you want to inherit from a class you’ll need to add an “open” keyword to the class you want to inherit from, also if you want to override a method, you would need to add “override” to the method that overrides.

Null safety:

By default, a variable in Kotlin is not nullable. If you want the variable to have an option of being null, you have to state it.

Kotlin distinguishes between those two options and handles the variable in different ways. If a variable can be null, Kotlin compiler will let you know if you forgot to check its nullability.

Kotlin also provides convenient ways to check for variable nullability, like safe call and elvis operator.

This feature leads to not having a “NullPointerException” in a Kotlin code. Well almost never , cause you can tell Kotlin: “i swear this variable is not null, if it is i deserve the NPE”.

Extension functions :

In Kotlin if you want to add a new functionality to a class, even for a third party class which you cannot inherit from , you don’t have to inherit the whole class and change whatever you want to change. It’s like having a member function of the class defined outside of the class.

Checked Exceptions:

Checked exceptions are typically set on methods and are checked at compile time.For example IOExceptions, if we have a method that throws IOExceptions, the compiler will force the developer to handle this exception.

This will make dirty code, mixing logic with error handling, especially if we have to handle a checked exception inside lambda.

Kotlin doesn’t use checked exceptions. Therefore there is no “throws” either cause throws is used to indicate a method is throwing a checked exception.

If you’re using both Java and Koling together Use the @Throws annotation.

Coroutine:

Coroutines are Kotlin’s way to write asynchronous or non-blocking code.It’s like a light-weighted thread and while threads are being managed by the OS, coroutines are being managed by the user.

Under the hood, we’ll have our regular thread pool but we can determines what thread or threads the corresponding coroutine uses for its execution, we can even stop a coroutine at some point and continue its work in another thread

I just started a new backend project, we were suppose to write the code in Java. I suggested Kotlin and explained the benefits and i am happy to say that my suggestion got accepted! . If you’re a backend developer you should try it too !

--

--