qiuxiafei
qiuxiafei

Reputation: 5977

What's the usual naming rule in clojure?

As i see, clojure has more characters for variable name than c/c++/java. For example:

Functions end with '?' usually return a Boolean, they are predicate.

There are also variables starting with '-', or ending with '!'.

i think these are all clojure-style naming. So, what's the usual naming rule in clojure? is there something in common for clojure programmers?

Upvotes: 15

Views: 3559

Answers (3)

omiel
omiel

Reputation: 1593

Apart from the Library Coding Standards mentioned by @mikera, there is now a (community-driven) Clojure style guide: https://github.com/bbatsov/clojure-style-guide

Upvotes: 0

mikera
mikera

Reputation: 106351

It's worth looking at Clojure's Library Coding Standards which I think are still probably the best reference on Clojure coding style.

The main function naming conventions seem to be:

  • Use lowercase function names: frobnicate
  • Multiple word names use hyphens as separators: frobnicate-with-extra-fizz
  • Use namespaces to allow you to re-use good names if needed: my.special.collection/concat
  • Use ? to indicate a predicate that returns true or false: sequential?
  • Use ! to indicate a function with side effects that is not transaction safe, e.g.: set!

For local variables the following are common:

  • f, g, h - functions
  • n - integer representing a size or count
  • index, i - integer index
  • x, y - numbers
  • s - string input
  • coll - a collection
  • pred - a predicate closure
  • & more - variadic input

Upvotes: 25

PeterMmm
PeterMmm

Reputation: 24630

Clojure is a dialect of Lisp, so Lisp convention may apply: http://www.cliki.net/naming%20conventions

Upvotes: 2

Related Questions