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
Add new Style/HashLikeCase
cop
#8280
Conversation
when :template then 'template tokens (like `%{foo}`)' | ||
when :unannotated then 'unannotated tokens (like `%s`)' | ||
end | ||
{ |
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.
Any particular reason you didn't move this to a constant?
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.
Those are just pieces of the message, not particularly useful for the whole class, so I decided it is cleaner to left it in a method instead of extracting to the top-level constant.
Can extract into a constant, if asked.
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.
Got it.
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'll repeat that I feel it's bad style to keep on recreating the same constant hash over and over. Wast of CPU cycles and when reading you have to check the whole hash wondering "Hum, is there anything dynamic in here"?
The code looks good overall. You might want to add some tests with an |
fad72e6
to
28a1ea6
Compare
Added a test for |
Thanks! |
Description: >- | ||
Checks for places where `case-when` represents a simple 1:1 | ||
mapping and can be replaced with a hash lookup. | ||
Enabled: 'pending' |
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.
Will this cop be enabled by default at RuboCop 1.0? I'm not sure about creating a constant of hash instance is a better style than case
syntax if there is no performance advantage. I think it may be considered disabled by default.
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 think it most cases using the hash results in code that's simpler, as you eliminate branching completely. Obviously a simple case
is not a big problem, but I feel that in most cases using a hash results in cleaner code.
Technically a |
Closes #8247