Tips for Clojure Beginners

Ben Orenstein

1. Learn the essentials with Clojure Koans

Clojure Koans teaches you the basics of the language by providing a series of tests for you to turn green.

The topics and tests are chosen well, and the project’s vibe is pleasant (“calling a function is like giving it a hug with parentheses”).

Open a koan. Make it pass. Meditate. Enjoy enlightenment.

2. Move on to 4Clojure problems

4Clojure is a great way to become familiar with Clojure’s many built-in functions.

Make sure to register an account and follow a handful of the Top Users. This will let you compare your solutions to theirs and be suitably mindblown.

A word of warning: 4Clojure tends to encourage code golf. Shorter is not always better.

For the longer problems, you may prefer to work in your editor. Check out offline-4clojure to get a local copy of the problems and tests.

3. Read a book or two

Clojure Programming and The Joy of Clojure are both great places to start.

Clojure Programming is approachable, well-written, and no-nonsense. In particular, the examples are well-chosen and understandable.

The Joy of Clojure is also excellent, but takes more mental horsepower to get through. Its examples are perhaps more realistic, but thus more complicated and harder to follow.

4. Learn to develop interactively from your editor

As a Rubyist, I’m used to running tests from my editor, and would never adopt a workflow that forced me to switch to the shell to run my tests. Additionally, I use the Spring pre-loader because rebooting the application every time I make a change and want to test it is painful. The ability to get test feedback quickly, and in the same place I’m writing them, contributes greatly to my flow and sense of happiness.

Despite this, when I want to interact with a running version of my application, it’s off to the Rails console I go. I write code “over here,” but interact with my running application “over there.”

Clojurians eschew this separation.

When writing Clojure, I can connect my editor to an instance of my running application that sticks around. When I change a function, I instruct that persistent application session to simply use the new function without restarting or reloading.

Further, when I want to see how the system behaves there’s no need to head off to some “over there” place. Instead, I can evaluate Clojure code in the context of my running application right from Vim, with results displayed wherever I might want them.

I had read descriptions of this development style and felt somewhat underwhelmed, but getting this set up and working really changed how much I enjoyed writing Clojure. Make sure you at least give this an honest shot before moving on.

  • Vim users: to get this experience, install Tim Pope’s fireplace.vim and read enough of the docs to learn how to eval code and open an in-editor REPL. Outdated resources might point you to VimClojure, but it is abandonware and should be avoided.

  • Emacs users: cider is what you’re looking for.

  • LightTable users: your editor does this out of the box! How enlightened of it. Check your docs for details, or just start on David Nolen’s interactive ClojureScript tutorial.

  • Users of other editors: you probably want to google something like [your-editor-name nrepl server].

5. Absorb Clojure’s philosophies and motivations with conference talks

One of my favorite parts of the Clojure ecosystem is how many excellent conference talks are available. Three great examples, all from Rich Hickey, creator of Clojure:

  • Are We There Yet? - Rich asks whether OO as we practice it today is the best we can do. Spoiler: he thinks not. A great starting place to understand Clojure design motivations.

  • The Value of Values - Immutable data structures are a key element in Clojure’s design. This talk gives a great overview of their rationale and characteristics.

  • Simple Made Easy - Required viewing, if only because “complect” and “simple, in the Rich Hickey sense” are terms you’ll hear community members use often.

The above are some of my favorites, but I’ve been pleasantly surprised at the high quality of most Clojure talks I’ve watched, so don’t hestitate to dive into whatever looks interesting. For lots of options, check out the ClojureTV YouTube channel.

Bonus tip: I find I can watch most talks at 1.5x without a loss of comprehension. Enjoy that 40-minute talk in just 26!

6. Ask for help when stuck

I’ve had good luck getting unstuck by asking for help in #clojure on freenode (IRC), reaching out to library authors directly on Twitter (thanks @swannodette, @weavejester, and @cemerick!), and the usual swearing/staring/source-diving that is sofware development.

7. Don’t panic

Chances are, you’re coming to Clojure from an object-oriented languge with mutable data structures. Moving to a functional language with immutable data is a significant change of paradigm, and will take some getting used to. This is normal. Don’t feel bad if you struggle early on. I certainly did (and often still am)!