When I define a private function in Clojure, I usually use a -
prefix as a visual indicator that the function cannot be used outside of my namespace, e.g.
(defn- -name []
(let [formatter (formatter "yyyy-MM-dd-HH-mm-ss-SSSS")]
(format "fixjure-%s" (unparse formatter (now)))))
But the -
prefix seems to also be a convention for public methods when using gen-class
.
Is there any generally accepted convention for defn-
'd functions in the Clojure community, or should I simply use non-prefixed names?
It seems that lots of code in clojure.contrib (may it rest in peace) uses normal names for private functions, so maybe that is best, but I really like the visual indicator--maybe my C / Perl background is just too strong! ;)
Private methods are those methods that should neither be accessed outside the class nor by any base class. In Python, there is no existence of Private methods that cannot be accessed except inside a class. However, to define a private method prefix the member name with the double underscore “__”.
Names that begin with an underscore followed by a capital letter are reserved to the implementation. Names that begin with an underscore followed by a lowercase letter are reserved to the implementation for use in global scope. In a non-global scope, a leading underscore followed by a lowercase letter is okay.
There's not a convention; the visual indicator is prevalent in languages with no built-in notion of private functions. Since Clojure's functions defined with defn-
are not visible outside their namespace, there is no need to prefix functions with an uglifier ;)
So do what you but, but you should probably want to just do as the rest of community does and just name them normally! It'll make your life easier.
I am unaware of any naming conventions but you can attach ^:private
metadata tag for defining private functions. This is exactly equivalent to defn-
, but is a little clearer, IMHO.
(defn ^:private foo [])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With