Open voddan opened 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.
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 = "")
@voddan maybe there should be different types in (2)? All these String-s looks too perfect
4) Alignment on :
:
data class UserRto(val lastName : String = "",
val firstName: String = "",
val age : Long = 0,
val password : String = "")
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.
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.
@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!
I was wondering what are the views on vertical formatting, e.i. putting similar code one under another:
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 invocationsI 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:
This code snippet utilises vertical formatting for (b) the
+
operators, (d) brackets[]
and (e) function parametersj
.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.