Migrating to Kotlin — Part 1
If you are following the trend, then it is obvious that you are aware of Kotlin and how Android community going crazy about the kotlin trend. This post targets to those who knows the great features of Kotlin but hasn’t tried it yet in their project.
Before going forward please note that this is not the article sharing any best practices to migrate your code from Java to Kotlin. Instead it shares how a programmer who doesn’t know a single bit of Kotlin can migrate the code incrementally. Moreover this is the way we follow in our app code base as we are very novice to Kotlin. Here is our app, you might be interested in:
So let’s start to understand how we can migrate our project to Kotlin. But before jumping directly into migration, let’s list down few concerns what you might have in your mind:
- How much time I need to spend to learn this whole new language?
- From where shall I start?
- What is the size of Kotlin library?
Let’s try to address those one by one:
Few of the best points about Kotlin you should consider are:
- It is strongly compatible with java, so if you start with Kotlin, it will not force you to convert your whole project to kotlin at ones. That means you can use both java and Kotlin together.
- You don’t need to learn all Kotlin features in one shot. So let’s start learning data types in Kotlin and enhance our knowledge gradually as we go.
There are many basic features in Kotlin you can start with. But we can still start with the most basic feature of any language i.e. data types, variable declaration and initialization. Please refer the documentation for understanding more about data types and how to use them.
Kotlin is a statically typed programming language for the JVM, Android and the browser, 100% interoperable with Javakotlinlang.org
So to start with the above basic concepts, I can figure out that simple POJO classes of our project can be migrated to Kotlin as our first step.
We are starting with POJO, because these are the simple classes with respect to language features. These classes will have below properties:
- Few member variables — Comes under data types and their declaration
- Getters & Setters — We don’t need to worry about as these methods will be auto generated by our smart Kotlin compiler
We always care about size of the library before using it. So lets try to figure it out.
You can notice that, it has 5751 number of methods. For a while you might think that just to write POJO classes in Kotlin, 5751 methods are not worth. But you don’t need to worry about that, please have a look at the below image.
As you can see, the method count got reduced to just 25. So this is clear that the more features you use, the more you need to pay in terms of method counts.
Hopefully your major concerns are clarified now. So let’s start converting our POJO classes into Kotlin.
Consider you have a POJO class User.java which has few fields, getters and setters, equals(), hashCode() and toString() method.
Now lets try to migrate it to Kotlin using the Android Studio provided converter. Just go to Code > Convert Java File to Kotlin File
As you can see, it has already reduced your code from 109 lines to 50 lines. Assume how fast you can do your development ones you learn Kotlin and start writing code by yourself. Just try to analyze the code, understand the syntax and you are good to go.
Note that the variable declarations are having null assigned to them. As you have heard that Kotlin makes your app null safe, so instead of null, you can assign an empty string as a default value. So let’s change the code further.
By doing this, you not only saved yourself from NPE, but also from extra null checks in your equals() and hashCode() methods. Now your methods looks more concise than earlier.
As you can see how without knowing anything we can incrementally migrate to Kotlin and learn gradually. As I am novice to Kotlin, you can suggest any better approach to migrate.