How does Kotlin work? Have you ever focused on this specific language? In reality, it hasn’t been talked about for a long time, and indeed, Kotlin is a programming language that has become famous in relatively recent times: according to Google Trends, it is only since 2017 that it has gained some relevance in user searches.
Yet, it is not so new: the first announcement by Jetbrains dates back to 2010. What happened, then, in 2017 or shortly before, that made it popular?
Is Kotlin only for Android development?
An event that, to date, of Kotlin is also the cause of one of the main misunderstandings related to the language: Google’s support and the choice to consider it the first choice for Android development, to the detriment of Java.
Since then, Kotlin has seen a surge in popularity, obviously in the Android ecosystem, but it has also been somewhat pigeonholed as “the language of Android“: the author has worked in Kotlin for years and is the maintainer of the most adopted open source mocking framework in Kotlin (https://github.com/mockk/mockk/), and just for having written “Kotlin” on the CV has been contacted several times for Android developer positions, despite knowing absolutely nothing about this ecosystem.
But then, how does Kotlin work? Well, this is, more than many others, a general-purpose language: not only is it usable in all contexts where Java can be used, therefore all backend programming, but it is also very suitable for entirely multiplatform native development, similar to tools like Flutter, which allow you to write the code once and compile it for different platforms such as Android, iOS and Desktop applications.
But what are the main features of the Kotlin language, how does it work? Is it a language for experts or is it also suitable for developers at the beginning?
Firstly, because of its JVM-oriented nature and its history, it is a language that Java developers will find very easy to learn: if you have some experience with Java, you can expect to become perfectly productive in Kotlin within a couple of weeks.
And honestly, you have no reason not to do it, since Java and Kotlin are perfectly interoperable: Java code can invoke Kotlin code and vice versa without any problem, and existing Java codebases can integrate parts written in Kotlin without effort, so much so that it is a fairly common practice in various companies to write directly in Kotlin the new parts of Java applications.
Who makes him do it? Why not continue to use Java which, after all, has been doing its dirty work for years?
Kotlin actually has many features that every Java developer has always wanted to have, above all the one that solves the famous “billion dollar mistake”: in Kotlin, in fact, all variables are non-nullable by default, and the risks of the dreaded NullPointerException are greatly reduced at compile time, because a piece of code that accesses a null variable is immediately identified by the compiler and not during execution.
But that’s not all, because Kotlin also has many features, such as data classes, extension functions and lambda expressions, which make it up to 40% more concise, and therefore more readable, than Java: one of the most famous criticisms of Java is its verbosity and the huge amount of lines of code it requires for the most trivial tasks, Kotlin does the exact opposite and allows developers to write only the code actually necessary to solve problems, not the boilerplate.
And it’s not over: if you have developed non-trivial applications in Java, you will surely know how complicated it is to manage asynchronous programming and multithreading; and if this is your case, you will love coroutines and the tools that Kotlin offers for asynchronous programming, from suspend functions to evolved objects like flow and channel.
Why and when to choose Kotlin
Is that all? (Just saying) Why choose Kotlin and not one of the other languages interoperable with Java, then?
We have already spoken of some areas of use, such as data science and cross-platform programming, where Kotlin is a clearly better option than other JVM-based languages such as Scala, Groovy or Clojure, but there is another detail that makes it superior to these last: adoption.
When talking about a tool like a programming language, having a critical mass of users who use it is fundamental, but quantity is not enough: it is also important to have the support of important players who somehow act as guarantors of the quality of the tool – after all, what would a framework like React be without the support of Meta?
Well, in this sense Kotlin can count on at least two very respectable authorities: first of all, as we have already mentioned, Google strongly supports Kotlin, but we must also not forget that it is a language born within a company, JetBrains, whose core business is producing IDEs, so the support for Kotlin’s tooling is absolutely excellent and IntelliJ offers a really big help to developers, making the development experience extremely ergonomic and simple.
Conclusions: More than just Android
Firstly, it is a strongly typed language, and as such “forces” you to learn to think about the data that our functions and our classes exchange and greatly reduces the possibility of bugs; then, as we have already said, it is a very concise language, which allows you to focus on the fundamental concepts of the code we are writing rather than wasting time with boilerplate; finally, it has a very well designed, consistent and complete standard library, which allows not very experienced developers to come into contact with very well written software.
In short, whatever kind of experience you have and whatever area of software development interests you, Kotlin definitely deserves that you dedicate some time to it: if then you wanted to contribute to MockK, the open-source mocking framework of which I am the maintainer, you would certainly do a good thing.