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

Numbered parameters #885

Open
Hugo-Hache opened this issue Sep 20, 2021 · 2 comments
Open

Numbered parameters #885

Hugo-Hache opened this issue Sep 20, 2021 · 2 comments

Comments

@Hugo-Hache
Copy link

Following my PR introducing a cop for numbered parameters, @koic suggested that choosing the default style required a discussion in the style guide.

As a basis, here is what we agreed upon in our team. "Explicit" array name is not per se enforcable with a cop, so I'm not sure how it would fit in the guide.

# bad - non explicit array name
results = electric_appliance.process!
results.each { _1.taste }

# bad - multi line block
waffles.each do 
  topping = @user.toppings.max_by(&:evaluation)
  _1.spread topping
end

# bad - block can be created from method
waffles.each { _1.taste } # => waffles.each(&:taste)
waffles.each { taste(_1) } # => waffles.each(&method(:taste))

# good - explicit array name + single line block + necessity to write block
waffles.each { _1.spread(jam) }
waffles.filter { _1.baking == :well_done }
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 22, 2021

I don't get this part at all:

# bad - non explicit array name
results = electric_appliance.process!
results.each { _1.taste }

Seems like an OK usage to me, if this has some side effects.

I think the code bellow is OK, even if it'd stick to the older shorthands.

# bad - block can be created from method
waffles.each { _1.taste } # => waffles.each(&:taste)
waffles.each { taste(_1) } # => waffles.each(&method(:taste))

@Hugo-Hache
Copy link
Author

# bad - non explicit array name
results = electric_appliance.process!
results.each { _1.taste }

I view the usage of numbered parameter bad in this case because by removing the variable name, you loose information on what you manipulate (as the array name is not specific enough). Here for instance the code below would have been easier to understand:

results = electric_appliance.process!
results.each { |waffle| waffle.taste }

I understand this part is quite subjective so it would not surprise me it would not fit in the style guide.

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