Ask a Dev: Should I use Kotlin to develop Android apps?

android

Kotlin is a new open-source programming language developed by JetBrains. It runs on the Java Virtual Machine (JVM) and is known for its functional programming and object-oriented features. But Kotlin can also support higher-order functions, anonymous functions, lambdas, and much more!

Nearly a year ago, Google announced support for Kotlin on Android, and Kotlin is now built into the Android toolkit. With Google’s support the free to use Kotlin is becoming more mainstream. Five-star apps that have been written in Kotlin include Pinterest, Evernote, Trello, Square, and Coursera.

I’m a huge advocate of Kotlin for Android development. Here’s why your developers should be, too!

Benefits of Kotlin

Kotlin is very similar to Java (the default language for Android). However, I think Kotlin is better than Java when coding on the JVM. For one, Kotlin code is simple and concise, and a more convenient syntax will boost your productivity, reduce boilerplate code, and allow you to focus on the development ecosystem. Simple and clear code also reduces the number of crashes, and makes it a lot easier for developers to see where a crashes is and then fix it.

kotlin
Source: Kotlin Programming

You can also use most of your existing tools (such as Sprint Boot, vert.x, and JSF) with Kotlin – especially if you’re using the IntelliJ IDEA. With IntelliJ and Android Studio, your Kotlin code can be refactored, searched, navigated, and auto-completed. These Integrated Development Environments (IDEs) also support debugging, unit testing, and profiling Kotlin code.

There are many extension functions for Kotlin, which can be found here. For example, you can add methods to classes without modifying the source code, and you can easily build auto-completions, and with little overhead.

Kotlin code is simple and concise, and a more convenient syntax will boost your productivity.

Maybe the greatest advantage of using Kotlin is the null safety feature. Kotlin aims to avoid the danger of null references in code; with other languages (including Java), accessing a member of a null reference will often lead to a null reference exception – one of the most common causes of application crashes in Android.

Part of the problem is that you can’t clearly tell whether a variable is nullable or not in Java. To avoid this sort of crash, developers need to implement many checks, which is inconvenient and will bloat the code.

Instead, Kotlin provides null safety (with nullable and non-nullable types) so a compiler can flag potential null pointer dereferences without an option type. There is also no overhead (checks that bloat the code). In the following example, the particular variable of String can’t take a nullable reference and the workspace or IDE alerts the developer.

KOTLIN:

var a: String = "abc"
a = null // compilation error

To allow a null, you add a ‘?’ to the String and the compiler will accept the line, see below:

var b: String? = "abc"
b = null // ok

Here, you can see how the compiler accepts the line, which also prevents the null reference exception type of crash.

Another massive benefit of Kotlin is that it is interoperable with Java, and can generate JavaScript and native code. This means developers can use their Java Libraries and Kotlin code can be translated to Java, and vice versa. Kotlin and Java files can coexist, and Kotlin is useful for expanding Java apps.

Because of their interconnections, Kotlin is hugely advantageous not only for Java programmers, but also for people using Scala, Python, and Ruby. This level of interoperability is important because you can use multiple languages in a project and the languages can communicate with each other. The coding languages support and enhance each other.

kotlin
Source: Kotlin Programming

To wrap up, if you’re looking to start writing in Kotlin, I would highly suggest the following:

  1. Take advantage of the question marks provided through the null pointer safety. This will save you from tedious, long code, and will lead to fewer crashes.
  2. Take advantage of Kotlin’s format strings. Kotlin streamlines strings, which removes a lot of unnecessary code so you can have more one-liners and more concise code.
  3. There are so many extra utility functions that make traversing or modifying lists much easier.

In short, Kotlin produces less code, which leads to fewer crashes, and all while providing the same functionality.

If you want to learn more about Kotlin, check out the Kotlin Language website, these tutorials, and the project on GitHub.

Photo via MobileSyrup

Avatar

Vincent Koo

Vincent is an Agile engineer at TribalScale. He graduated from the University of Waterloo’s Computer Science program. He is passionate about technology, and is interested in blockchain and cryptocurrencies.

3 replies on “Ask a Dev: Should I use Kotlin to develop Android apps?”