After introducing Kotlin to colleagues…

Image for post
Image for post

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 Money(val ammount: Int, val currency: String)
object Singleton {}
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))

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…?

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store