kotlin-style-guide
kotlin-style-guide copied to clipboard
Work-in-progress notes for the Kotlin style guide
When wrapping chained calls, put the . character or the `?.` operator on the next line, with a single indent: ``` val anchor = owner.firstChild!! .siblings(forward = true) .dropWhile {...
For loops vs forEach - two very similar constructs with very similar syntaxes: `for (foo in foos) { foo.thing() }` vs `foos.forEach { it.thing() }` I prefer the traditional for...
Is there a style guide or convention for how to declare an infix function? I wonder what is the officially recommended way, or what is the most used way. ###...
Tabs allow better accessibility: * if someone needs a larger indent (e.g. to recognize the code better), they can adjust tab width * `tab` consumes 1 slot on a [refreshable...
## apply Use `apply` for initialization: ```kotlin val foo = createBar().apply { property = value init() } ``` ## also Use `also` over `apply` if the receiver is used for...
When you have a variable `val value: Boolean?` you have a couple of possible ways to test it for `true`: 1. `if (value != null && value)` (doesn't work with...
Prefer using the expression form of `try`, `if` and `when`. Example: ``` return if (x) foo() else bar() ``` The above is preferable to: ``` if (x) return foo() else...
What is the feeling on wildcard imports? I know the general consensus among Java programmers is that they are undesirable, but I'm wondering if the pros may outweigh the cons...
When possible, put lambdas on a single line, using `it` for single-parameter lambdas: ``` elements.dropWhile { it !is KDoc } ``` Don't use `it` in nested lambdas; always declare parameter...
Generally, the contents of a class is sorted in the following order: - Property declarations - Initializer blocks - Method declarations - Companion object Do not sort the method declarations...