Learning a new programming language (Kotlin): My Approach

For the past year, I have had to code in Java due to legacy reasons. When I got an opportunity to modernize the stack, Kotlin was the natural choice because of its modern features as well as interoperability with Java. The only problem? I didn't know jack about Kotlin. So I decided to learn it. Here is how I approached learning a language completely new to me, modernized a legacy Java application, and shipped it all to production.

First off, What is Kotlin?

Kotlin is a JVM-based programming language widely regarded as a better Java alternative. It combines object-oriented and functional programming features seamlessly. It is completely interoperable with Java, which means you can use all your Java code in a Kotlin project. Kotlin was developed by JetBrains (the maker of IntelliJ Idea) in 2010 and open-sourced in 2012. JetBrains uses Kotlin in many of its products including its flagship IntelliJ IDEA.

Compared to Java, Kotlin is safer, more concise, and streamlined while not giving up on performance at all because they compile to the same bytecode. Kotlin also has excellent tooling support.

Why I decided to learn Kotlin

I was working on a large Java codebase. It kept getting progressively bigger as we added more features, and as the team grew the pain was compounding. It was time to break the monolith down into microservices. As I extracted a relevant and domain-sensible microservice from the monolith, I decided to write that new microservice as a Kotlin project. I did not want to rewrite business-critical logic again and reusing things previously written in Java was a must for me. At the same time, this was a golden opportunity to set up the project in a nicer language (* ahem, opinions might vary).

Benefits of Kotlin over Java

While you can find many articles about this on the web, I will emphasize my lived experience. We experienced innumerable NullPointerExceptions in staging as well as production (damn NPEs). Kotlin's null safety helps a lot in this regard. Kotlin also has a much more concise syntax which is more pleasurable to read, although getting the hang of it can be challenging initially. I also appreciate the first-class nature of functions.

How I am learning Kotlin

Aside from Google-fu for what I need to achieve while coding at work, I am going through Kotlin Koans, which are bite-sized basic exercises designed to teach the language. Every time I read a Koan, I go through the linked docs in their entirety, even if the knowledge is not needed to complete the Koan at hand. While reading those I make notes on my computer, which I break down and put into Anki, a spaced-repetition software. Anki makes it possible for me to never forget what I read. Putting knowledge into Anki is a skill of its own, and I have discovered what works for me through trial and error over the years of using the software. It is definitely a very high effort, high reward exercise. Anki has helped me a lot in learning things, but it is very difficult to get right as well.

What my impressions are

I am very happy with Kotlin. All the major libraries we use support it - Spring and Spring Boot, DGS, etc. The compile times are considerably faster and as a result, my local server boots up faster. The syntax is nicer to read and write, and safer as well.

What my future plans with Kotlin are

I definitely plan on writing more things in Kotlin, writing a better test suite, and converting some pieces of existing code to Kotlin. Quite excited about that.

A word from the detractors

There is a legitimate argument that Kotlin is bound by Java, so it must maintain feature parity. As Java develops and adds syntax for things that are already present in Kotlin, Kotlin will either need to support both the Java syntax and their own which would add superfluousness to an otherwise terse language. Or Kotlin will become an increasingly different language than Java, which would be antithetical to one of the language's goals. This is a valid concern in my opinion and it would be interesting to see how the language evolves alongside Java.

With that said, as of right now, I am happy to be writing and learning Kotlin.