Kotlin – a great newbie for building API:s

I am working in a team that builds backend services at SVT. We strive to build great API:s with a microservices architecture, primarily for our video platforms but also other clients. To do this we need the most powerful languages and tools, so for us Kotlin is great evolution in the JVM ecosystem that improves our developer experience, efficiency and enables us to write more readable, compact and maintainable code.

What is Kotlin and why is it great

Kotlin is a relatively new language from Jetbrains that has gotten a lot of buzz recently, especially after being adopted with open arms by the Android community.

Kotlin is a statically-typed programming language that runs on the JVM. Kotlin utilizes the power of Java and its ecosystem and gives you new powerful features (some of them cherry picked from other paradigms and languages, examples comes later). Kotlin interoperates with Java code (you can call Kotlin code from Java and vice versa) with no more setup than including dependencies. So it is just plug and play with your existing Java project.

The Kotlin code is compiled to different Java targets (currently Java 6,7 and 8) so the gain is even higher if you have a Java version lower than 8. It leaves out lots of the boilerplate code you have to write using Java.

Java code example:

List<AccessServices> accessServices = attributes.getVersions().stream().map(it -> {

   if (it.getAttributes() != null) {

       return it.getAttributes().getAccessServices();

   }

   else {

       return null;

   }

})

.filter(Objects::nonNull)

.collect(toList())

Which in Kotlin becomes:

val accessServices = attributes.versions.map(it.attributes?.accessServices)

Features we like

  • Null pointer safety
  • Type inference. (will be introduced in Java 10)
  • Coroutines: Simple lightweighted ”threads” that solve the challenges with asynchronization in an elegant way. We have experimented with using coroutines instead of RxJava
  • Data classes (equivalent in JEP for upcoming Java releases). Great for working with JSON data. No getters and setters. Replaces our use of Lombok
  • Extension functions. Add new functions to any class (even final classes such as String)
  • Default and named arguments. Have one constructor and the creator decides what fields to set (if fields is not set as no-null)

Article describing features: https://android.jlelse.eu/kotlin-best-features-a3facac4d6fd

Our experience

Microservices built partially or entirely in Kotlin have been running in production for some time now with a great result.

Learning Kotlin, if you are used to write Java 8 code, is relatively easy since the syntax is similar and all the developers on our team adapted it quick and is fun to work with.

When learning a new language, it is always tempting to use all features and create complex constructs which makes it harder for beginners to the language. We try to avoid this since it is encouraged that other teams make changes in our API:s (together or reviewed by us) to make the development more agile.

Kotlin has become our goto language when building new services or features in an existing Java codebase and it is great.