-
Notifications
You must be signed in to change notification settings - Fork 213
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
Prefer each.with_index and each.with_object #39
Conversation
933e9b1
to
b12d30e
Compare
My first version had a side effect of enabling preferences for other collection methods ( |
Why did you make these changes in ruby-2.2.yml instead of base.yml? |
Those didn’t come until 1.9 (I think).
I’ll verify later if you haven’t by then. ;)
… On Dec 8, 2018, at 9:56 AM, Justin Searls ***@***.***> wrote:
Why did you make these changes in ruby-2.2.yml instead of base.yml?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
As to the substance of this issue (and the closely related but apparently-opposite-in-recommendation #41) I am a bit torn. I love me some consistency, but telling people which methods to call seems like it crosses a line. To be honest I think it's a little bizarre for a linter to push people to invoke different methods or avoid an alias. The reason behind this feeling, I think, is because it seems pretty presumptive to say "no, user, you may have meant to say Additionally (IIRC), RuboCop does not always perfectly make these corrections, so (using the same example) if Ruby and (especially) Rails has an utterly massive surface area of methods and aliases to those methods and whether that's a strength or weakness depends largely on one's context. (Been a while since I linked to this, heh.) But regardless of the merits, Rubyland is littered with humane interfaces and it feels like it would be antithetical to what Ruby is to try to consistent-ify that by effectively banning alias use. Even if we agreed that it was a good idea, we'd never find them all. Going to close this issue, but please feel welcome to change-my-mind.gif |
Ah, so the way the config is loaded is that first base is loaded, then the earlier targets are applied only if they're running on an older Ruby. So if this rule change is applied to |
Wups, I misunderstood something. Reopening because I misread the meaning of |
I want to be clear that I’m only suggesting I have opinions on the other methods, but not strong enough to add here. |
Yes I understand that now. Do you know what the |
Ruby, at its very heart, embraces the "there's more than one way" philosophy (which is why I personally think it's a terrible first programming language to learn). A few examples that have come up during code reviews in recent memory:
Obviously, these aren't all semantically identical expressions, nor are they necessarily the same category of coding style as the suggested change, but my tendency is to reach for different methods when they "feel right" (yeah 🤢 I know). So on a philosophical level, I hesitate to believe that Standard can or should address the category of coding style that includes prescribing specific semantically identical methods. Unfortunately, I'm not sure how to articulate why I think method prescriptions should be excluded while To actually address the original issue, if I understand correctly, the implicit argument (ignoring the specific syntax) is to prefer Enumerator iteration over Enumerable iteration so that the developer doesn't need to have memorized the entire Enumerable API. I disagree, mostly just because I prefer the aesthetics of
All those reasons are basically just me justifying my feelings though, so at the end of the day I just think Standard shouldn't prescribe "one right way" when it comes to which methods to call. |
@Iakobos, as always I love your thoughtfulness and self-reflection! I think these are good arguments for being pretty conservative about changing any method calls, even the I'd like to give @danielmorrison a chance to mull over this and respond |
These methods encourage learning about other enumerable possibilities (map.with_index, etc.). Set no preference (~) for other methods (collect, inject, etc.), so this doesn’t have the side effect of enabling preferences for those.
b12d30e
to
444e2a8
Compare
@searls I updated to move this to The I'll follow up later on the philosophical issues. |
Deciding to close this PR. Very much appreciate the sentiment and @danielmorrison's perspective. Feel like in this case because it can't be applied to 100% of cases without potentially confusing folks means it's not appropriate to prescribe. Kudos for more visibility for this feature though. I know I underuse it. |
I know we have had some back on forth on these (e.g. #169 and #237). These preferences were fine when we could automate fixing them with RuboCop. But as we move to standard I think we should buy fully into [standard's opinion on these aliases][standard opinion]. If we don't agree with that opinion, we should discuss the issue on standard instead of in this guide. [standard opinion]: standardrb/standard#39 (comment)
I have long held belief that we should encourage
each.with_index
overeach_with_index
. It comes from years of teaching Ruby. Someone would inevitably ask if there was amap_with_index
(there isn't). I realized it was better to introduceeach.with_index
instead, so that they naturally would think to trymap.with_index
and find out that it Just Works™.Rubocop nicely converts
inject
toeach_with_object
where it can, and this goes a step farther by converting it toeach.with_object
.Before:
After this patch, running
--fix
produces:This is a possibly unpopular hill that I'm willing to shed blood defending.