Kotlin / kotlin-style-guide

Work-in-progress notes for the Kotlin style guide
288 stars 14 forks source link

vertical formatting #23

Open voddan opened 8 years ago

voddan commented 8 years ago

I was wondering what are the views on vertical formatting, e.i. putting similar code one under another:

val width  = 111 +   n + listOf(239, 100500, 10).max()
val height = 22  + num - listOf(239,         10).max()

The example above demonstrates vertical formatting for: a. The assignments (=) b. The operators (+) c. The pair operators (+ and -) d. Brackets () or [] e. The function parameters on invocations

I think it is popular in Haskel and Scala. This practice does make code more readable, and more importantly, prevents typos, e.i in situations like that one:

val neighbors = me[i - 1, j + 1] + me[i, j + 1] + me[i + 1, j + 1] +
                me[i - 1, j    ] +                me[i + 1, j    ] +
                me[i - 1, j - 1] + me[i, j - 1] + me[i + 1, j - 1]

This code snippet utilises vertical formatting for (b) the + operators, (d) brackets [] and (e) function parameters j.

The down side is that currently InteliJ Idea has no support for it in the Kotlin plugin. That means that the IJ auto-formatter ruins any vertical-aligned code. Also this is very time consuming when done by hands.

I separated the use cases into 5 categories (a, b, c d, e) because their may be considered useful or unuseful independently of each other. The categories are sorted from the most useful IMHO.

yole commented 8 years ago

I think that at least (1) can be implemented as an option in the formatter, but I don't think the default style guide should give any guidance for or against it. I think that trying to align anything other than the assignment statements automatically would be far more surprising than useful, but at least we can ensure that the formatter doesn't ruin the alignment done manually.

voddan commented 8 years ago

Another case is function headers. Since the syntax is so complex, vertical alignment really improves readability. What case of alignment below would you prefer provided that it is done with the formatter?

1) No alignment:

data class UserRto(val lastName: String = "",
                   val firstName: String = "",
                   val age: Int = 0,
                   val email: String = "",
                   val password: String = "")

2) Alignment on types::

data class UserRto(val lastName:  String = "",
                   val firstName: String = "",
                   val age:       Int = 0,
                   val email:     String = "",
                   val password:  String = "")

3) Alignment on =:

data class UserRto(val lastName: String  = "",
                   val firstName: String = "",
                   val age: Int          = 0,
                   val email: String     = "",
                   val password: String  = "")
dstd commented 8 years ago

@voddan maybe there should be different types in (2)? All these String-s looks too perfect

stepango commented 8 years ago

4) Alignment on ::

data class UserRto(val lastName : String = "",
                   val firstName: String = "",
                   val age      : Long = 0,
                   val password : String = "")
yole commented 8 years ago

The problem with any kind of vertical alignment is that, while it's easy to implement in a batch code formatting tool, it's much more difficult to maintain during regular code editing, both when you initially type a declaration and when you change something. I want it to be easy to write style-guide-compliant code without the use of batch formatting tools or super-smart IDEs. Because of that, vertical alignment will not be a requirement in the style guide.

elizarov commented 7 years ago

Citing the above rationale, I would propose to put a recommendation not to use vertical alignment into the style guide. One should try to design and layout code so that vertical alignment is not needed to make code readable.

lujiwen commented 6 years ago

@stepango ,ktlint would throw waning like

Missing newline before ")"

on the first line of class declaration data class UserRto(val lastName : String = "", I know the style of your code is acceptable, but how can I mute this warning ? Would you please knidly help me with this, thanks in advance!