简体   繁体   English

Kotlin编码约定:水平空白

[英]Kotlin coding conventions: Horizontal whitespace

In coding convention of Kotlin, in white spaces sections it is noticed that: 在Kotlin的编码约定中,在空白区域中注意到:

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. b重命名为(例如) someFluentBuilder将破坏声明中的对齐方式以及构建器的用法。

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. 是水平对齐的示例,因为'val b'之后的其他空格用于'='与下面的行的等号对齐。 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? 进一步....为什么选择非常无用的名称“ b”,而不是选择跟随类名的“ someFluentBuilder”? The suggestion is the name was chosen just to make it easy to align all the dot method calls in the example. 建议选择名称只是为了使示例中的所有dot方法调用易于对齐。 The point being do not take steps to have the code horizonally align with the lines above or below. 关键是不要采取措施使代码与上面或下面的线水平对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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