Advantages of Kotlin from practical way

Nearly every Java programmer heard about Kotlin. If not, please go to Kotlin webpage or listen to very good polish talk by Paweł Byszewski or if you are not polish, listen to Hadi Hariri talk from GOTO conference. That post will not be about what is Kotlin, but why you should or not switch into it.

The best about Kotlin (from business point of view) is its interoperability with Java. You, like many big companies (including Jetbrains, Google, Uber, Foursquare, Pinterest, Gradle, Evernote, Pivotal, Coursera, Atlassian), you can test it by writing as little as one class in K and then if you think it is better than other JVM languages, you can write all or some of new code in K, and Java code will work as if K code will be Java’s code (top-level functions will be members of (not)special classes).

That’s the business, but what’s about programmers? We are writing code, so language that we use, most affects us. Kotlin code look like a mix of Java, JS, C# and some other. In fact this is mix of other languages. Jetbrains people, while there were planning that language, they simply bring best stuff from every language they know, and put them in one language. Programmer must all-the-time learn new things, for example for his(her) self-development. Learning new language is opportunity for that. Kotlin is an excellent way for achieving this because, like mentioned above, it has concepts from different languages. Modern, secure, flexible and easy is probably the shortest description of K.

So, what Kotlin have, that Java doesn’t have? That list is more than big, so I won’t write most of them, but only most important/interesting for me.

  • Declaring objects by val/var keywords. This is not very big feature, but makes code much more readable (it’s my opinion) and let us consider if variable that we are declaring isn’t better to be final.
  • Extension functions and what it means. That destroy reasons of creating “Utils” or “Commons”, because you can add your own methods to classes. Many Kotlin libraries was created for extending bigger libraries or frameworks. K standard library is in big percent extension functions for JDK.
  • Lambda as `{}` is small change, but by this, you can make your code more beautiful.
  • Operator overloading is a next way to make code more readable. You can simply add 5 days to current date by date + 5 or compare Strings by ==.
  • Delegated properties means very easy use of lazy loading, useful in Android for writing code near declaration and with no error.
  • K classes doesn’t have fields. It has something nearly same to it named properties. The difference is the properties (if they are public) have automatically made setters and getters and you can write custom version of them some time later without breaking code that use them (properties).
  • Last, but not least, Coroutines. If you ever written any multi-threaded code you know it is demanding and easy to fuck-up. Coroutines are easier, safer and has better performance. For them, there will be another post, so wait a bit.

Many of Kotlin differences comparing to Java are influenced by “Effective Java”. More on that topic you can read at that page.

I haven’t mentioned that, but Kotlin is not only JVM language. It can be also compiled to JS and to platform native code (the same as compiled C) thanks to project Kotlin/Native (you can use libraries written in C. From 1.2 version of Kotlin there will be some kind of libraries structure. There will be platform dependent libraries, like today, other for JVM, other for JS… From 1.2 you will be able to write “common modules”. It will work on every platform that Kotlin is supporting.

So, I think you have nothing to lose and (probably) many things to gain. Simply give Kotlin a try.

If you have any suggestions or questions write comment or directly contact me.

Like what you read? Give Oskar Drozda a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.