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 Metrics/CollectionLiteralLength
cop
#11584
Conversation
82bd9d5
to
ab1c871
Compare
config/default.yml
Outdated
Description: 'Checks for extremely large literals, such as `Array`s and `Hash`es with many entries.' | ||
Enabled: pending | ||
VersionAdded: '<<next>>' | ||
LengthThreshold: 250 |
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 picked this arbitrarily. 🤷
We shouldn't pick a number so high that it makes the rule useless, but also shouldn't be too aggressive in our default. Application owners are free to adjust the number as they see fit.
I can understand that |
This is actually related to #11588 which I just opened. An application of ours had an array of 2000+ sub-arrays of reconciliation data to be inserted in a backfill rake task. A I created this cop because I have seen many instances of this over the years, where otherwise simple files are crowded by large data/config sections that would be better extracted into separate files (e.g. CSV), or better yet, stored externally from the git repo all together, and uploaded to the app for one time use (via an approach like a Maintenance Tasks' CSV task). Another occurrence I saw recently was a project where they'd tried to load a CSV, but it was taking too long (simple mistake, see rubocop/rubocop-performance#328 which I opened to catch it), so they decided to hard code a It is my opinion that in most projects very large literals would be a smell (perhaps 500, or 1,000 would be better thresholds), and that projects that use a DSL where this makes sense would simply disable the cop. |
This cop checks for extremely long `Array` and `Hash` literals, and `Set` pseudo-literals (actually parameters to the `Set.[]` method).
55c450e
to
a9a16ce
Compare
I like the overall idea, although I'm not sure whether this is something we can have enabled by default. A couple of remarks:
|
Fair. We'll probably enable it in
Yeah, good point. I'd originally chosen a generic name because I wanted to also include strings, but as I started to look into it it wasn't as simple. Perhaps
Yeah, I just picked a number that felt
So not fully arbitrary, but not based on any metrics or anything like that. |
I'd be fine by me. @rubocop/rubocop-core Proposals for a better name would be welcome. Btw, it's also debatable if this is a Style cop or a Metrics cop, given it's similar in spirit to method/class length checks. If it's there it can be simple |
Interesting. Metrics may be better. |
@bbatsov, @koic what do you think of I think it's important to have |
Ah yes, I agree that is better. I'll make the change. |
This test appears to disable all departments except `Style` for the purposes of the test. However, it missed `Metrics`, which means `pending` `Metrics` tests result in unexpected output.
Thanks! |
Style/LengthyLiteral
copMetrics/CollectionLiteralLength
cop
This cop checks for extremely long
Array
andHash
literals, andSet
pseudo-literals (actually parameters to theSet.[]
method).Before submitting the PR make sure the following are checked:
Commit message starts with[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.