Kotlin and You

If you are in android application development and haven’t heard of Kotlin, you must be from another planet. But if you’ve heard about it and still wondering what it is all about, do read on.

What is Kotlin?

Kotlin is a statically typed programming language with some really unique and awesome features such as non-nullability, high order functions etc. that are supposed to make the life of developers much easier. Kotlin has gained so much popularity in its small span of time. Kotlin is being positioned as the SWIFT for android application development. Kotlin may do to Java; what SWIFT did to Objective C.

Why is there so much Buzz?

In Google’s recent annual developers meetup known as Google IO, Google announced its official support for Kotlin to develop android applications. It is not that Kotlin was not used for android development earlier. A league of developers have been using it and raising their voices to make it an official one. Now that is it officially announced, “Kotlin” is the most talked about word these days in android community.

Kotlin was developed by JetBrains which is coincidentally responsible for developing the IntelliJ Idea platform on which the android studio, the official IDE for android development, is based.

What’s so cool about Kotlin?

One might be thinking that why are people going crazy about Kotlin? Well, there are reasons worthy of such response from developers. I spent a few days exploring the awesome and unique features of Kotlin and I would suggest you to explore it first hand to get a better context.

For invoking your excitement to dive into it, I’ll list down some features of Kotlin that make it the language it is.

        1. Non – nullability : If you have been using Java for your development, this is one of the most annoying issues of your life, gives you scary dreams, doesn’t it? But Kotlin comes to your rescue.

          Yeah, you read it Right. Kotlin has its variable declaration syntax defined in such a way that you can decide which variable can hold null values and which can not. In case, if you assign a potential null value to a not-null variable, it will be caught at compilation time itself. Thus a not-null variable will never have a null value at runtime, So no NPEs. Cool, isn’t it? So following declaration and assignment of variable “str” will give compile time error:
          var str : String = “abc”<br /><br />
          a = null // compilation error

      If you want a variable to hold null value you can define it like this:
      var b : String? = “abc”<br /><br />
      b = null // ok

    1. Smart cast
      It is the feature that you always wanted to have in your development with Java. This feature allows you to check the type of a variable then perform operation on it according to its type. For example, if the datatype of a variable is String it will allow operations defined on a String object and on the same object if it detects it is an Integer then it will allow operations done on Integer.How do we do that? Just use an operator “is” to check for the type.
      val n:Any = "Hello"<br /><br />
      when(n) {<br /><br />
      is String -> n.length<br /><br />
      is Int -> n.inc()<br /><br />
      }
    2. High Order Functions Long gone are days when function could just take data types as their parameters, as the high order functions are here. High order function are those functions which can take another function as parameters. This is another feature that we missed so much in Java.
      fun logExecution(func: () -> Unit) {<br /><br />
      Log.d("tag", "before executing func")<br /><br />
      func()<br /><br />
      Log.d("tag", "after executing func")<br /><br />
      }
    3. Default and named arguments
      Absence of this feature in Java makes the developer to add function overloading concept as all parameters declared has to be passed by the caller but with this feature you can get rid of all these just by giving the default value to the parameter whom you want to make optional while calling that method. With named argument now you can easily change the sequence of the parameter at the time of calling this is done by writing the name of the argument and then assigning the value to it. For example a function like the following:
      fun awesomeFunction(num: Int, str: String = "Kotlin") { }
      All of the following are valid :
      awesomeFunction(45, " Kotlin is pretty good")<br /><br />
      awesomeFunction(str = " i am a fan now", num = 25) //Named argument<br /><br />
      awesomeFunction(95) //usage of default argument

      Along with above features there are few others as well, like, Data Classes that remove the need of making POJOs, Multi-value return from function, Extension functions etc.. Oh No, did i forgot to mention that with Kotlin you do not need to use  findViewById()  anymore? Kotlin extensions do it for you. You can perform operations directly on IDs defined in your xml files. There You Go !

    Kotlin Or Java ?

    This is the one very question that every android application developer has in mind now. I think the answer is pretty simple, change is the only constant. With the list of amazing features that Kotlin has, one would eventually have to move on from Java. For now, one should start writing codes in Kotlin as well in their respective projects. Java will come to your rescue if you get stuck with syntax or anything because Kotlin supports full Java interoperability (another great thing). Kotlin will save one from writing a lot of boilerplate code that we all hate to write. Just give it a try, you’ll love it.

    Best Places to learn kotlin :

    I am listing a few resources here, just in case you feel lost in google search results:

    Jetbrains github repo for Kotlin : https://github.com/jetbrains/kotlin
    Kotlin KEEP : https://github.com/Kotlin/KEEP
    From Java To Kotlin : https://github.com/fabiomsr/from-java-to-kotlin
    Kotlin by Antonio Leiva : https://antonioleiva.com/
    Design Patterns in Kotlin : https://github.com/dbacinski/Design-Patterns-In-Kotlin
    Stack Overflow Documentation On Kotlin: https://stackoverflow.com/documentation/kotlin/topics

    Google IO Kotlin announcement:

    https://youtube/googlekotlinannouncement

- Akhilesh Awasthi

Leave a Reply

Your email address will not be published.