# An opinionated guide on how to make your Kotlin code fun to read and joy to work with

DevFeed: [An opinionated guide on how to make your Kotlin code fun to read and joy to work with](<https://devfeed.tech/articles/an-opinionated-guide-on-how-to-make-your-kotlin-code-fun-to-read-and-joy-to-work-with-25931.md>)

Original publisher: [Read original article](<https://proandroiddev.com/an-opinionated-guide-on-how-to-make-your-kotlin-code-fun-to-read-and-joy-to-work-with-caa3a4036f9e?source=rss-7a8d96da8cb6------2>)

Author: Gabor Varadi

Published: 2021-02-15T06:16:05Z

Content type: tutorial

Language: en

Sources: [Gabor Varadi](<https://devfeed.tech/sources/stories-by-gabor-varadi-on-medium.md>)

Topics: [Code quality](<https://devfeed.tech/topics/code-quality.md>), [scope function](<https://devfeed.tech/topics/scope-function.md>), [Safe-Call Operator](<https://devfeed.tech/topics/safe-call-operator.md>), [conditionals](<https://devfeed.tech/topics/conditionals.md>), [if statement](<https://devfeed.tech/topics/if-statement.md>), [code beautification](<https://devfeed.tech/topics/code-beautification.md>), [pretty printing](<https://devfeed.tech/topics/pretty-printing.md>), [smart cast](<https://devfeed.tech/topics/smart-cast.md>), [constructor](<https://devfeed.tech/topics/constructor.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [code-style](<https://devfeed.tech/tags/code-style.md>), [guide](<https://devfeed.tech/tags/guide.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [style](<https://devfeed.tech/tags/style.md>)

## AI overview

The guide favors Kotlin code whose intent and types stay easy to read as code evolves. Put constructor properties on separate lines when there are multiple arguments, use named lambda parameters in multi-line or chained expressions, and name multiple callback lambdas. Reserve `let` for simple assignments or returns; avoid using it as general control flow or replacing `if`/`else` with `x?.let {} ?: run {}`. Prefer early returns with the Elvis operator when there is no alternate branch, explicit return types on public non-override functions, `val` where mutation is unnecessary, and non-null parameters when null is not meaningful. Use named data classes rather than positional decomposition or tuple return types when fields carry distinct meanings. The author also recommends read-only `List` types instead of exposing mutable list types, built-in collection operations instead of equivalent loops, and choosing function types, `fun interface`, or regular interfaces according to whether callbacks need a name or multiple methods. These are presented as style recommendations, with some preferences explicitly personal or situational.

## Source excerpt

I've been meaning to write this article in a while. Hopefully, the following tips and style recommendations (in no particular order) will help you write better Kotlin! Let's get on with it, shall we? For 2 or more constructor arguments, prefer not to keep the properties on the same line as the class name in the constructor definitionclass MyClass( val a: A, val b: B, ): MyParentClass(a, b), MyInterface { // ... } This format is preferable, because when adding the 3rd argument, important bits (such as a possible base class or interface) can appear too far on the right side of the screen. Listing them one after the other (including the trailing comma since 1.4.21+) allows for better readability and extensibility (reducing the chance of causing merge conflicts with future code changes, for example). Prefer to use the scoping function 'let' only in assignments or return statements, but NOT as general control flow, or a "quick rename to 'it'" Oftentimes, Kotlin code tends to do something like this: fun myMethod(nullableA: A?) { nullableA?.let { // no it.x() // no it.y() // no it.z() // no } } (Note: in a method like this, A? could be replaced with A to take advantage of typed nullability, this is just an example.) To allow better maintainability and readability, we should instead prefer to only use ?.let { in simple assignments, or return statements, but not as general control flow. For example, the following chain is easy to understand: val protoClass = savedInstanceState?.getByteArray("protoValue") ?.let { ProtoClass.parseFrom(it) } However, in this case, ?.let { is part of an assignment. Also, while ?.let { can be chained with a ?: to provide a default value (especially in combination with takeIf) it shouldn't be used to execute side-effects (also is more suitable for that). In fact, it needs to be said: x?.let {} ?: run {} is NOT a general purpose replacement for if-else statements and null-checks, and should never be used in this particular format for that purpose!