You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I propose that we modify gofmt to permit keeping the case expression and the following statement on the same line if the statement is also a single line.
and gofmt won't try to rewrite into the expanded form. It will however, column align all vertically adjacent statements.
To be clear, I'm not proposing that gofmt rewrite existing expanded switch statements into the compact form,
only that it avoid expanding it if the author deliberately wrote it in the compact form.
The benefit of this is increased readability and consistency:
Readability: All the color enums vertically appear together as a column without any interspersed color values. Similarly, all the color values appear together in a single column. Also, this representation takes half the lines, and so makes more efficient use of more of the editor pane's width.
Consistency: There is syntactic similarity with something like a Go map literal:
As with any formatting change, this can lead to less readable code, but the at least this change allows the programmer to best decide what is most readable and not have gofmt expand the whole switch statement. It also avoids the temptation to use a map literal just because it is more compact.
The text was updated successfully, but these errors were encountered:
It looks like this has come up a few times and there's a couple of points I would like to raise that don't seem to have been considered previously:
Single line case statements can be trivially sorted (alphanumerically) in a lot of text editors. This is useful for maintaining large lookup tables.
The linked previous issue suggested a switch is unnecessary for building lookup tables - #57667 (comment). If the lookup table holds references to functions, and those functions make recursive use of the lookup table, this causes a initialization cycle for lookup compiler error.
The cycle could be broken by reconstructing the lookup table on each recursion, but I would imagine that would incur a performance penalty.
Proposal Details
Sometimes you have relatively simple switch statements that look like:
In terms of total lines, this can get quite long.
I propose that we modify
gofmt
to permit keeping thecase
expression and the following statement on the same line if the statement is also a single line.Thus, the above can be written as:
and
gofmt
won't try to rewrite into the expanded form. It will however, column align all vertically adjacent statements.To be clear, I'm not proposing that
gofmt
rewrite existing expanded switch statements into the compact form,only that it avoid expanding it if the author deliberately wrote it in the compact form.
The benefit of this is increased readability and consistency:
As with any formatting change, this can lead to less readable code, but the at least this change allows the programmer to best decide what is most readable and not have
gofmt
expand the whole switch statement. It also avoids the temptation to use a map literal just because it is more compact.The text was updated successfully, but these errors were encountered: