简体   繁体   中英

Kotlin coding conventions: Horizontal whitespace

In coding convention of Kotlin, in white spaces sections it is noticed that:

As a general rule, avoid horizontal alignment of any kind. Renaming an identifier to a name with a different length should not affect the formatting of either the declaration or any of the usages.

What does this mean?

Renaming an identifier to a name with a different length should not affect the formatting of either the declaration or any of the usages

Here's an example of a violation of that rule:

val b      = SomeFluentBuilder()
val result = mutableListOf<String>()

b.foo()
 .bar()
 .baz()
 .build()

Renaming b to (for example) someFluentBuilder would break the alignment in the declaration, and also in the usage of the builder.

Horizontal alignment is using whitespace to move text horizontally so that things line up vertically.

So in the already provided answer...

val b      = SomeFluentBuilder()
val result = mutableListOf<String>()

is an example of horizontal alignment because additional spaces after 'val b' are used to the '=' align with the equals of the line below. The correct style is:

val b = SomeFluentBuilder()
val result = mutableListOf<String>()

Further....why choose the very uninformative name 'b', rather than perhaps.. 'someFluentBuilder' which follows the name of the class? The suggestion is the name was chosen just to make it easy to align all the dot method calls in the example. The point being do not take steps to have the code horizonally align with the lines above or below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM