Skip to content
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

Cop idea: enforces usage of create_pair #50

Open
morissetcl opened this issue May 25, 2023 · 1 comment
Open

Cop idea: enforces usage of create_pair #50

morissetcl opened this issue May 25, 2023 · 1 comment

Comments

@morissetcl
Copy link
Contributor

morissetcl commented May 25, 2023

Hello team,

I have a simple cop proposal, the idea is to enforce the usage of create_pair instead of create_list of 2.

     # @example
     #   # bad 
     #   create_list(:foo, 2)
     #
     #   # good 
     #   create_pair(:foo)
     #

Let me know if it make sense to you.
Thanks.

@pirj
Copy link
Member

pirj commented May 25, 2023

Hi!

Thanks for the suggestion.
Personally, I'm quite conservative, and can't remember using create_pair deliberately in practice.

"Pair" sounds like those two are somehow related, apart from being in the same array, like a two-element tuple.
Are there practical uses for that where it would make sense, like create_pair(:boot), or create_pair(:ice_skater).

If e.g. there's a spec with three examples, one with a single-element array, another with a pair, and yet another with a three-element array, I would dismiss the idea to use another method for the second one. Not to say, we recently started tolerating single-element arrays (let(:luggage) { [create(:bag)] }).

There are certain methods, like Rails Array#second, which might sometimes increase readability, but I wouldn't agree to replace all array[1] calls with array.second. I'm uncertain if it's possible to come up with a rule to use one vs another.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants