More productivity with Kotlin
By: Florina Muntenescu
Tags: Android, Android App Development, Kotlin, Buildbetterapps

Kotlin is known for its conciseness and this, in practice, translates into higher productivity. Even more, 67% of professional Android developers who use Kotlin say it has increased their productivity. In this blog post, I’ll share a couple of ways Kotlin has increased the productivity of our partners’ engineers and look at the Kotlin features that help with this.

67% of professional Android developers surveyed who use Kotlin say it has increased their productivity

Conciseness, simplicity and productivity

Kotlin’s conciseness has effects on all stages of development:

As an author it means that you can focus on the problem you need to solve, rather than on the syntax. Less code means less to test and less to debug and less chances of creating bugs.

it means that you can focus on the problem you need to solve, rather than on the syntax. Less code means less to test and less to debug and less chances of creating bugs. As a reviewer or maintainer it means that you have less code to read, making it easier to understand what the code does, and therefore easier to review or maintain.

One example of this comes from the team at Flipkart:

“During an internal survey, 50% of developers mentioned that they’d provide smaller estimates [to complete a feature] if the module was written in Kotlin.” (Flipkart)

Kotlin features and productivity

The majority of Kotlin features result in higher productivity, both due to conciseness and higher readability so let’s look at some of the most used ones.

Default arguments and builders

In the Java programming language, when some of the parameters of your constructor are optional, you usually take one of two roads:

Add multiple constructors

Apply the builder pattern

With Kotlin neither of these is necessary due to the use of default arguments. Default arguments allow you to implement function overloads without the extra boilerplate.

When the Cash App team started using Kotlin, they were able to eliminate many builders, and cut down on the amount of code they needed to write. In some cases, they saved 25% in code size.

For example, here’s how the implementation of a Task object, where the name of the task is the only mandatory parameter would look like when using a builder or when using default arguments:

Find out more about default arguments from our Kotlin Vocabulary blog post.

Objects and singletons

The singleton pattern is probably one of the most used patterns in software development — it helps you create a single instance of an object which can be accessed and shared by other objects.

To create a singleton you would need to control how the object is created, allowing only one instance of it and making sure that the code is thread safe. In Kotlin, you only need one keyword: object .

Operators, string templates and more

The conciseness and simplicity of the Kotlin language is shown in features like operator overloading, destructuring or string templates — the resulting code gets so easy to read.

For example, let’s say that we have a library with books. To remove a book from the library, and then work only with the title and print it, the code we write can look like this:

Here are the Kotlin features used:

-= is implemented using operator overloading

is implemented using operator overloading val (title, author) = book uses destructuring

uses destructuring println(“Borrowed $title”) uses string templates

Conclusion

Kotlin makes it easy to read and write code. Patterns like singleton or delegation are part of the language, removing the need to write a lot of code that can lead to bugs being introduced and a higher maintenance burden. Features like string templates, lambda expressions, extension functions, operator overloading and more, make the code more concise and simple. Less code to write leads to less code to read, less code to maintain, fewer errors and higher productivity.

Read more about how you can Build Better Apps with Kotlin, and see how developers have benefited from Kotlin by reading our case studies. To make your first steps with Kotlin, one of the most loved languages in the world, check out our Getting started page.