Five Features That Make Kotlin Awesome

Five Features That Make Kotlin Awesome

  • map-filter

I hope that by now, you are convinced that Kotlin is truly awesome. Here are 7 of my reasons, that make Kotlin unique, powerful and intuitive to work with:

 

1) Underscore Seperator

We read code more often than we type it. Have you ever experienced reading a large integer, and found yourself counting the digits just to see how large it actually is?

var maxInteger: Int = 2147483647

Luckily for us, Kotlin allows us to place underscores between the digits to get a quicker read, without actually affecting the number:

var maxInteger: Int = 2_147_483_647

This gives you a better read by letting you distinguish between smaller and larger numbers more easily without having to count all the digits every time.

 

2) If-Expressions

You might be familiar with the way of initializing variables based on a condition in other languages. For example, in C#, you might do something like this:

bool isEighteen = (age >= 18) ? true : false

This is obvious enough if you know what it means, especially in this context. But when I was starting out, I had no clue what was going on reading more complicated expressions. Kotlin makes it more understandable by incorporating the if-keyword itself.

bool isEighten = if (age >= 18) true else false

You can also do things, besides assigning based on the condition:

bool isEighten = if (age >= 18) {

print("You are old enough to drive a car!)
true

} else {

print("You are still very young!)
false

}

 

3) Null-Safety

This is the features ¬†you have most likely already heard of. Compared to Java for example, Kotlin prevents us from assigning null to a variable if we don’t explifitly allow it to accept null.

var cantBeNull: Boolean = null // gives us an error

We have to use a ? to make it valid.

var cantBeNull: Boolean? = null // compiles just fine

This prevents us from NullSafetyErrors at runtime.

 

4) Return When

This is a feature introducted to clean up our code a little bit. When you could return multiple values, based of many conditions instead of typing this:

fun getCommentBasedOnAge (age: Int){
   when(age) {
     when (age){
        1 -> return "Hey Baby"
        2 -> return "You are getting older..."
        3 -> return "Already 3 years old!"
        4 -> return "Two more years until you go to school"
        5 -> return "The last year before school..."
        6 -> return "You are finally in first grade!"
        else -> return "You are too old for this..."    
     }    
   }
}

 

You can simplify it by putting only one return keyword before the when statement itself:

fun getCommentBasedOnAge (age: Int){
   return when(age) {
     when (age){
        1 -> "Hey Baby"
        2 -> "You are getting older..."
        3 -> "Already 3 years old!"
        4 -> "Two more years until you go to school"
        5 -> "The last year before school..."
        6 -> "You are finally in first grade!"
        else -> "You are too old for this..."    
     }    
   }
}

We just removed six unneccessary return keywords that did not add anything to our code.

 

5) Map-Filter

 

 

Map-Filter inside of Kotlin has a pretty neat way of functioning. They work with lamba expressions, which is not particularly unique to Kotlin. But as opposted to other languages, you can use the it keyword to describe all items you want to manipulate. Here is an example of it in action:

names
.sortedBy { it }

This simply sorts all the elements inside of the “names” alphabetically. You can add more actions by putting them underneath each other:

names
    var names = listOf("Bob", "Robbinson", "OG", "John")
    names = names
            .sortedBy { it }
            .filter { it.length >= 3 }
       
    
    names.forEach { n -> println(n) }

This example first sorts the names, and then removes all names that are not at least three characters long. Afterwards, we print out our list.

There are obviously many more awesome features you will find in Kotlin. For this post, I chose these five as they are very valuable in many situations you will encounter.

I hope you enjoyed them and have fun trying out all other features you can find.

Cheers, Gabriele

Leave a Reply

Your email address will not be published. Required fields are marked *