Posted on

Android Top Things I Like About Kotlin


android kotlin
Designed by Freepik

 

Hey there! Should you take the time to learn Kotlin? It’s a relatively new language, it’s definitely better than Java. And when web developers have their languages evolving so fast it’s kinda irritating to use the language version that was released more than 10 years ago. Yes, we can use Jack&Jill (which doesn’t support Instant Run yet) or Retrolambda, but that’s still the language level from 3 years ago. So let’s talk about more sweet stuff Kotlin introduces

In the previous post I wrote about null safety, which I consider the most important pro of this language. Nice POJO implementation and more. But the question comes

OK, Kotlin is great, fantastic. But in what projects can I use it?

One thing for sure – the perfect one is app which just you who work on it. Then you won’t have any issues. But when it comes to a team, it’s more complicated. Kotlin was developed by JetBrains for their own purposes in 2010, of course they use it for sure. Even if you agree with the team for everybody to write in Kotlin and better to switch completely, if ever I think. Even so, what about new team members coming and having to learn new language, if so. The good point is that it’s not as huge as it sounds. It feels like extended Java in most cases. Which is great and I think it’s just as hard to learn Kotlin as learning new big mindset switching library like RxJava. One thing to consider, need to test app build time for Java and Kotlin versions.

Two more things: Kotlin is relatively new thing. Version 1.0 was released just a year ago, so you get this new wave kind of feeling which is awesome. The second is Kotlin here to stay. JetBrains use it in their software instead of Java. More companies switch to it

String Interpolation

OK, let’s get to coding. First thing today is gonna be string interpolation. This one is amazing. Let’s say we have string firstName and lastName. We want to print ‘Hey, my name is Ihor Klimov. In Java we would do

Look at Kotlin solution

How often do you make concatenation with pluses in logging? Imagine how your logs would look with Kotlin! And use ${someMethod()} for methods

Assigning Variable With If/Else

In Java how often do you have few branches in which you assign a value. And then use it after that. Let’s say we have parking price calculation based on week day like this

first line is here just for initialization. Luckily, there’s a ternary operator, so we can assign value in one line

But it’s impossible if there’s more than one line of code for each branch or more than two branches and we go back to first way or you just extract that code into a method with return value. In Kotlin we can assign with if/else statement.

I still think it’s better to extract the whole thing to method if there’s too much code into something like

Extension Functions

If you worked with Data Binding adapters and enjoyed them, then you’ll love this one! What Data Binding Adapters allow to do is basically having a lightweight extending any view behavior and creating custom attributes without the need to extend view itself. Just by creating a static method.

What Kotlin Function Extension does is pretty similar. You can extend any class’ behaviors without the overhead of creating new class and extending it. Just by creating a function. Don’t know who came up first with the idea – Data Binding guys or Kotlin or even it comes from some other languages.

Let’s say we have LatLng class with just two fields or more and it comes from some library. What we want is add calculating distance to other LatLng. Let’s see what we can do in Java

In Kotlin it would look more natural with extended  functions

And for single argument functions like this you can add infix modifier which will allow you to call method without dot and parenthesis which looks like custom keyword, pretty cool

If Null/More Null Checks

One more great thing is inline if not null/else. How often do you check if object is not null then access field, otherwise return something. The most common and simple that I have is on getCount for RecyclerView.Adapter method or any Collection.

Let’s say we have a nullable list and if it’s null, we return count 0, otherwise its count. In Java it would look like this

In Kotlin it’s simpler

First ?. prevents from NPE, but it will return null though. In that case we add ?: which handles object null case.

 

That’s it for today. Thanks for reading, don’t forget to subscribe, follow me on Twitter, G+, Facebook and share with friends if you think they will benefit from it!

 

 


  • Osama Inam

    What is the main purpose of Kotlin in Java. How can it help us ? Will it help us compete with the looks and designs of other platforms?

    • It’s more minimalistic than Java. Check this post and look how many lines of code there for POJO’s required https://goo.gl/L2Nwk4

      If you want something want something fresh – go for it