A Second Look

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.

The language was not only enjoyable to use but also surprisingly intuitive. I was able to pick it up as I went, looking things up when needed. And I must say, it is one of the funkiest languages I have ever used.

For example:

y1 = y2.also { y2 = y1 }

I love it! I had understood why Android developers embraced it so quickly, but hadn't quite appreciated why Spring developers were also so enthusiastic. 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.

It's also a pragmatic language, written by developers for developers.

The ability to move lambdas outside parentheses when they're the last argument in a function call, reminiscent of Ruby Blocks, along with features such as infix functions, makes it perfect for creating domain-specific languages.

The code almost looks like English at times.

Here is a simple object I created to perform a flood-fill operation:

package graphics

object Fill {
    var pen: Char = 'o'

    fun execute(start: Pixel, pen: Char) {
        this.pen = pen
        visit(start)
    }

    private fun visit(pixel: Pixel) {
        val target = pixel.getPen()

        pixel drawWith pen

        if (pixel upIs target) {
            visit(pixel.up())
        }

        if (pixel leftIs target) {
            visit(pixel.left())
        }

        if (pixel rightIs target) {
            visit(pixel.right())
        }

        if (pixel downIs target) {
            visit(pixel.down())
        }
    }
}

This is a small illustrative implementation, but I particularly like expressions such as pixel upIs target and pixel drawWith pen.

They demonstrate how Kotlin's infix functions can make code read almost like a description of the operation being performed.


The New Java?

Kotlin supports functional programming and offers a collection of expressive language features, including val and var, nullable and non-nullable types, smart casts, if and when expressions, data classes, objects, companion objects and coroutines.

By smoothing over the distinction between primitives and objects, Kotlin removes a number of common irritations.

It offers a more concise and expressive alternative alongside Java, while retaining excellent interoperability with Java libraries and frameworks on the JVM.


The Sleeping Giant Awakens

I'm not sure whether Kotlin helped provide the motivation, but Java has undergone considerable evolution of its own.

With its regular release cadence and features such as records, pattern matching and sealed classes, modern Java is a very different language from the one I first encountered.

Meanwhile, technologies such as Spring Boot and Spring Cloud continue to make the JVM ecosystem an attractive place to develop applications.

So, is it worth moving to Kotlin?

It depends on your preferences. Kotlin is flexible, concise and less ceremonious, while interoperating closely with Java. But modern Java has become increasingly expressive too.

If you're a seasoned Java developer, you may be perfectly happy where you are.

To be honest, I'm just as happy working in Java.


Summary

Kotlin isn't limited to the JVM. It also supports JavaScript and native targets, making it useful for cross-platform development.

As you'd expect from JetBrains, its integration with IntelliJ IDEA is first class, and Kotlin's expressive language features have encouraged the development of libraries and frameworks that simplify application development.

What changed my opinion, though, wasn't a feature list. It was actually using Kotlin to solve a problem and discovering how natural and enjoyable the language could be.

Kotlin is funky. And that's a good thing.