Conventions, Style, and Usage for Clojure Constants?

前端 未结 8 1231
余生分开走
余生分开走 2021-02-02 06:48

What are the best practices for defining constants in Clojure in terms of style, conventions, efficiency, etc.

8条回答
  •  清歌不尽
    2021-02-02 07:14

    I don't think there is any hard and fast rules. I usually don't give them any special treatment at all. In a functional language, there is less of a distinction between a constant and any other value, because things are more often pure.

    The asterisks on both sides are called "ear muffs" in Clojure. They are usually used to indicate a "special" var, or a var that will be dynamically rebound using binding later. Stuff like out and in which are occasionally rebound to different streams by users and such are examples.

    Personally, I would just name it pi. I don't think I've ever seen people give constants special names in Clojure.

    EDIT: Mister Carper just pointed out that he himself capitalizes constants in his code because it's a convention in other languages. I guess this goes to show that there are at least some people who do that.

    I did a quick glance through the coding standards but didn't find anything about it. This leads me to conclude that it's really up to you whether or not you capitalize them. I don't think anyone will slap you for it in the long run.

提交回复
热议问题