I am writing a Clojure library and I am wondering what is the best practice for setting configuration parameters of a library.
Many libraries (like those in clojure-contrib) use global level parameter like *buffer-size*
which the user can set by calling set!
on them. But this does not seems to be best way to me as it creates a global state and there are chances of name collision.
The other way is to pass the parameters in every function call that is dependent on them. If there are many parameters then a map of them can be used instead of passing individual ones.
As an example, let's say that I am writing a cache library.
Using the first approach I have global parameters like *cache-size*, *expiry-time*, *cache-dir*
etc. The user set!
s these (or not and let them be default) and calls functions like (set-in-cache id obj)
and (get-from-cache id)
.
Using the second approach, the user first create a map of parameters and passes it to every call
(def cache-parameters {:cache-size 1000
:expiry-time: 1440
:cache-dir "c:\\cache"})
(set-in-cache cache-parameters id obj)
(get-from-cache cache-parameters id)
So which way is the preferred way in Clojure and why?