Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Clojure convention for naming private functions?

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! ;)

like image 361
Josh Glover Avatar asked Jun 01 '12 07:06

Josh Glover


People also ask

What is the convention of naming private methods in Python?

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 “__”.

How do you name a private method in C++?

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.


2 Answers

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.

like image 165
Isaac Avatar answered Oct 16 '22 22:10

Isaac


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 [])
like image 25
Julien Chastang Avatar answered Oct 16 '22 23:10

Julien Chastang