-
Notifications
You must be signed in to change notification settings - Fork 279
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
Keyword arguments vs multi-arity functions #166
Comments
I don't know. I usually go with multi-arity when the params for a function are stable. Here's some thoughts that resonate with my perspective https://stuartsierra.com/2015/06/01/clojure-donts-optional-arguments-with-varargs Any thoughts from someone else? |
I think the difficult thing about more than one optional is that it might make you pass nil values to the function when calling it: (my-fun required nil 1 nil) As I see it, function calls with nils in the arguments are less readable and clear. Compare that to: (my-fun required :optional2 1) Which doesn't leave you thinking "what are all these nils about"? |
FWIW... I agree with erez-rabih that when there are multiple optional and independent arguments, keyword args should be preferred. However, multi-arity should be preferred when each additional optional arg requires the preceding one to be present. As a trivial example, imagine a You can equally say it's more readable to use keyword arguments, or too verbose. This is up to taste and the situation imo. A function which takes keyword arguments is more open to extension, whereas a multi-arity function is more prescriptive about what it accepts. Again, both situationally advantageous. |
I think this is relevant: https://clojure.org/guides/destructuring#_keyword_arguments Also this quote from there:
So, the form:
Is also valid. Another thing to mention. In all cases, it is possible to set default values. In multi-arity asigning the default value instead of |
I would like to propose the following guideline:
Prefer keyword arguments on multi arity when a function allows 2 or more optional parameters to avoid passing nil on function call.
The text was updated successfully, but these errors were encountered: