Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conventions, Style, and Usage for Clojure Constants?

Tags:

clojure

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

For example, is this right?

(def *PI* 3.14)

Questions:

Should constants be capitalized in Clojure?

Stylistically, should they have the asterisk (*) character on one or both sides?

Any computational efficiency considerations I should be aware of?

like image 992
Julien Chastang Avatar asked Aug 26 '10 20:08

Julien Chastang


2 Answers

From http://dev.clojure.org/display/community/Library+Coding+Standards:

Use earmuffs only for things intended for rebinding. Don't use a special notation for constants; everything is assumed a constant unless specified otherwise.

like image 53
Max Avatar answered Sep 27 '22 23:09

Max


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.

like image 41
Rayne Avatar answered Sep 28 '22 01:09

Rayne