def keyword_method(a: 1, b: 2, c: 3, d: 4, e: 5)
end
DEFAULT = { a: 1, b: 2, c: 3, d: 4, e: 5 }
def non_keyword_method(hash_options = {})
hash_options = DEFAULT.merge(hash_options)
end
gives the following output:
== test.rb ==
C: 1: Avoid parameter lists longer than 4 parameters.
1 file inspected, 1 offence detected
I don't think it should see this as an offence. It should only show this as an offence when there are > 4 _mandatory_ parameters
I'll think about that. Since keyword arguments are relatively new feature I haven't thought much about them. Others are welcome to voice their opinions as well. At the very least we can have an option for this cop about whether to count them or not.
@bbatsov Also, with keyword arguments it's completely clear what is the purpose of each argument, so there is no need to warn about it either way.
I lean towards the advice in Clean Code, that too many method parameters is a source of issues. I'm doing some thinking about this as well ...
Sandi Metz, the author of Practical Object-Oriented Design in Ruby, is quoted here saying "Pass no more than four parameters into a method. Hash options are parameters." and here saying "You can pass no more than four parameters and you can鈥檛 just make it one big hash.". Ruby 2.0's keyword arguments are essentially the same as using an options hash, but with added syntax, so I think Sandi Metz' rule applies there too.
Keys identifying each argument does not take away the whole problem of too many arguments. On the other hand there is a difference between keyword arguments and arguments without keys, so a new configuration option where users can set a different max value for keyword arguments would be good.
Ideally we should report methods like non_keyword_method in @jurriaan's example but that might be too difficult.
Most helpful comment
Sandi Metz, the author of Practical Object-Oriented Design in Ruby, is quoted here saying "Pass no more than four parameters into a method. Hash options are parameters." and here saying "You can pass no more than four parameters and you can鈥檛 just make it one big hash.". Ruby 2.0's keyword arguments are essentially the same as using an options hash, but with added syntax, so I think Sandi Metz' rule applies there too.
Keys identifying each argument does not take away the whole problem of too many arguments. On the other hand there is a difference between keyword arguments and arguments without keys, so a new configuration option where users can set a different max value for keyword arguments would be good.
Ideally we should report methods like
non_keyword_methodin @jurriaan's example but that might be too difficult.