What are Clojure's Naming Conventions?
Can anyone explain or point me to where I can find clojure's naming conventions for:
- File names
- Functions (From what I understand, function names are simply dash separated values)
- Variables
Solution 1:
You might want to look at the Clojure library coding standards on the developer Wiki - this is probably the most comprehensive list that I've seen.
To your specific points:
- File names are lowercase, and stored in a directory structure to match the namespace, and end in .clj e.g. "my/special/namespace.clj
- Functions are dash-separated-lowercase-words, ideally descriptively chosen so that your code is clear and self-documenting. Don't be afraid to re-use good function names in different namespaces (that is what namespaces are for!).
- Variables (by which I assume you mean parameters, let-bound variables etc.) are also usually dash-separated-lowercase-words. Since code-is-data, I think it is appropriate that functions and data have the same naming convention :-)
Solution 2:
You might want to take a look at this non official style guide.
Solution 3:
There are some interesting guidelines on naming written by Stuart Sierra which suggest that:
- pure functions should be nouns describing the return value (
age
instead ofcalculate-age
) - side-effecting functions should be verbs describing the action (
create-
for constructing andget-
for retrieving), reserving the bangswap!
changes to mutable references. - verbs that can also be nouns should be distinguished as verb phrases (
send-message
instead ofmessage
) - coercions should name the output type without an arrow prefix (
connection
instead of->connection
) except when the input type must be explicit (input-type->output-type
) - namespace aliases can save on repetition (
products/price
instead ofproducts/product-price
) and prevent local clashes in let bindings - functions returning functions should have the
-fn
suffix
Solution 4:
There is an interesting set of naming conventions documented in a comment by Taoensso in his Encore library.
He proposes names using !
for side-effects, ?
for booleans,
$
for expensive operations, _
as dereffable,
*
for macros; plus a few other combos.