Is there a Clojure convention for naming private f

2019-03-17 05:17发布

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

2条回答
smile是对你的礼貌
2楼-- · 2019-03-17 05:39

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-17 05:47

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 [])
查看更多
登录 后发表回答