Migrating legacy Java code to Kotlin
2021-09-10
Background
Once upon a time, this morning, I was confronted by some Java code I wrote in 2007. No jokes! Back in the day, I actually knew a thing or two about Java Swing, believe it or not. Fast forward to today, and I get an email from a client I haven't heard of in over a decade, saying they need to upgrade the system to make it more scalable and were wondering if I'm open to giving them some guidance and advice.
Surprisingly, this code has been running, on and off, on a virtual machine hosting Windows XP with Java 6. The application takes satellite images and overlays some geospatial data to help operators detect anomalies and clean up the data. Nothing fancy, but it works and there's been no need for change until now.
The client wants to migrate to a web-based solution, backed by various micro-services. There's an opportunity here to make a lot of improvements and apply nearly 15 years worth of lessons! Of particular interest to me is the opportunity to revisit legacy code and help port it to Kotlin.
Why Kotlin though? Why take perfectly working, reliable and battle-tested Java code and move it to a new language? Will the benefits outweigh the risks? That's the discussion we had and I want to share a little bit of it with you, because I realise there's still folks out there that have not had a chance to try out Kotlin yet.
The beginning
Let's take a look at an existing Java class in the application and use that as an example of how migrating to Kotlin can simplify the code and ease some of the cognitive load on the developers and maintainers of the system. I understand that this is a simple example, but I am not writing a book, so bear with me a little!
This is a typical Java class, used to store some data that gets managed in some or other data structure. There are hundreds of these types of classes in a large Java application, and making changes to them can become a chore. Sure, you can use tools like Lombok etc. to make your life easier, but that just adds more layers of abstraction and complexity and I'm not a big fan of that approach.
The middle
JetBrains, the people behind Kotlin, have spent a lot of time on the tooling support and it shows in things like the automated Java-to-Kotlin converter. While not always perfect, it does save a lot of time and effort in porting code to Kotlin and will be able to help us migrate a significant portion of the code.
The plan is to chunk the migration into manageable pieces and run integration tests at the end of each day to ensure we are not introducing bugs as we go along. The following snippet shows the first run of the conversion tool on the existing Java class.
That's already a lot more readable, with no effort. Of course, like with many automated processes, there's some room for improvement, but I think this is a great first step.
The end
Now that we have some Kotlin code, we can look into taking advantage of some of the features of Kotlin, like Data classes.
By making use of this feature of Kotlin, we can rewrite the class as follows:
Notice how much shorter and easier to read the code has become! Now imagine this impact over hundreds of
classes.
This does not even consider the impact of being able to use extensions, functional
paradigms, co-routines etc. yet!
Note about testing
Testing is a topic for another blog post, but rest assured that before we do any kind of migration, there'll be a lot of unit tests that get added to the system. The plan is to extract the various modules that make up the monolith as Java code and write comprehensive unit tests for all of them.
Once all the modules are extracted and tested, we'll write integration tests and compare the original system to the extracted one, side by side. There's a ton of data and verified output available, so we can write some pretty solid integration tests.
Conclusion
When you consider the fact that Kotlin has nearly seamless interoperability with Java, which allows you to migrate a single class at a time, it becomes a no-brainer to embrace Kotlin and take advantage of the increased developer productivity and happiness. Kotlin has had a tremendous impact on the Java world and I'm delighted to be part of a growing number of developers who have embraced Kotlin across the various areas they work in.
There's a lot more to Kotlin, like Compose for Web, for example! Using Kotlin paradigms and knowledge to build modern web-based UI's brings a new realm of possibilities and it's something you should definitely explore.
Return to the blog index