This article was published in DVT Insights and ITWeb, and an extract in Brainstorm magazine.
Before the announcement of first-class support for Kotlin on Android at Google I/O 2017, not many had heard of the programming language that was suddenly stepping into the limelight. Kotlin 1.0 had been released only the year before, following many years of development by a team of JetBrains developers based in Saint Petersburg, Russia.Why did Google decide on such a relatively unknown language to join giants Java and C++ in first-class support? Will it become widely used elsewhere, such as server-side and the web? This article will touch on a few key points on why Kotlin is a great language worth learning and ultimately using in production.
Kotlin supports more comprehensible code
Comprehensibility, not conciseness, should be used as a measurement of how good a programming language is. We spend so much time reading code, not for its own sake, but rather to understand it. Kotlin supports writing more comprehensible code in many ways. For example, data classes are like POJOs (Plain Old Java Objects), but the accessors, mutators, equals, hashCode and toString methods are implied. Therefore, these methods do not have to be written/generated, tested and maintained. Another example is named arguments, where the programmer can be explicit about which argument is being specified. Calling legacy code - that often have lengthy parameter lists - using named arguments can clarify code significantly.
Kotlin is interoperable with Java
Java code can be called from Kotlin, and Kotlin code from Java. This provides Kotlin with the Java ecosystem of frameworks and libraries. The developers of Kotlin have also made the smart decision to use the Java collection framework, rather than create their own. In terms of interoperability, this means that no transformation between Kotlin and Java collections is necessary. Motivated to overcome the shortcomings of the Java collections, the developers of Kotlin provided the ability to create extension functions for any class, which can be called as though it formed part of the original class.
Supporting safer code
Kotlin supports writing safer code - code that is naturally less buggy. It is a statically-typed language, so the compiler will pick up errors before the code is even run for the first time. Kotlin forces you to think about null values very carefully. If a variable could possibly be null, you are forced to declare the type as nullable and handle the null case wherever the variable is used.
Kotlin encourages you to think about mutability versus immutability, with idiomatic Kotlin favouring immutability. Kotlin limits the extension of classes by making classes final by default and allowing for explicitly-restricted/sealed hierarchies, thereby ensuring that subclasses are not inadvertently allowed to break contracts.
A modern language
Kotlin is a modern programming language. It is object-orientated, but also allows top-level functions and constants to be declared without the need for irksome utility classes. Kotlin allows for functional programming, with lambdas and higher-order functions.
Excellent tooling
Kotlin has excellent IDEs (Integrated Development Environments) available from JetBrains: IntelliJ Idea and Android Studio. At the time of writing, there is also an Eclipse plugin and Emacs mode available for Kotlin. The JetBrains IDEs have Java to Kotlin converters, which can be significant timesavers and often only require minimal checking and clean-up. They also have handy suggestions to help you on your journey towards writing more idiomatic Kotlin.
References:
Books: • Jemerov D and Isakova S 2017. Kotlin in Action. Shelter Island, NY: Manning Publications Co.
Videos: • Introduction to Kotlin (Google I/O '17) • What can Kotlin do for me? (GDD Europe '17) • 10 Kotlin Tricks in 10 ish minutes by Jake Wharton
Webpages: • Wikipedia article
Comments