Crystal: Formatter: Allow empty line in case expression (between `when` blocks)

Created on 1 May 2017  路  9Comments  路  Source: crystal-lang/crystal

Given:

case something
when Int32
  do_something_with_int something

when Bool
  do_something_with_bool something
  do_something_else_for_bool

when String
  do_something_with_str something
end

Currently, a case expression is formatted without spaces:

case something
when Int32
  do_something_with_int something
when Bool
  do_something_with_bool something
  do_something_else_for_bool
when String
  do_something_with_str something
end

I think the original form is more readable. The no-spaces version should stay the default, but I think we should allow to have (one) empty line between when blocks.

WDYT?

Most helpful comment

@bew For sure. Gofmt would probably enforce 0 spaces, I only propose enforcing 0 or 1 (the choice being the programmers, maybe I wasn't clear on that).

All 9 comments

This is something I'd also like to see. For me it does not seem helpful for code where each when section is only a line or two, but I like to do it in code where each section is longer, and the length varies between sections. So in code where you have a single case statement where the whens vary between 10 and 25 lines long.

I don't feel too strongly about this, but I thought I'd mention the situations where I do like to use blank lines. I don't use them in all case statements, just some of them.

It was my understanding that the formatter was not supposed to modify vertical space, just indentation, so while I don't personally think the first option is better, probably the formatter should leave it as is.

The formatter should modify vertical space, and does in many places. The options for vertical space here should be either 1 line or 0. And the formatter should enforce that. I'm always in favour of a strong formatter like gofmt.

Strong formatter is not a bad idea, & help to have uniform code across multiple codebase, but we shouldn't sacrifize readability over uniformity... And I strongly support readability.

@bew For sure. Gofmt would probably enforce 0 spaces, I only propose enforcing 0 or 1 (the choice being the programmers, maybe I wasn't clear on that).

I think this should be fixed (an empty line should be allowed in this case)

So is the suggestion to hard force 1 line or 0 line for everyone, or is it that if the developer has 2 or more blank lines, the formatter would shrink that to 1 line, but if the developer had 0 lines, it would leave 0 lines?

The formatter lets you leave empty lines. Here the formatter will leave an empty line if you had one, and leave none if you didn't have any.

Actually, right now the formatter removes empty lines in some cases. For example this:

class Foo

  def foo
  end

end

is formatted to:

class Foo
  def foo
  end
end

Same goes with if, unless, and pretty much every construct in the language. And I wouldn't like to allow empty lines there. The same applies for case. The formatter is opinionated, so closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rdp picture rdp  路  112Comments

straight-shoota picture straight-shoota  路  91Comments

asterite picture asterite  路  60Comments

HCLarsen picture HCLarsen  路  162Comments

asterite picture asterite  路  78Comments