kotlin-style-guide
kotlin-style-guide copied to clipboard
Work-in-progress notes for the Kotlin style guide
Annotations are typically placed on separate lines, before the declaration to which they are attached, and with the same indentation: ``` @Target(AnnotationTarget.PROPERTY) annotation class JsonExclude ``` Annotations without arguments can...
If the function has an expression body that doesn't fit in the same line as the declaration, put the `=` sign on the first line. Indent the expression body by...
How should a method be formatted which contains a one-line assignment? Use cases: ``` kotlin // mutating methods: var x = 0 fun mutateX(d: Int) { x += d }...
In a case of "assignment" syntax with a boolean expression `==` put brackets around the expression: Good: ``` kotlin // assignment: val x: Boolean = (this == other) // expression...
Declare a function as `infix` only when it works on two objects which play a similar role. Good examples: `and`, `to`, `zip`. Bad example: `add`.
What should be the number of blank lines between declarations inside a single class/object or within a single file? ``` Kotlin class C { fun foo() {} fun bar() {}...
Avoid using multiple labeled returns in a lambda. Consider restructuring the lambda so that it will have a single exit point. If that's not possible or not clear enough, convert...
Use extension functions liberally. Every time you have a function that works primarily on an object, consider making it an extension function accepting that object as a receiver. To minimize...