kotlin-style-guide
kotlin-style-guide copied to clipboard
Number of blank lines between declarations
What should be the number of blank lines between declarations inside a single class/object or within a single file?
class C {
fun foo() {}
fun bar() {}
}
fun baz() {}
Option 1: do not specify
Option 2: always use one blank line
Option 3: use one blank line between class members and two blank lines between top level declarations (PEP8 style)
I separate groups of class members with 2 lines by semantics. For example, one line between methods, but 2 lines after the last public one and before the first private one.
In case of using a lot of one-liners with the expression syntax, it makes sense to put them on next lines:
class Ring {
fun state() = state
fun isZero() = (state == 0)
fun hasNext() = (state != size - 1)
}
Also, there is no point in putting a lot of blank lines between function signatures, e.i. in interfaces:
interface Circular<T> {
fun state(): T
fun inc()
fun isZero(): Boolean
fun hasNext(): Boolean
}
I personally find that, of all formatting rules in PEP 8, the "two blank lines" one has one of the smallest ratios of readability benefit compared to enforcement effort. You're welcome to add as many blank lines as you need to organize the code in a logical way, but I really don't want to include any requirements of this kind in the Kotlin style guide.
I personally find that, of all formatting rules in PEP 8, the "two blank lines" one has one of the smallest ratios of readability benefit compared to enforcement effort.
For me, the benefit is not readability, but the fact that I don't need to decide and maintain the number of blank lines on a case by case basis manually.
@matklad Then don't, and always use one blank line. This is sufficiently readable.
This is what I do at the moment, but I do this manually because formatter allows either two or one line anywhere. So when I move code around I must pay a bit of extra attention and not introduce blank lines by accident. Anyway, I don't have very strong feelings about this, and "do not specify" is ok with me.