After introducing Kotlin to colleagues…

At first I was little bit sceptical of the feedback (of promotion of Kotlin language in form of presentation and live coding exercise), but as soon as I started sharing my humble knowledge, valuable attention was captured and everyone was hooked in no time.

So here are the few language highlights that I’ve used as key points in my introductory presentation:

1. Interoperability

One class written in Java, other one in Kotlin, same project, same Java dependencies (with huge and high quality eco-system of Java libraries). Everything working together seamlessly. If you are lazy, you can try automatic converting code to Kotlin with two clicks in IntelliJ (even though, that does not work all the time, but gives you a good hint how to do it). Code stability risk during migration can be lowered because conversion can be executed in stages. This kind of full interoperability is not seen in other JVM languages — for instance Scala (one of the most popular) which reinvents some of the types and libraries.

2. Multi platform target

JetBrains (inventors of Kotlin) are very ambitious, supporting even native platforms — so you can create Win32 apps, program iOS UIKit or directly handle unix sockets. Finally language that you could use almost anywhere: from server, desktop, mobile, web client (it even transpiles to Javascript). I encourage you to check Kotlin native code samples. What more to wish for?! And I especially want to point that even though Kotlin is currently mostly used for Android, it is not bounded by it in any way.

3. NPE protection

NPE (NullPointerException) — in Java, String is not consistent — most of the time it has normal expected string content, but sometimes it could be null. Even though this is good resource wise, it imposes lots of bugs — just remember seeing someone write userName.toLowerCase() without knowing that userName String is null. Elegant unwrapping optional (nullable) values is introduced in Java 8 (but it is not imposed). Unwrapping of nullable types is set as mandatory in Swift language. So this is certainly important thing to do, and it is integrated into nature of Kotlin language. Pinterest reduced 40% of their null pointer errors on Android only by migrating their codebase to Kotlin. So I think this is good proof for such valuable investment — less maintenance cost and happier developer who can do more features and less bug fixes.

4. Promoting immutable types

It is encouraging you to use val (immutable variable) instead var (mutable variable) — because developer (me first) tends to be lazy and don’t use final keyword so often. All collections are also immutable by default — because quiet often we don’t have to add/remove elements in such list, and when we do (want to mutate list) we are marking such list explicitly which is informing the developer that this list is prone to changes. This approach tries to reduce code complexity, improve readability and gives us nice intro to functional programming.

5. Promoting functional approach to problem solving

With Kotlin I finally start using map, filter, reduce. I was never quite fond of Java streams (they seemed to me a little bit off in OOP first language). But in Kotlin it seems natural and is really fun to use. Especially when you can shorten the function to single expression function and get reusable coding blocks (I recognise Javascript influence here) without extra braces and return statement (so our brain can parse code faster and focus more on business logic).

fun filterLastFiveCitiesWithNoBobsUnderAge(persons: Array<Person>, age: Int): List<String> = 
persons.filter { it.age < age }
.filterNot { it.name == "Bob" }
.map { it.city }
.takeLast(5)

6. Syntactic sugar and boilerplate reducers

data classes (replacing POJO classes and obtaining the default implementation of equals(), hashcode(), toString() and copy() functions), one liner singleton, multiline string (how many times I just wanted to write Json in JUnit test)…

Data class

data class Money(val ammount: Int, val currency: String)

Singleton

object Singleton {}

Multiline string

val json = """
[{
"name": "John",
"age": 31,
"city": "New York"
},
{
"name": "Alice",
"age": 42,
"city": "Vukovar"
}
]
"""

7. Extension function

One of the most interesting feature during live coding was extension function, that enables you to write human readable code — DSL — shown in the below Integer extension function, percentage calculator example (modified snippet from Kotlin (Google I/O ’17) event). Finally syntax that enables us to write less code which has more functionality, while at the same time staying in human readable form. That is what I liked in python — you can read it like plain english (no wonder it was chosen as first programming language on MIT).

//instead of writing standard method callval ticketsTax = 25.percentOf(tickets)// you can make infix function that enables you to write human readable code, like thisval popcornTax = 25 percentOf popcorn// infix inline single expression function declaration
infix fun Int.percentOf(money: Money) = money.ammount.multiply(BigDecimal(this)).divide(BigDecimal(100))

Here are raised concerns and questions that come up during presentation:

Performance

After compilation the generated bytecode is almost the same as from Java. I read through quite few benchmarks and reports and and at the moment I see no considerable runtime performance difference. Notable exception is build time which is longer and this might be the pain (especially for Android devs). But with each newer version of Kotlin (which has short release cycles), there are improvements and optimisations, so I expect things to catch up quickly.

Another language

In world of never ending list languages and frameworks, even JVM languages: Scala, Groovy, Clojure; or high performant languages like Go. Where to start, which language to learn? Kotlin just came up into top 50 most popular languages. It has a long way to go with Stackoverflow knowledge-base and tutorials. But personally, after small familiarity with Swift, little experience with NodeJs, the learning curve was extremely fast and pleasant. I would even say that Kotlin kicks both languages.

Conclusion

After the presentation I received many good comments. Even though I breached the time box, nobody left but even contributed with more ideas and open questions. One colleague said that now he cannot wait to make something with Kotlin. So I would say that world is ready for another language. And, I already received few more pings (in following days) that I should start preparing following presentation. So what would it be — deep dive into functional programming, Rx, coroutines, or…?

Only thing I am sure of is that knowledge is the only value that is being increased when shared, so keep on reinforcing what you know and passing it on.

Besides kicking volleyball, playing jazz piano, I design software, manage team and myself in the first place