-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Go 2: Single line if
variant for returns and other jump statements
#62434
Comments
Previous proposals have been around implementing single line returns via First, how clever should Second, I think having a explicit syntax for this brings some benefits:
|
The problem with laying out code like this is that the most important part of the line, the action (return, break, etc.) is hidden in the middle of a long line of text rather than being the first thing one sees. |
short line will be nice, i think it can used to only "if err!=nil{return err} " Statement :) |
The idea of making error checks a single line has been proposed several times, such as in #38151, and always declined. This proposal is slightly different in that it omits the curly braces, but it's not very different. This would also be the only place where a block is permitted but optional. Also the emoji voting is not in favor. Therefore, this is a likely decline. Leaving open for three weeks for final comments. |
I think following the benefit of S in SOLID principles: just keeping one line of code for handling one thing, in this case just for condition only, makes code clear and readable |
No change in consensus. |
Author background
Would you consider yourself a novice, intermediate, or experienced Go programmer?
Intermediate to experienced
What other languages do you have experience with?
C#, JS/TS, Python, Ruby
Related proposals
Has this idea, or one like it, been proposed before?
I've dug around through the past issues quite a bit, haven't seen this specific kind of proposal yet.
The closest proposals have been to preserve existing syntax, but change
gofmt
to allow single-lineif
statements.Does this affect error handling?
It does not meaningfully alter the error handling pattern, though it is largely motivated by an interest in reducing lines of error handling code
How does this differ from previous error handling proposals?
This approach aims to not introduce any new syntax specific to errors. Instead it focuses on a more concise way to conditionally exit a function (and other jump types). I believe there are no fundamental issues in the error handling paradigms in go, there is only noise due to the required 3 additional lines of code in every site where an error needs to be checked and returned.
Is this about generics?
No
Proposal
What is the proposed change?
The proposal is to introduce a new variant of
if
statement syntax that allows replacing the statement block with one of the control flow keywords that interrupt execution flow, i.e.return
,break
,continue
, orgoto
. Example:This kind of jump condition would be the only form of
if
that is allowed to display on a single line.Who does this proposal help, and why?
This helps reduce noisy boilerplate in code that distracts from the essential behavior of a function. It allows representing common conditional control flow jumps as a single line of code, a considerable improvement over the current requirement of 3 lines for an
if
statement. It aims to do so with a syntax that is immediately clear to readers, and without introducing magical characters, new keywords, or significant changes to existing control flow and error handling paradigms.Please describe as precisely as possible the change to the language.
if
syntax would allow replacing a block statement with a single jump statement (return
,break
,continue
, orgoto
) without brackets. This would have to be written on a single line.if
statement would not allow a trailingelse
statement. Anelse
statement in this context serves no purpose. If the condition is true, the flow of execution will be transferred to another location. If not, execution continues.if
variant. I am a bit biased against this style in general though, I feel it adds too much horizontal complexity, and would be even more so with areturn
statement included on the line. I am flexible on this though, could be open to allowing it and leaving its use to developer judgment.What would change in the language spec?
Please also describe the change informally, as in a class teaching Go.
A common critique of golang is the verbosity of handling errors. Go requires every method that can fail to be handled manually by the caller, and every site of error handling requires an
if
statement that occupies 3 lines of code, sincegofmt
does not allow single lineif
statements. This change provides a new way to writeif
conditions specifically forreturn
statements and other control flow commands on a single line, which can reduce the lines of code dedicated to this kind of boilerplate by up to 66%.Is this change backward compatible?
Yes
Code Example - Before
Code Example - After
Orthogonality: how does this change interact or overlap with existing features?
Fairly isolated addition, only adds to the syntax of
if
conditions. Does change how people will write error handling code, but only insofar as they'll be able to write fewer lines of code using the same patterns.In terms of readability/scannability, I think this code will still aid quick identification of jump conditions. As the only kind of
if
condition that displays on a single line, eyes will quickly adjust to recognizing these as conditional control flow points.Is the goal of this change a performance improvement?
No
Costs
Would this change make Go easier or harder to learn, and why?
Admittedly it would be another variation of valid
if
syntax to learn, though a modest one and entirely optional. If encountered in code by someone previously unfamiliar, it should be quite clear what it does.What is the cost of this proposal? (Every language change has a cost).
The compiler and parsers would have to be updated to recognize it as valid syntax, but I would expect this effort to be relatively modest compared other syntax additions (though I'm no expert on this subject).
How many tools (such as vet, gopls, gofmt, goimports, etc.) would be affected?
Most of the above I would imagine. Syntax highlighting still works as expected for me, would imagine goimports would not need modification (speculating here - again, not an expert in these parsing tools).
What is the compile time cost?
I'm speculating again, but I imagine it would be negligible. Maybe even faster if it can skip parsing the block of statements?
What is the run time cost?
Should be none
Can you describe a possible implementation?
This is a bit outside my wheelhouse, but imagine it would entail having parsers supporting seeking an occurrence of one of the supported control flow key words after an
if
keyword, instead of only seeking an opening{
bracket.Do you have a prototype? (This is not required.)
No
The text was updated successfully, but these errors were encountered: