Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

vertical formatting #23

Open
voddan opened this issue Jun 14, 2016 · 7 comments
Open

vertical formatting #23

voddan opened this issue Jun 14, 2016 · 7 comments

Comments

@voddan
Copy link

voddan commented Jun 14, 2016

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
Copy link
Contributor

yole commented Jun 14, 2016

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
Copy link
Author

voddan commented Jun 16, 2016

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 = "")
  1. Alignment on types::
data class UserRto(val lastName:  String = "",
                   val firstName: String = "",
                   val age:       Int = 0,
                   val email:     String = "",
                   val password:  String = "")
  1. Alignment on =:
data class UserRto(val lastName: String  = "",
                   val firstName: String = "",
                   val age: Int          = 0,
                   val email: String     = "",
                   val password: String  = "")

@dstd
Copy link

dstd commented Aug 30, 2016

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

@stepango
Copy link

  1. Alignment on ::
data class UserRto(val lastName : String = "",
                   val firstName: String = "",
                   val age      : Long = 0,
                   val password : String = "")

@yole
Copy link
Contributor

yole commented Aug 31, 2016

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
Copy link

elizarov commented Nov 8, 2016

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.

@ghost
Copy link

ghost commented Aug 1, 2018

@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!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants