I’ve worked in software development for 17 years. I mainly work with Big Data, microservices and distributed systems. I primarily code in JVM-based languages (e.g., Java, Scala, Kotlin). Recently, our team worked on a client project using Kotlin.
Kotlin is an open source statically-typed programming language developed by JetBrains. The project began in 2010 and the current version is 1.5.10, published on May 24, 2021. Kotlin is free, with source code available on GitHub.
Our client provides a distributed trade finance platform that uses blockchain technology. In this post, I’ll detail how our team successfully delivered the project using Kotlin.
Why Kotlin was chosen for the project
When picking the language main factor was to use one programming language to avoid language switching for the for Corda developers. Developing in Corda consists of writing the flows (cordapps) and orchestrating those.
Having that our client uses Corda -distributed ledger technology and Kotlin is primary language for the Corda. Kotlin option was evaluated.
We found out that Kotlin supports full interoperability with Java and is more concise and modern comparing to Java. Thus it was picked as a primary language.
Key features of Kotlin
The first and most important feature of Kotlin, in my opinion, is null-safety. Within the Kotlin context, there is no way to get a Null Pointer Exception. This is different from Java, in which Null Pointer Exceptions can still occur. Without having to worry about Null Pointer Exceptions, you can write less protective code and there are significant savings in terms of inspections.
Another feature is immutability. Kotlin supports “val/var,” or separate interfaces for mutable/immutable collections. When a variable is announced, it says it is not changeable and, accordingly, if we know that our object is immutable this eliminates a lot of side effects.
We can be sure that by passing the object inward, no one will change any property of it. There are mutable and immutable collections and it’s easy to trace what’s going on.
Another benefit is coroutines. If we need to work with asynchronous multi-flow code often, coroutines make life easier, as they let you get rid of callbacks.
When handling events, you write asynchronous code as if it were synchronous. There is quite a bit of magic going on there, but it allows you to write complex synchronous code in an easy and readable manner.
There are also extension functions, which allow you to extend the behavior of virtually any class. We can write any additional function either for a string or for our own type, which is quite convenient.
There are smart casts, where we don’t need to specify a type -- the type is inferred by Kotlin automatically. There is no separation, unlike Java on raw types. It doesn't matter whether “Integer” starts with an uppercase or lower case letter.
We just have an “int” type and Kotlin chooses whether it’s possible to use a regular type or to create an object. As a result, we write much less code. When we need to box something, we don’t need to call an install integer and so on.
There is an impression that Kotlin inherited common features from other programming languages. For example, null safety is present in C# and Scala. Immutability is inherent in Scala. Lambda expressions are present in Scala and C#. Kotlin combines the best and most effective features from these languages, but at the same time, is less complex. It allows us to write code easier and faster.
We bought books and tutorials related to Kotlin. Some of the resources that came in handy:
- The Kotlin documentation site,
- The book “Kotlin In Action” by Dmitry Jemerov and Svetlana Isakova,
- The book “The Joy of Kotlin” by Pierre-Yves Saumont.
It was important for new team members to understand that Kotlin is closer to Scala than pure Java. We wanted them to understand the concepts of immutability and null-safety. We started out with strict code reviews. Two engineers were responsible for reviewing code, leaving comments and advising the rest of the team how the code could be improved.
Secondly, we had meetings once a week where we discussed what was accomplished, what was done well. We were able to train a large number of engineers in just one month. Soon enough, we were able to remove the restriction of strict code reviews and the pace of pull requests picked up immediately.
People began writing quickly and in a good way and it looked like Kotlin. We removed strict viewers and the problem with slow emergence of PRs went away.
The initial project delivery was a big success and we continue to work with the client today.
With Kotlin, we generated less boilerplate code. If we analyze the examples included in Corda, which has both Kotlin and Java code, we see that the Kotlin code is considerably shorter. Because of Kotlin’s null-safety, we didn’t need to guard against or respond to Null Pointer Exceptions. This leads to a lower number of errors. The immutable tables meant that we had less problems with related side effects.
Engineering T@lk „From Java to Kotlin. Lessons learned and main Kotlin features”
We are happy to invite you to our next Engineering T@lk in Katowice! This talk is dedicated to switching big teams from Java to Kotlin and the main lessons learned during the switch.
- Date: 9 June 2022
- Hours: 18:00 - till the end of our networking session. We are open to exchanging experiences, getting to know you better, and telling you more about Capgemini Engineering.
- Location: Aioli, Rynek 5, Katowice, or online (but we all miss the offline events, don`t we?)
- The presentation will be conducted in english
- Topic: From Java to Kotlin. Lessons learned and main Kotlin features.
My name is Andrey Kolodnitskiy. I am Staff Engineer my primary focus is around distributed systems, microservices and JVM based languages.
Overview of main Kotlin features will be provided and specifically, we would discuss the coroutines and how those are working.
Kotlin is a modern programming language that gets more and more popular nowadays.
This talk will be dedicated to the story and important aspects related to switching teams from Java to Kotlin and lessons learnt during such switch.
During the talk we will go through main Kotlin features like nullability handling, extension functions, working with collections and other key features. Particularly deeper dive on Kotlin coroutines will be made, which would help to understand the difference in asynchronous processing and concurrency approach suggested by Kotlin.