Function parameteres

There are no hard rules or conventions around this.

Some thoughts:

  • Lately the clojure CLI has added support for executing functions from the command line with clojure -X my-ns/my-fn :foo 1 :bar 2 so if you’d like to support that then go with one map argument.
  • It’s fine to put required arguments first to ensure they are there and passing the options last: (foo req1 req2 {:opt1 ...}), an idiom which is well established I’d say.
  • If you’re designing an API, it might be best to go with a consistent style: choose one or the other
  • Both the single map or required args first approaches don’t ensure that the correct things are passed: you can still make typos in a map key or pass the arguments in the wrong positions. To really have some guarantees around this you’ll have to make some assertions. You can do this via:
    • clojure.spec s/fdef or s/assert
    • clojure.core/assert
    • other libraries like Schema / plumbing (defnk), malli, …
      Usually these libraries have some kind of toggle to disable the assertions in production, so you will also have to think about if these assertions are essential in production or OK to use only in development.
4 Likes