Context

I’ve wanted to use Kotlin for the longest time. For my day job, I tried to use Kotlin for tests and prototypes when I could, but it didn’t really catch on with the rest of my coworkers. So me learning Kotlin has been put on the back burner.

Due to life changes brought by the pandemic, I managed to get a bit of time for personal endeavors. I decided to try and give Kotlin a shot again. However, I did not have a project in mind to build something with Kotlin. Fortunately, I was reading Real-World Software Development and had a “genius” idea.

I wonder if by porting this sample code from Java to Kotlin, I’ll learn some of its ins and outs.

Quick review of the book

Real-World Software Development is from authors Raoul-Gabriel Urma and Richard Warburton. The book is mostly aimed at junior developers, or experienced developers who might want to refresh their fundamentals. The book covers topics such as SOLID, KISS, TDD, and many more.

It’s a good book and a quick read also. I highly recommend it.

How it went

I only got to spend 30 minutes to an hour on workdays. On my free days, I tried to do more, but being a father of a very loving 4-year old, I still was not able to work on the exercise as much as I could. Looking at my git commits, it took me two months to finish the exercise. I originally thought this to be a one month thing at most.

For the exercise itself, I ported the sample code chapter by chapter. What I did was read a chapter to get a preview of what I will be porting. After I finished reading, I proceeded with porting to Kotlin.

For the most part, I felt I was still coding in Java which is expected since I’m just porting Java code to Kotlin. You have your class, your methods or functions, your properties, and so on, just written differently and with less code. I did quite have a realization when I went on to do some refactoring on my day job. I thought that the class or piece of code I was refactoring might read better if I used Kotlin. The book uses Junit for its tests. Since I wanted to try all things Kotlin, I went with Kotest instead. In hindsight, this was a mistake. Kotest is great but by using it instead of Junit, I ended up adding unnecessary complexity to what I was doing.

Another mistake I made during the exercise was to not write down my thoughts each time I finished porting a chapter. While I did leave a bit of comments, I had to read the code again and recall what was going on in my head at the time. Should I do this again, I swear to note things down when I can.

My initial thoughts on Kotlin

Obviously I did not go through all of Kotlin’s features. I barely scratched the surface with this exercise. The book also did not use any frameworks like Spring so neither did I. I particularly like the nullable types (String is different from String?) and mutable collections (List is read-only while MutableList can be mutated).

One of the things I don’t fully understand yet are scope functions (let, run, with, apply, also). Even if the tests return green, I’m not sure if I used the correct scope function.

Some of my other thoughts are below.

Things I like:

  • Nullable types
    • String and String? are different and will result in a compile error when used incorrectly. This is great for expressing intent because it forces you to think if the variable should be nullable or not.
  • Mutable and immutable collections
    • List is read-only. If you need a list that can be mutated, use MutableList. Again, great for expressing intent because it forces you to think if a collection can be modified or not.
  • Compile time null-safety
    • With a nullable type, it needs to be suffixed with either ? or !! to compile.
  • if is an expression and returns a value.
    • The result of an if statement can be directly assigned to a variable. No need for initializing variables.
  • Scope functions (let, run, with, apply, also)
    • Lets you run a block of code in the context of an object.
  • val and var
    • Using these two, one can easily tell if a variable is immutable (val) or not (var).

Things I don’t like or I’m not used to yet.

  • public by default
    • I think I prefer Java’s approach to having classes package-private by default. This has been a wanted feature since 2016.
  • How the primary constructor is defined
    • I find declaring the primary constructor inline with class name to be awkward.
  • Declaring of custom getters and setters
    • I didn’t use it much or at all during porting, but reading the docs about it, I think it’s more complicated than Java’s.

Final thoughts

I’m happy I did this exercise as it gave me a better appreciation of Kotlin, and the fact that I actually finished something I said I would do gave me a sense of accomplishment.

One of my goals is to use Kotlin as the primary language for a backend server application. Short-to-mid term, I would like to use Kotlin to build frontend applications. I’ve been a backend developer throughout my career so I’m thinking of trying frontend with whatever Kotlin has to offer. I dabbled with Flutter a bit earlier in the year and I did enjoy the time I spent with it. It would be nice to compare the experience between using Flutter and Kotlin when developing frontend applications.

If you’re interested, the code can be found on GitHub