Kotlin tips: Singleton, Utility Functions, group Object Initialization and more…

Tips on writing good Kotlin code and using what the language has to offer

Mostafa Gazar
Trade Me Blog
4 min readSep 18, 2017

--

There are many benefits of using Kotlin; it is concise, safe and most importantly it is 100% interoperable with Java. It also tries to solve some of the limitations of Java. Check out this great document that Jake Wharton wrote back in 2015 on why Square should start using Kotlin.

Now is a good time to consider Kotlin for your next big feature or project, because Google finally announced it to be a first-class language for writing Android apps in Google I/O 17.

So here are some tips to help you start making use of what the language has to offer and writing good Kotlin code.

Singleton

Implementing lazy loaded and thread-safe singletons in Kotlin is really easy, unlike Java where you would have to rely on the complex double-checked locking pattern. And although Java enum singletons are thread-safe, they are not lazy loaded.

Contrary to a Kotlinclass, an object can’t have any constructor, but init blocks can be used if some initialization code is required.

The object will be instantiated and its init blocks will be executed lazily upon first access, in a thread-safe way.

Utility Functions

Favor Kotlin top-level extension functions over the typical Java utility classes. And for easier consumption within Java code, use @file:JvmName to specify the name of the Java class which would get generated by the Kotlin compiler.

Object Initialization

Use apply to group object initialization statements to allow for cleaner, easier to read code.

Some more small tips just for you!

let()

Using let() can be a concise alternative for if. Check out the following code:

With let(), there is no need for an if.

when()

when replaces the switch operator of Java and it can also be used to clean up some hard to read if conditions.

And in Kotlin it would look like:

Read-only lists, maps, …

Kotlin distinguishes between mutable and immutable collections (lists, sets, maps, etc). Precise control over exactly when collections can be edited is useful for eliminating bugs, and for designing good APIs.

The Kotlin standard library contains utility functions and types for both mutable and immutable collections. For example listOf(), and mutableListOf().

Lazy property

lazy is good, use it when it makes sense! Keeping a low memory footprint is a good use case, and also saving CPU cycles if its initialization is expensive.

Lazy properties only get computed upon first access.

Fight the temptation to squeeze everything in a single expression

It can be quite tempting to try to squeeze everything into a single expression. Just because you can, doesn’t mean that it’s a good idea.

If a single expression function is forced to wrap to a new line it should be a standard function.

Aim for readable and clean code instead.

Curious? Here are some more materials to check out

https://sd.keepcalm-o-matic.co.uk/i/keep-calm-and-be-curious-80.png

A complete reference to the Kotlin language can be found here.

Are you curious yet? Try Kotlin!

Advanced Kotlin tips is now available, check it out at http://bit.ly/advanced-kotlin-tips

If you liked this article make sure to 👏 it, and follow me on Twitter!

--

--