This page contains the current coding style for the Kotlin language.
If in doubt default to the Java Coding Conventions such as:
There is a space before colon where colon separates type and supertype and there's no space where colon separates instance and type:
interface Foo<out T : Any> : Bar { fun foo(a: Int): T }
In lambda expressions, spaces should be used around the curly braces, as well as around the arrow which separates the parameters from the body. Whenever possible, a lambda should be passed outside of parentheses.
list.filter { it > 10 }.map { element -> element * 2 }
In lambdas which are short and not nested, it's recommended to use the it
convention instead of declaring the parameter explicitly. In nested lambdas with parameters, parameters should be always declared explicitly.
If a function returns Unit, the return type should be omitted:
fun foo() { // ": Unit" is omitted here }
In some cases functions with no arguments might be interchangeable with read-only properties. Although the semantics are similar, there are some stylistic conventions on when to prefer one to another.
Prefer a property over a function when the underlying algorithm:
O(1)
complexity
© 2010–2017 JetBrains s.r.o.
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/docs/reference/coding-conventions.html