Clojure Conventions

Learn about the Clojure style guide that recommends best practices in the layout and organization of our source code.

We'll cover the following...

Like most languages, Clojure has conventions and best practices for the layout and organization of source code. Let’s take a look at these below.

Names

As you might have noticed, Clojure uses the kebab-case to define most of the names in Clojure, which means that the names are all in lowercase and separated by the dash symbol (-). We’ll define def, defn, bindings in general, and many others with this style.

Press + to interact
(def my-variable-with-many-information 10)

Use PascalCase for protocols, records, structs, and types.

Press + to interact
(defprotocol Dog
(bark [dog] "A function to represent the sound of the bark")
(eat [dog] "A function representing the action of eating")
(sleep [dog] "A function representing the action of sleeping"))
(defrecord Labrador []
Dog
(bark [dog] (println "Woof, woof."))
(eat [dog] (println "I WANT IT ALL"))
(sleep [dog] (println "SLEEP 1 . 2 . 3")))

The names of predicate functions, which return a boolean value, should end in a question mark (?). In this way, we always know that that function is a predicate. A Clojure example is the even? function.

Press + to interact
(defn valid-numbers?
[a b]
(= a b))
...

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy