Celebrating 5 Years of Kotlin with the most useful features

Priya Sindkar
Novumlogic
Published in
4 min readAug 24, 2022
Photo by: Louis Tsai- https://unsplash.com/photos/lqcvMiBABHw
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, I was not much 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(), hashCode() by just declaring a class as a data class!

It wasn’t long when I decided to slowly start writing new classes in the project in Kotlin and explore the likes of var, val, objects, companion objects, apply, let, ?, delegates, and what not!

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

And now, since 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 on a daily basis and without which my code would not have been as clean and concise.

1. Scoped Functions (let, run, with, apply, 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 initialise an object with variety of members in it.

2. Transformation functions (map, flatMap, flatten)

I would loose 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 favourite features of Kotlin. Absolutely handy when you want to add some feature to an object without writing too much boilercode everytime or without inheriting the class to get access to it’s methods.

Like when you want to fetch text of an EditText-

fun EditText.text(): String {
return this.text.toString()
}
// use it like this
val userEnteredName = nameEditTextField.text()

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

fun View.gone() {
visibility = View.GONE
}
fun View.visible() {
visibility = View.VISIBLE
}
// use it like this
recyclerView.gone()
emptyView.visible()

5. Null Safety

After going through the pain of NullPointerException a million times in Java, this is, I think, my 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 (?.) have been a friend in disguise where you can make calls from an object but the code will be by-passed automatically if that object is null. Thus eliminating the need for null checks or NullPointerException try-catch boilercode.

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

val people : List<Person>?  = emptyList()// create a new object for Jane is not found in the list of people
val jane = people?.map {it.name == "Jane"} ?: Person(name = "Jane")

6. When

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

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

when {
people.isNotEmpty() -> { print("Show no stats!") }
people.any { it.address.city == "Mumbai" } -> { print("Include Mumbai in stats too") }
people.none { it.address.city == "Surat" } -> { print("Exclude Surat from stats") }
person == "Jane" || person == "John" -> { print("Include all stats") }
}

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 am needed 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 a tip of an iceberg when it comes to how Kotlin have helped revolutionise the way we code optimally and concisely in Android. These were the most useful features of Kotlin that I find myself using everyday of coding Android Apps. There are still many that I intent to write about shortly.

Till then, Happy coding in Kotlin!

--

--

Priya Sindkar
Novumlogic

Sr. Android Developer 💚 | Believer in neat code and clean architecture | Loves to read | A coder through and through 👩🏻‍💻