Kotlin is Funky!

Kotlin

A couple of years ago I took a quick look at Kotlin and came away with the impression that it felt like a band-aid over Java. Not much to see here.

Recently, however, I had to use Kotlin to complete a coding challenge for a job application. I was pleasantly surprised. Not only is the language a joy to use, it is very intuitive, I was able to pick it up on the go, pulling information from google as required. And I must say, it’s one of the funkiest language I have ever used. For example:

I love it! I understood why Android coders had moved to Kotlin so quickly, but not why Spring developers were embracing it with enthusiasm. Now I know.

Pragmatic Joy

Kotlin has that magical feel, like coding in Ruby, but you’re actually closer to Java. It is a joy to use. And it is a pragmatic language, written by developers for developers. The ability to inline lambdas if they are the last argument in a function call, and language features like the infix operator, make it easy to create domain specific languages. The code almost looks like English at times, here is a simple object I created to perform a flood-fill operation:

Floodfill

Kotlin has strong support for functional programming. With value and variable semantics baked right into the language, support for non-null references, smart casting, if/when expressions, data classes, objects and companion objects for singletons and static methods, coroutines, and a powerful runtime library, Kotlin proposes to bring Java forwards twenty years. By eliminating primitives, a whole bunch of issues have been removed.

The Sleeping Giant Awakens

I’m not sure if Kotlin was the spark, but the Java world is on fire. Not only are technologies like Spring Boot and Spring Cloud invading the Enterprise, Java itself is aggressively evolving.

With Java on a new six month release cadence, and rapidly adding features like records, you may be wondering if it is worthwhile moving to Kotlin. It depends on your preferences. Personally, I find it easier to express myself in Kotlin, it is more flexible and concise, and less ceremonious. I feel happy coding in Kotlin, it’s the same feeling as when I’m coding in Ruby – which is remarkable given that Kotlin is one hundred percent compatible with Java. But if you are a Java fan, you may be happier where you are, and as mentioned it’s rapidly acquiring many of the features Kotlin boasts. Here is a good presentation by Jake Wharton on the Java vs Kotlin topic:

Summary

Kotlin not only targets the JVM, but can transpile to JavaScript, and there is a native compiler in the works. JetBrains are also working on a dedicated Kotlin to WASM compiler. As expected from JetBrains, integration within Intellij is first class. Being such an expressive language, a number of Kotlin libraries have been written to simplify existing Java frameworks and cross platform development, such as TornadoFx, Android-KTX and KMM – as per this example from KAndroid:

// instead of: 
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
    override fun onQueryTextChange(q: String): Boolean {
        update(q)
        return false
    }
    
    override fun onQueryTextSubmit(q: String): Boolean {
        return false
    }
}) 

// just write:
searchView.onQueryChange { query -> update(query) }

Kotlin is so familiar for Java/C# developers, you should be effective immediately. If you are interested in getting started in Kotlin, Google has a free course available on Udacity here.

And there are more free learning resources available here, and here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s