Clojure Indentation Commas

Lately I’ve been dabbling in using commas to control indentation in Clojure. I’m sure this will be a contentious one, but I wanted to put it out there to spur converation.

Commas are simply treated as whitespace in Clojure, so they’re sometimes used to add visual clarity between key / value pairs, or even as an approximation of an ellipsis in code samples:

(defn handler [{:keys [db], :as req}]
  ,,,)

But don’t get the wrong idea and try to use them in numeric literals:

;; Probably not what you intended
[1,000]
;;=> [1, 0]

Recently I’ve been using commas in a few specific scenarios where I want to violate the typical rules of Clojure indentation. Most notably:

  1. Datomic queries with multiple :where clauses that I want to align. I like having them indented after the :where keyword like the rest of the key-value “pairs”:
(datomic.api/q '[:find ?user
                 :in $ ?email
                 :where [?user :user/email ?email]
                 ,      [?user :user/role :user.role/admin]]
               db email)

(And yes, I am aware of the map syntax for q.)

  1. Compactly-aligned let bindings where I have one or two lengthy names I’m binding and I don’t want to shift all of the values annoyingly far right:
(let [name       "Bilbo Baggins"
      email      "bilbo@shire.org"
      confirmed? false
      something-annoyingly-long
      ,          42]
  ; ...
  )
  1. To add visual clarity to cond, emphasizing which forms are the conditions and which are the branch logic:
(cond
  (extremely-long-condition with some lengthy args)
  , (frobnicate bits)
  (another-extremely-long-condition here)
  , (reticulate splines)

I’m sure somebody will say this is the worst thing ever, but I firmly believe that indentation impacts readability, and while the comma trick isn’t exactly beautiful, it lets me express the patterns in the code that I want a reader to notice, without making me fight my editor’s indentation features, and it calls out the non-standard indentation as being intentional.

What do you think? Useful? Insufferable? I first posted about this on ClojureVerse, so feel free to comment there or find me on Twitter.

I suppose we could call these “indentation commas”? Or maybe in a world with “Rich comments” and “Stuart Sierra components”, we just call these “Cameron commas”? 😜