Celebrating 5 Years of Kotlin with the most useful features

The image depicts a language named kotlin
Photo by: Louis Tsai

As recently announced by Google, it has been 5 years since Kotlin was officially supported for Android development!

Being a hardcore lover of Java ever since my undergrad years and having worked on Java as a postgrad fresher, I must admit, that I was not very thrilled to explore Kotlin 5 years back when it was announced to be the officially supported language. I was happy to continue using Java which was still going to be supported.

Little did I know that a small bit of Kotlin code that my team lead wrote in our Java-only Android project would change my entire outlook on writing code! I was intrigued by how a little bit of code could do so much!

Just one data class and you get all kinds of methods like getters, setters, toString(), and hashCode() by just declaring a class as a data class!

It wasn’t long before I decided to slowly start writing new classes in the project in Kotlin and explore the likes of varvalobjectscompanion objectsapplylet?delegates, and whatnot!

I had officially fallen in love with Kotlin within just 2 months of exploration!

And now, for the last 4 and a half years, my love for Kotlin has never dithered its way. Reading Kotlin’s journey with Android in this detailed blog by Android Developers, I decided that I too, wanted to share my journey with Kotlin.

So here I am, sharing the most helpful Kotlin functions/classes/features that I use the most daily and without which my code would not have been as clean and concise.

1. Scoped Functions (letrunwithapply, and also)

The Scope Functions are the most helpful when I have to execute a large set of operations on a single object.

I mostly use them when I have to initialize an object with a variety of members in it.

 

2. Transformation functions (map, flatMap, flatten)

I would lose track of counting if I count the number of times I use these functions in my projects where I have to deal with a relational database and extract and combine data from multiple datasets.

Super useful when you want only some data from your data classes. Example-

3. Filtering functions (filter, any, all, none)

Again, I find myself using these functions many times a day to filter out unwanted data from a large database.

Useful when you want to filter your lists with only the required list items or check what sort of data resides in your list.

4. Extension Functions

One of my favorite features of Kotlin. Handy when you want to add some feature to an object without writing too much boiler code every time or without inheriting the class to get access to its methods.

For when you want to fetch the text of an EditText-

or when you want to show/hide data in your app dynamically everywhere-

5. Null Safety

After going through the pain of NullPointerException a million times in Java, this is, I think, by far the best feature introduced with Kotlin.

The operators, !! and ? made writing clean code so much easier!

Not to mention the use of safe call operator (?.) has been a friend in disguise where you can make calls from an object but the code will be bypassed automatically if that object is null. Thus eliminating the need for null checks or NullPointerException try-catch boiler code.

And then there is the Elvis operator (?:) which lets you write code for what to do if an object is null.

6. When

When may seem as tedious as switch-case in Java, but it is super flexible in that it allows you to have multiple conditional checks irrespective of the type of object or type of condition.

To me when has come in handy across almost all of my projects where I needed to redirect the flow of my app based on lots of conditions and object states.

7. Coroutines

Coroutines gave a new meaning to background processing. Once avoided feature has now become an active participant in day-to-day coding.

Coroutines come in handy whenever I need to take the network calls to the background or make multiple concurrent network calls and combine the results of both, upon completion.

I don’t know how I would have optimized my app for network calls without coroutines.

Conclusion

This was just the tip of the iceberg when it comes to how Kotlin has helped revolutionize the way we code optimally and concisely in Android. These were the most useful features of Kotlin that I find myself using every day of coding Android Apps. There are still many that I intend to write about shortly.

Till then, Happy coding in Kotlin!

Categories