-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: go/printer: don't reformat single line switch cases #57667
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
Comments
I think, just like for if statement, the case statement with the single lines is actually easier to read as long as the return is simple. |
Assuming that Type is an integer type and its values come from a simple iota, you don't need a switch: var ttStr = [...]string{
LPAREN: "(",
LBRACK: "[",
LBRACE: "{",
RPAREN: ")",
RBRACK: "]",
RBRACE: "}",
ASSIGN: "=",
ADD: "+",
SUB: "-",
MUL: "*",
QUO: "/",
REM: "%",
EQL: "==",
GTR: ">",
LSS: "<",
NEQ: "!=",
GEQ: ">=",
LEQ: "<=",
DOT: ".",
COMMA: ",",
COLON: ":",
IDENT: "IDENT",
STR: "STRING",
NUM: "NUMBER",
}
func (tt Type) String() string {
if tt < 0 || int(tt) >= len(ttStr) {
return "UNKNOWN"
}
return ttStr[tt]
} |
I'm opposed to this. The advantage of gofmt is that it provides a single formatting guideline and thus eliminates a lot of style discussions. It's nobody favorite format, but it's OK. I think that #57645 is at least worth discussing because there is so much discussion about error handling in Go. I can't recall any discussion about the formatting of switch statements. That is, I think that #57645 is an exception, and we shouldn't use that as a basis for providing more formatting options. |
I understand but just to clarify, I found the #57645 just when I came here to create the proposal. I don't even agree with the linked issue, just pointed out it's a similar proposal. @ianlancetaylor feel free to close this. |
I do agree that the one-line form shown in the opening comment is more readable to me than what However, I do find myself wondering where the "breaking point" is that would make it no longer more readable to format it that way. I assume that there's some level of complexity of I don't feel strongly about this proposal and would not be upset if it were rejected but if it is accepted then I think we'd need to decide a rule for what expressions are considered simple enough to be more readable in the tabular shape. My sense of it would be that it's only allowed if the
With that said, despite the slight increase in readability in this proposal, I do prefer the simplicity of the current rule that just treats all |
There is some prior art for this in how gofmt allows single-line functions if they're short enough:
And even:
If it gets too long (~120 columns IIRC) it will break to the next line. This is useful because sometimes you have a bunch of very simple functions, and writing it on one line is fine. I've written code similar to the OP a few times, and found myself wishing gofmt would allow single-line |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
The snippet below:
looks like easier to read and maintain than the one below (from
gofmt
):This proposal is similar to #57645 but applied to
switch
with single-line cases.I don't know if it's related to my dyslexia but I had a piece of code printed (on paper) with big switch cases like this and it's difficult for the eyes to match each
case
with itsreturn
line.I know it's a very subjective topic... :(
The text was updated successfully, but these errors were encountered: