Sunday, May 21, 2017

What's up with Kotlin?

This year Google I/O event gave more to chew for developers than any time in the past. If you had not had a chance to take a look, you can re-watch it here

In the event, much to developers surprise Kotlin got an official nod for Android development(This has been brewing for some time, especially with Android's proximity with IntelliJ). Where did this language come from? Is this going to be the next benchmark for Android development segues for something else? I am sure as fellow developers in the industry this news may either tickle your mind or run a riot in your mind.

No rush 

No need to change your existing Java android applications to Kotlin. With that said, you wonder, why Kotlin?
The answer is subjective. Let me share my thoughts the long way first, finally my concerns. Java for long has (and is) been the most popular language amongst developers. Popularity for this language is due to - its strong OO principles, JVM, Enterprise & open nature and finally resource availability. When we put all of them in perspective we realize, why Java has been patronized so much in the market.

If all is well with Java why take an alternative route? That's a great question. Although Java has been great, Kotlin has been silently growing for the past 5 years. A lot of enterprises adopted it, built robust solutions that have performed as well as any Java, J2EE based application, with lesser development pain. Especially when it comes to type-safety and null reference issues. You may want to read this somber article from the creator of Null reference exception. Although it goes all the way back to early 70's, still a hard pill to swallow. A big win for Kotlin was to get rid or at least attempt to rid of null reference exception. It has seen amazing success in their pursuit, so far. If this is not an impetus for us developers to give Kotlin a try, what else can be?

This is not all, Kotlin has more. There is a lot of code clutter in Java, we can honestly get rid of it. Here are a few cases, like getter's-setter's, constructors, etc. Kotlin addresses a lot of these bare bone issues. As a language gets older, it gathers some dust and redundancy. It's tough to change functionality that was backbone for its success. The impact of these changes cannot be surmised without alienating some developers. If you have not noticed, we already see enough noise about Java 9 on some breaking changes.

How different is Kotlin from Java? 

To be clear, Kotlin runs on top of JVM. This implies that all underlying framework we have use in Java will also work without any interoperability issues in Kotlin. Remember the storm with VC++/VB to .NET, or quite recently ObjectiveC to SWIFT. No such nonsense with Java & Kotlin. The reason for easier integration can also be due to the fact Kotlin has been around for some time, may be new for Google and Android. For all Java developers, this may seem to be odd, but believe me, I was able to get running on Kotlin with a full fledged application in 2 days time. I am not talking about some HelloWorld or a ToDo list application. A complete end to end solution that was on Java. All this with no knowledge in Kotlin to start with.

Here are a few articles for reference

  • https://kotlinlang.org/docs/reference/
  • https://kotlinlang.org/docs/tutorials/. 
  • If that was boring, please head over to PluralSight or Udemy for beginners course. You'll be surprised what $10 can do.


The fundamentals of functions, enums, classes, interfaces, abstract behavior, all remain the same. It's the syntax and in few cases code placements have changed to accommodate modern programmers. Rest all is plain old Java. This might bring some relief to you.

My complete GitHub for starter material on Kotlin is available here. Feel free to clone or even take a look

To give you a simple example from my samples here's the traditional HelloWorld application.

class HelloWorld(val firstName: String?) {
init {
println("Init method fired");
}
fun sayhello() {
if(firstName != null) {
println("Hello ${firstName}");
} else {
println("Hello, welcome to KOTLIN world");
}
}
}
fun main(args : Array<String>) {
var hWorld = HelloWorld(null);
hWorld.sayhello();
}

A more concise example with a simple class

Person.kt
package Classes

import java.time.Year
import java.util.*

/**
 * Created by sriramk on 5/20/2017.
 */
class Person(name: String, age: Int) {
    var name: String
    var age: Int

    init {
        this.name = name;
        this.age = age;
    }
    constructor(name: String, birthYear: Year) : this(name, Calendar.getInstance().get(Calendar.YEAR) -  birthYear.value) {

    }
    //constructor(firstName: String, lastName: String, doby: Int)
    fun yearBorn(): Int = Calendar.getInstance().get(Calendar.YEAR) - age
}

SimpleClass.kt
package Classes

import java.time.Year

/**
 * Created by sriramk on 5/20/2017.
 */

fun main(args: Array) {
    var person = Person("Krishnan", 38)
    println("Name: ${person.name}, Age: ${person.age}, Born in the year: ${person.yearBorn()}")

    var person2 = Person("Indhu", Year.of(1980))
    println("Name: ${person2.name}, Age: ${person2.age}, Born in the year: ${person2.yearBorn()}")
}

As you can see, changes are subtle. The underlying system works the same way. These subtle changes are a neccessity to help developers of today, run faster and build more robust applications, without succumbing to same pitfall issues we tradinationally have.

Is there nothing new, just these? Not really, few additions are very interesting. Say singleton pattern and data pattern are nice new additions.

I plan to add more samples to this repository - Threads, more on collections and memory handling & DB operations. If you plan to adopt Kotlin for your enterprise, there's nothing to loose. It's perfectly interoperable with Java, so go for it.

Is everything good with Kotlin and Google? 

Hmmmm.. I have some degree of concern. "Go" has been taunted as a language to watch out for. I have read plenty of articles alluding android development with "Go". The question that begs for an answer is, why Kotlin and Java. Why not take a step like APPLE did. Jump the bus with "Go". Yes, there may be issues with platform, libraries, but that's nothing new to Google. They have such a strong empire to articulate any problem and provide a solution. To me, jump to Kotlin seems to be more experimental.

Although my concern is only on Android and Kotlin, I strongly believe, industry as a whole will roll along and benefit out of Kotlin. SpringBoot has already made this move and we all know how popular it has become in the recent past. Check it out, if you have not.

As always, thanks for your time. I 'd appreciate your comments, thoughts or anything you wish to share. Until next time,  "Happy Programming"