-
Notifications
You must be signed in to change notification settings - Fork 16
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
Don't prefer Array.new(3) over 3.times.map #84
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree
Without taking a view here the reason for this cop existing is because the Array.new form is more performant (apparently) See: rubocop/rubocop#2583 I personally think that unless you are specifically optimising some code for performance you should always use the more readable form anyway .... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks a lot! 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the readability of 3.times.map { |x| } over Array.new(3).map { |x| }.
Well, it's really Array.new(3) { |x| }
I personally don't see any difference in readability. I would even say that I prefer the second: "Create an new array of size 3 and initialize it using the block"
instead of "map "3.times" with a block"
@Knack now sure if this convinces you but I found the original diff was easier to follow that way. "Give me this thing 3 times" |
@Knack sorry my mistake on the syntax. |
But that's because the original code had a bug
Whereas
Even if all the values are the same you can pass it as a second argument: But as I said, I don't see a big difference and I don't care too much about performance. So it's ok to disable it. |
Agree about the lack of need to optimize for performance here. I think I'd also fall into the habit of writing |
Thanks for the extra explanation @Knack! I also agree with this quote:
In the end I think the PR title is maybe incorrect, but the fix is 👍 😉 |
Yes, it would be more appropriate "Don't prefer Array.new(3) over 3.times.map" 😄 |
Thanks for the suggestion @Knack I have changed it. |
Context https://github.com/cookpad/global-web/pull/7808#issuecomment-387919152
Disable https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Performance/TimesMap
I prefer the readability of
3.times.map { |x| }
overArray.new(3).map { |x| }
.More discussion:
https://stackoverflow.com/questions/41518896/why-does-rubocop-suggest-replacing-times-map-with-array-new