kotlin-style-guide icon indicating copy to clipboard operation
kotlin-style-guide copied to clipboard

Number of blank lines between declarations

Open matklad opened this issue 9 years ago • 10 comments
trafficstars

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() {}

matklad avatar Jun 14 '16 12:06 matklad

Option 1: do not specify

matklad avatar Jun 14 '16 12:06 matklad

Option 2: always use one blank line

matklad avatar Jun 14 '16 12:06 matklad

Option 3: use one blank line between class members and two blank lines between top level declarations (PEP8 style)

matklad avatar Jun 14 '16 12:06 matklad

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.

voddan avatar Jun 16 '16 04:06 voddan

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)
}

voddan avatar Jun 16 '16 04:06 voddan

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
}

voddan avatar Jun 16 '16 04:06 voddan

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.

yole avatar Jun 16 '16 09:06 yole

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 avatar Jun 16 '16 09:06 matklad

@matklad Then don't, and always use one blank line. This is sufficiently readable.

yole avatar Jun 16 '16 09:06 yole

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.

matklad avatar Jun 16 '16 09:06 matklad