Idiomatic Clojure indentation – wide or deep?

Is it more idiomatic to write Clojure code wide or deep? Should I keep forms on the top line and use the full width of the page, or split the form and increase the line count but reduce horizontal scanning?

Brave Clojure presents its hobbit example like this:

But to me this narrower version seems easier to scan:

cljfmt doesn’t appear to object to either version there, but it does want to format this:

…with additional indentation, presumably to align the first parameter of str with the second:

Are there any rules or general guidance about when to wrap forms onto new lines, or is it largely a matter of taste and preference and being consistent?

I find myself mostly preferring narrow style (deep, not wide), but the problem is that there is no idiomatic answer :confused:

To further confuse things, there is a third style common in EDN config files. To take your example, you wouldn’t normally see this style in code:

(def asym-hobbit-body-parts [ ;; <-- content behind the open-paren
  {:name "head" :size 3}
  {:name "left-eye" :size 1}
  {:name "left-ear" :size 1}

But this is becoming pretty common in config files:

 :deps { ;; <-- content behind the open-paren
   org.clojure/clojure {:mvn/version "1.8.0"}
   ring {:mvn/version "1.5.0"}
   hiccup {:mvn/version "1.0.5"}

Anyway, here are my suggestions:

  • Ask this question by filing an issue in the Clojure Style Guide. Bozhidar will certainly have an opinion on this and reply, and will probably add some resulting guidelines. (Whether or not they are followed by the community is a different matter, but it’s good to have at least something to point to from someone heavily opinionated on these matters. If there’s an authority on this topic, it’s definitely him.)
  • Check out zprint to see what it does. It’s probably the most serious Clojure formatter I’ve seen. There are a lot of options, even coining some terms like hang and flow to describe some Clojure indentation idioms pretty well.
1 Like