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
Is your feature request related to a problem? Please describe.
I just upgraded a very large project from 1.13 to 1.24. Most of the offenses auto-corrected code that was written in 2013-2016 to use newly available features. There were only two rules I disagree with which had over 10 offenses. But they are both Lint rules and I hate to disable those too swiftly. I try to take lint rules very seriously before disabling them.
One of them is Lint/AmbiguousOperatorPrecedence and I agree with much of the feedback in #10080.
The other is Lint/ElseLayout. I see what the rule is getting at, but... the style I've been (manually) enforcing doesn't have that issue, IMO. I found #10147 and I agree with the feedback there and the proposed solution (allow single-line else when the if/elsif before it are single line) seems good to me, once it's working. But IMO, the rationale behind the original lint rule still applies to the examples given in that discussion.
To steal the simple example at the very top of #10147:
IMO, this visual alignment of conditions and results is confusing when it comes to the else clause. And IMO this is just as true for the else clause in case/when/else statements.
Describe the solution you'd like
When I have a case statement or a chain of if/elsif/else statements which are all (or almost all) able to fit within 80 cols, I will usually convert it to the single-line style... but only if I can align all of the then clauses. e.g:
# this is how I would handle the example aboveifnumber == 1thenputs"One"elsifnumber == 2thenputs"Two"else"Unknown"end# and... aha! After alignment, I think I found a "bug" in our toy code.# what our else clause _probably_ meant to do was the following:ifnumber == 1thenputs"One"elsifnumber == 2thenputs"Two"elseputs"Unknown"# alignment highlights the missing putsend
See how helpful alignment is for this specific use case? 🙂 Like I said above, I don't just allow alignment, I require it for all single-line else clauses. I also require it in case/else statements.
Here are a couple of other examples of what I'd consider "good" layout:
In the if/elsif cases, not only do I require else clauses to be aligned, but also the original if condition.
Describe alternatives you've considered
Multiple times I've tried just never writing single-line if/elsif/else statements... I've tried not-aligning... and I can't do it! 😉 I crave the aligned tabular layout! It clarifies the code structure more than is possible with only indentation and keyword syntax highlighting. Consider:
cond foo
one
cond bar
two
cond baz
three
vs:
cond
result
cond foo
one
cond bar
two
cond baz
three
I'm sure this is more "style" than objective reality, and "de gustibus non est disputandum" ... but hopefully you can see how the tabular layout is a style some teams might want to enforce. I will gladly refactor code to name each conditional and each result with their own methods or procs, if it provides a clarifying tabular layout. But single-line conditional + result clauses without alignment... for me that's the worst of both worlds. 🙂
So, if I can fit everything and align it, I prefer single-line style. If I can't align (nearly) every row within my max columns, I usually convert all of them to "regular" style for both consistency and legibility. I do make a special exception for the else clause on its own line, because it's not unusual for the else clause to be an exception or otherwise special compared to the others.
Does that make sense?
The text was updated successfully, but these errors were encountered:
FWIW: if someone gives me a few pointers to how to get started, I'd like to start learning how to make this sort of rule myself. Maybe I'll make a PR. :)
Is your feature request related to a problem? Please describe.
I just upgraded a very large project from 1.13 to 1.24. Most of the offenses auto-corrected code that was written in 2013-2016 to use newly available features. There were only two rules I disagree with which had over 10 offenses. But they are both
Lint
rules and I hate to disable those too swiftly. I try to take lint rules very seriously before disabling them.One of them is
Lint/AmbiguousOperatorPrecedence
and I agree with much of the feedback in #10080.The other is
Lint/ElseLayout
. I see what the rule is getting at, but... the style I've been (manually) enforcing doesn't have that issue, IMO. I found #10147 and I agree with the feedback there and the proposed solution (allow single-line else when theif/elsif
before it are single line) seems good to me, once it's working. But IMO, the rationale behind the original lint rule still applies to the examples given in that discussion.To steal the simple example at the very top of #10147:
IMO, this visual alignment of conditions and results is confusing when it comes to the
else
clause. And IMO this is just as true for the else clause incase/when/else
statements.Describe the solution you'd like
When I have a
case
statement or a chain ofif/elsif/else
statements which are all (or almost all) able to fit within 80 cols, I will usually convert it to the single-line style... but only if I can align all of thethen
clauses. e.g:See how helpful alignment is for this specific use case? 🙂 Like I said above, I don't just allow alignment, I require it for all single-line
else
clauses. I also require it incase/else
statements.Here are a couple of other examples of what I'd consider "good" layout:
In the
if/elsif
cases, not only do I requireelse
clauses to be aligned, but also the originalif
condition.Describe alternatives you've considered
Multiple times I've tried just never writing single-line
if/elsif/else
statements... I've tried not-aligning... and I can't do it! 😉 I crave the aligned tabular layout! It clarifies the code structure more than is possible with only indentation and keyword syntax highlighting. Consider:vs:
I'm sure this is more "style" than objective reality, and "de gustibus non est disputandum" ... but hopefully you can see how the tabular layout is a style some teams might want to enforce. I will gladly refactor code to name each conditional and each result with their own methods or procs, if it provides a clarifying tabular layout. But single-line conditional + result clauses without alignment... for me that's the worst of both worlds. 🙂
So, if I can fit everything and align it, I prefer single-line style. If I can't align (nearly) every row within my max columns, I usually convert all of them to "regular" style for both consistency and legibility. I do make a special exception for the
else
clause on its own line, because it's not unusual for theelse
clause to be an exception or otherwise special compared to the others.Does that make sense?
The text was updated successfully, but these errors were encountered: