views:

321

answers:

4

As a non-lisper coming to clojure how should I best understand the naming convention where vars get a name like *var-name*?

This appears to be a lisp convention indicating a global variable. But in clojure such vars appear in namespaces as far as I can tell.

I would really appreciate a brief explanation of what I should expect when an author has used such vars in their code, ideally with a example of how and why such a var would be used and changed in a clojure library.

+5  A: 

Stuart Halloway's book says:

Vars intended for dynamic binding are sometimes called special variables. It is good style to name them with leading and trailing asterisks.

So it denotes you are using it as a threadlocal variable. If you don't use asterisks that means you are saying the var will keep its root binding.

Nathan Hughes
+2  A: 

Some references I found in the Clojure newsgroups:

Re: making code readable John D. Hume Tue, 30 Dec 2008 08:30:57 -0800

On Mon, Dec 29, 2008 at 4:10 PM, Chouser wrote: I believe the idiom for global values like this is to place asterisks around the name.

I thought the asterisk convention was for variables intended for dynamic binding. It took me a minute to figure out where I got that idea. "Programming Clojure" suggests it (without quite saying it) in chapter 6, section 3.

"Vars intended for dynamic binding are sometimes called special vari-
ables. It is good style to name them with leading and trailing asterisks."

Obviously the book's a work in progress, but that does sound reasonable. A special convention for variables whose values change (or that my code's welcome to rebind) seems more useful to me than one for "globals" (though I'm not sure I'd consider something like grid-size for a given application a global). Based on ants.clj it appears Rich doesn't feel there needs to be a special naming convention for that sort of value.

and...

I believe the idiom for global values like this is to place asterisks around the name. Underscores (and CamelCase) should only be used when required for Java interop:

(def *grid-size* 10)
(def *height* 600)
(def *margin* 50)
(def *x-index* 0)
(def *y-index* 1)
Carl Smotricz
Is markdown breaking your code example? I assume the stuff that is italicized should be in asterisks such as `*grid-size*`? (I escaped the `*` by putting them in backticks.)
Alex Stoddard
Definitely, and thanks for pointing it out! Let me see if I can get that fixed...
Carl Smotricz
+8  A: 

It's a convention used in other Lisps, such as Common Lisp, to distinguish between special variables, as distinct from lexical variables. A special or dynamic variable has its binding stored in a dynamic environment, meaning that its current value as visible to any point in the code depends upon how it may have been bound higher up the call stack, as opposed to being dependent only on the most local lexical binding form (such as let or defn).

Note that in his book Let Over Lambda, Doug Hoyte argues against the "earmuffs" asterix convention for naming special variables. He uses an unusual macro style that makes reference to free variables, and he prefers not to commit to or distinguish whether those symbols will eventually refer to lexical or dynamic variables.

Though targeted specifically at Common Lisp, you might enjoy Ron Garret's essay The Idiot's Guide to Special Variables. Much of it can still apply to Clojure.

seh
For others like me, who are grappling with new concepts using clojure as a first lisp a reference detailing more of how clojure handles binding is useful for comparison. I found this blog http://clivetong.spaces.live.com/blog/cns!3F21DF299C355E7F!311.entry helpful.
Alex Stoddard
+1  A: 

Functional programming is all about safe predictable functions. Infact some of us are afraid of that spooky "action at a distance" thing. When people call a function they get a warm fuzzy satisfaction that the function will always give them the same result if they call the function or read the value again. the *un-warm-and-fuzzy* bristly things exist to warn programmers that this variable is less cuddly than some of the others.

Arthur Ulfeldt