kotlin-guide

Kotlin best practises

Foreword

The purpose of this document is twofold: it has been created to act both as a guide for new developers, especially ones without previous Kotlin experience, on the most important Kotlin specific practises and tools from the perspective of the team and to act as a living document of the various architecture and coding style decisions that the team has made during service development.

Each service will still have their own architecture decision log, but here we will list any non service specific decisions, style guides and general tips that anyone working on our code base should be aware of.

While this document mainly exists for the Wallet team’s internal usage and therefore only covers things relevant to our particular needs it should also provide a starting point for any other team that wishes to develop with Kotlin, particularly on the backend.

What is Kotlin and why we use it

https://en.wikipedia.org/wiki/Kotlin_(programming_language)

https://kotlinlang.org/

In short Kotlin is "yet another functional JVM language". The main benefits compared to developing with Java are similar to other functional languages.

There are however some things Kotlin brings to the table that are not shared by all similar languages.

Learning Kotlin

This document doesn’t go through all the basics. https://kotlinlang.org/docs/reference/ has all the up to date resources to get you started. The team library also includes Kotlin in Action which is a slightly heavier read but the first few chapters do a really good job of introducing the major differences between Kotlin and Java.

Best practises

Principles

Keep it Readable

"Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. …[Therefore,] making it easy to read makes it easier to write." - Martin C. Fowler, Clean Code

Keep it Safe

Coding conventions

Safe variable declarations

Use when

https://kotlinlang.org/docs/reference/control-flow.html#when-expression

In Java you mainly used the switch statement together with enums. In Kotlin switch has been replaced by when which can and should be used for a far wider array of flow management.

Classes and functions

Declaring multiple classes in one file

Constructors

Parameter and state validation

Properties and data classes

https://kotlinlang.org/docs/reference/properties.html

https://kotlinlang.org/docs/reference/data-classes.html

Since many data classes are only ever created by one class in the project it in those cases makes sense to declare data classes in the same file as the creating class

Scope Functions

Companion objects

https://kotlinlang.org/docs/reference/object-declarations.html#companion-objects

Return values

Returning sealed classes

https://kotlinlang.org/docs/reference/sealed-classes.html

https://phauer.com/2019/sealed-classes-exceptions-kotlin/

Chaining collection functions

Nested lambdas

Top level functions

Closeables

Java libraries

Style

Tools and libraries

Logging

Tips

Strings

Operator overloading

Equality