tag:blogger.com,1999:blog-7821856652257554779.post8566622531662060269..comments2025-09-20T09:27:07.510+02:00Comments on Andrzej on Software: args, opts, paramsAndrzej Krzywdahttp://www.blogger.com/profile/06399276063142826365noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-7821856652257554779.post-9612046363331121242011-12-20T07:53:27.459+01:002011-12-20T07:53:27.459+01:00I agree that method signatures should be as simple...I agree that method signatures should be as simple as possible. But then there are those methods where multiple, optional arguments make perfect sense.<br /><br />Since Ruby does not support named parameters (yet), the best way I know to mimic named parameters is to use a hash. A call to assert_valid_keys makes sure no caller passed an unexpected option and is some sort of lightwheight documentation. We then document the avaliable options in a comment block above the method.<br /><br />I really like the expressiveness of method calls with an options hash.Gernothttps://www.blogger.com/profile/08836163611074377149noreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-33168082358203749612011-12-19T07:43:20.955+01:002011-12-19T07:43:20.955+01:00Definitely, it's a smell. But it's a style...Definitely, it's a smell. But it's a style that has been promoted (sanctified, even) by our favorite framework :)<br /><br />On the other hand, if it's really an "opts-style" parameter, what alternative would be better? A 20 boolean parameters? A new class every time?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-7821856652257554779.post-65615563676217857762011-12-18T22:50:10.193+01:002011-12-18T22:50:10.193+01:00I agree so much! Hashes are really great tool unle...I agree so much! Hashes are really great tool unless they are used as method parameters.<br />I always try not to do that by defining methods with multiple "simple type" parameters, but...<br />Do you know active_doc library? (https://github.com/iNecas/active_doc) in shortcut it allows you to define more info about method parameters (even gives some kind of error or exception, i dont remember which one, if parameter does not goes along with definition)<br />For example if you have method def do_sth(param)...<br />You can define that param must be hash which must have specific keys and value of each of them must have specific type. What do you think about it?koszczhttps://www.blogger.com/profile/11994662942282786301noreply@blogger.com