As great as it is, being a developer is no easy task. You have to constantly learn and adapt to the ever-changing world of programming. Technology has been advancing at a staggering rate and so has software. Code needs to be written fast, but it also has to be safe and easily maintainable. Naturally, existing programming languages had to evolve over time to fit these needs. This also gave rise to a plethora of new modern languages, aiming at exactly that – make the life of developers easier, hence improve code quality. Today, we are focusing on one of the most promising amongst them – Kotlin.
What is Kotlin?
Kotlin is a statically typed general-purpose programming language, developed by JetBrains, the company behind the popular IntelliJ integrated development environment (IDE). In 2017, shortly after its official release, Google announced that Kotlin was now officially supported for Android development. This acted as a catalyst and soon enough it had already become the fourth most loved language, getting adopted by many big companies, such as Airbnb, Atlassian, Coursera, Gradle, Pinterest and Uber. Currently, Kotlin is not only one of the three officially supported Android development languages but it is also Google’s preferred one as Android is actively becoming Kotlin-first. What drove so many large companies to get aboard the hype train so fast? What caused this change of heart and made Google neglect Java, which has been the driving force behind Android development for over a decade? Let’s find out.
Why use it?
1. Easy learning curve
Kotlin targets the Java Virtual Machine (JVM) and therefore, compiles to Java bytecode. Unlike another JVM language – Scala, which JetBrains clearly took great inspiration from, Kotlin was designed for clarity and got rid of most of the complexity. If you have some prior experience with object-oriented programming (OOP), you should be able to get up to speed in no time! The official documentation is very thorough and well-written, and the community is very passionate and engaged, ready to answer all of your questions, be it on stackoverflow, reddit or the official forums.
2. Java interoperability
Kotlin code is completely interchangeable with Java code and mixing them both in the same project feels like a walk in the park. IntelliJ (as well as Android Studio), being developed by JetBrains themselves, features flawless Kotlin integration. You can paste Java code into a Kotlin file and vice versa, and the IDE will automatically convert it for you. This conversion can also happen with the simple click of a button. On top of that, Kotlin is fully backwards compatible, so you can build upon your legacy projects with it as well. This is extremely useful if you are still getting into the language as you have the opportunity to use it only partially until you feel confident enough to fully embrace it.
3. Leaner syntax
The biggest issue with Java has always been the huge amount of boilerplate due to its high verbosity. With Kotlin, in comparison, you have to write a lot less code to achieve the same results. For one, you can safely omit semicolons in 95% of the cases – they will be automatically inferred. Similarly to C#, property types will also be inferred. String interpolation is present as well, allowing you to inject variables directly into the string. There are also smart casts which, once you’ve checked whether an object conforms to a certain type, allow the compiler to track it down and internally cast the object to it within the scope.
“Switch” statements, on the other hand, are replaced by the “when” block – a far more concise structure, not restricted by argument types, that smart casts everything within its scope. Moreover, it can be used both as a statement and as an expression, returning a value. It is even allowed to omit the argument completely. Using it should definitely take precedence over longer if-else statement chains to greatly improve code structure.
4. Data classes
We often need classes that contain data and nothing else. Kotlin introduces data classes, which serve that very purpose. Their getters and setters, as well as derived implementations of methods like equals(), hashCode() and toString(), are generated automatically. Another cool automatically-generated function is copy() – it accepts the same arguments as the constructor, copying the whole object over to another reference, allowing for alteration of its properties on the spot. Data classes alone can save you from tremendous amounts of bloat, often shrinking classes from more than 50 lines down to a single one. If you need multiple constructors, just assign default values to some of the parameters and you will be able to omit them upon object instance creation.
5. A world without NullPointerException
The null reference – the “billion-dollar mistake” – a reference which does not point to a valid object. Attempting to access a member of such a reference, or rather – dereferencing it, would result in an error. In Java, a NullPointerException is triggered, terminating the program if left unhandled. And despite giving birth to some incredibly funny Java/Android memes, it is still a nightmare to work around. Kotlin finally puts an end to this struggle.
You can still end up with the exception, but you might need to work extra hard for it. By default, all objects are non-nullable. You have to explicitly declare them as nullable and even then, the compiler wouldn’t let you access them until a special safe call operator is used. Except for some extremely rare occasions of bad interoperation with Java code, the only way you can get this dreaded exception is to use a not-null assertion operator instead or by throwing it yourself.
6. Coroutines – no more annoying callbacks
7. Functional Programming
Unlike its older sibling, Kotlin aims to be as much functional as object-oriented. As in Python, functions in Kotlin are actually values that can be assigned to variables and passed as parameters. There is a great focus on lambda expressions, and they are powerful, while their syntax and implementation are among the best. Extension functions are also present, with which any existing class’s functionality can be extended. Lists are a bliss to filter through and there are a lot of useful utilities like the five scope functions – let, run, with, apply and also. They allow you to execute a block of code on an object without the need to access it by its name. All five of them work in a very similar manner, sharing the purpose of bringing more conciseness and readability to the code. It is especially handy to use the “let” keyword for null checks, but it shouldn’t be overused as in some cases it may create an additional variable unnecessarily.
All this and more make up for code that is a lot more readable, structured and visually appealing. Benefits can be reaped both during coding, as it largely accelerates the whole process, and later on in pull request (PR) reviews, as code becomes much more succinct and traceable.
Kotlin is best known for its use in Android development but there is actually much more to it. As a general-purpose language, one of its main goals is to be able to work across a wide variety of domains. Here are some other non-Android areas where you can make use of Kotlin:
- Kotlin’s Java interoperability means that it is fully compatible with all Java libraries and frameworks. This makes it perfect not only for Android development but for backend development as well. Spring has been pretty invested in Kotlin and has officially supported it since Spring Framework 5.
- Kotlin/Native – a technology that allows Kotlin to compile to native binaries, which can run without a virtual machine. This enables development for Android, iOS, MacOS, Windows, Linux and WebAssembly. It is currently in beta and an official release is expected soon.
- Kotlin as a data science language. Yes, the community has already started creating libraries that utilize Kotlin for data science purposes. Some even believe that it will eventually dethrone Python.
To top it all off, Kotlin 1.2 introduced a new experimental feature called Multiplatform Projects. This allows for code reuse between all of Kotlin’s target platforms via a shared module, meaning that business logic can be written once and then shared across all clients, including mobile, back-end, front-end and desktop.
Things to keep in mind
Kotlin will be rather easy to pick up, especially if you are coming from Java, but it is still a whole different language. Mastering it would require a certain time investment, which should be taken into consideration.
Generally, Kotlin’s initial clean compile times are slower than Java’s but things even out on consecutive builds. However, this has been gradually improving in recent versions.
What is more, conciseness is a double-edged sword. Being able to save a few lines of code here and there sure may be tempting but it can easily get out of hand. It is important to keep performance and readability in mind and not overdo things.
Kotlin’s biggest disadvantage, however, is its resources or more precisely – the lack of them. Even though the language has been around for a couple of years, it got really popular only recently – after Google’s intervention. Despite being extremely supportive, its community is still small, thus the available resources are rather limited.
The future is looking bright
Now that Kotlin is officially Android’s primary development language, adopting it in your workflow as an Android developer is highly advisable, if not mandatory. The transition will be relatively smooth, thanks to the flawless Java interoperability. Even if you are rather inexperienced or not coming from a Java background at all, the simple and intuitive syntax of the language will surely make you feel right at home in no time.
All in all, Kotlin is a great user-friendly language that aims to tackle the limitations of Java. And by the looks of it, it might just have succeeded. We can see the whole industry shifting towards more user-friendly and productive languages. An example being Apple’s own Swift that follows suit on many of the above-mentioned features and has even struck a stark resemblance in terms of syntax. The future of programming has never looked more exciting!