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

Metrics/ParameterLists supports MaxOptionalParameters config parameter #9010

Merged
merged 1 commit into from Nov 26, 2020

Conversation

fatkodima
Copy link
Contributor

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 9, 2020

I think the real problem is not a particular number of default arguments, but having any of them in the first place, and mixing them with keyword arguments. I'd just discourage their usage completely, but that's just me. @rubocop-hq/rubocop-core wha t do you think?

@marcandre
Copy link
Contributor

There already a cop for too many positional arguments I believe, but the default maximum is super high (5?).

My personal preference:

  • public methods: max of 1 positional or rest argument, everything else is keyword argument ("Rails convention")
  • private methods: no maximum, but never any default for positional arguments. It's typically to pass some context around, so typically the arguments passed will be clear on the call site.

@fatkodima
Copy link
Contributor Author

My intention with this cop was to avoid ugliness like

def do_something(a = nil, b = nil, c = nil, d = nil)
end

do_something(nil, nil, nil, value)

and more general

def do_something(a = 1, b = 2, c = 3)
end

do_something(1, 2, 4) # you have to repeat default values for "a" and "b" to call with different "c"

but having any of them in the first place, and mixing them with keyword arguments

agreed with mixing

Feel free to close this if is not worth it.

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 25, 2020

Btw, I was just reminded that we already have a cop Style/OptionalArguments (yeah, I know we messed up its name), and it seems natural to me to just add the limit for optional arguments there.

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 25, 2020

As for keyword vs optional params - I'd love for us to have some cop that enforces more or less what @marcandre suggested, but I think that's orthogonal to @fatkodima's current suggestion.

@bbatsov bbatsov self-assigned this Nov 25, 2020
@fatkodima fatkodima force-pushed the too_many_default_arguments-cop branch from b6d6675 to 5db2a23 Compare November 25, 2020 11:16
@fatkodima fatkodima changed the title Add new Style/TooManyDefaultArguments cop Style/OptionalArguments supports Max config parameter Nov 25, 2020
@fatkodima
Copy link
Contributor Author

and it seems natural to me to just add the limit for optional arguments there.

This is what I wanted 👍 So added that config.

@fatkodima fatkodima force-pushed the too_many_default_arguments-cop branch from 5db2a23 to ee297eb Compare November 25, 2020 17:03
MSG = 'Optional arguments should appear at the end ' \
include ConfigurableMax

MSG = 'Optional parameters should appear at the end ' \
'of the argument list.'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argument -> parameter

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 25, 2020

Btw, I was thinking that we can also have this check in the other cops Metrics/ParameterLists. I'm on the fence which method is a better home for it.

@fatkodima
Copy link
Contributor Author

Forgot about its existence. Metrics/ParameterLists seems a better place to me, since the original cop deals with default parameters placement (at the end or not), but the mentioned one with counts of parameters.

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 25, 2020

Okay, let's go for it then. Sorry that I'm making you do so much extra work, I just have trouble remembering everything we've done so far. 😆

@fatkodima fatkodima force-pushed the too_many_default_arguments-cop branch from ee297eb to cfe1c78 Compare November 25, 2020 18:35
@fatkodima fatkodima force-pushed the too_many_default_arguments-cop branch from cfe1c78 to 639494c Compare November 25, 2020 18:41
@fatkodima fatkodima changed the title Style/OptionalArguments supports Max config parameter Metrics/ParameterLists supports MaxOptionalParameters config parameter Nov 25, 2020
@fatkodima
Copy link
Contributor Author

No worries. I have a lot of free time 😄

@bbatsov bbatsov merged commit 356baf8 into rubocop:master Nov 26, 2020
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

Successfully merging this pull request may close these issues.

None yet

3 participants