Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: cmd/gofmt: permit single line case and statement #66433

Open
dsnet opened this issue Mar 21, 2024 · 5 comments
Open

proposal: cmd/gofmt: permit single line case and statement #66433

dsnet opened this issue Mar 21, 2024 · 5 comments
Labels
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Mar 21, 2024

Proposal Details

Sometimes you have relatively simple switch statements that look like:

switch color {
case MintCream:
    return "#F5FFFA"
case Azure:
    return "#F0FFFF"
case AliceBlue:
    return "#F0F8FF"
case GhostWhite:
    return "#F8F8FF"
...
}

In terms of total lines, this can get quite long.

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.

Thus, the above can be written as:

switch color {
case MintCream:  return "#F5FFFA"
case Azure:      return "#F0FFFF"
case AliceBlue:  return "#F0F8FF"
case GhostWhite: return "#F8F8FF"
...
}

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:
color := map[Color]string{
    MintCream:  "#F5FFFA",
    Azure:      "#F0FFFF",
    AliceBlue:  "#F0F8FF",
    GhostWhite: "#F8F8FF",
    ...
}

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.

@dsnet dsnet added the Proposal label Mar 21, 2024
@gopherbot gopherbot added this to the Proposal milestone Mar 21, 2024
@ALTree
Copy link
Member

ALTree commented Mar 21, 2024

Previously: #57667.

@ianlancetaylor

This comment was marked as resolved.

@dsnet

This comment was marked as resolved.

@xiaokentrl
Copy link

switch color {
    case MintCream:  return "#F5FFFA"
    case Azure:      return "#F0FFFF"
    case AliceBlue:  return "#F0F8FF"
    case GhostWhite: return "#F8F8FF"
}

vary good

@richjyoung
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

6 participants