Is there a Clojure convention for naming private functions?

末鹿安然 提交于 2019-12-03 15:39:46

问题


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


回答1:


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.




回答2:


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 [])


来源:https://stackoverflow.com/questions/10846423/is-there-a-clojure-convention-for-naming-private-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!