Indenting cond forms

Recently I came across this blog post on indenting cond forms: http://blog.danieljanus.pl/2020/02/10/cond-indentation/ and I must say it rung a bell - I personally love the shape:

    (cond
       a?    some-a
       b?    some-b
       :else toobad)

but of course it’s not always possible. I personally don’t find very legible the shape:

    (cond
        a?
        some-a
        b?
        some-b
        :else
        toobad)

especially if some-a and some-b are multi-line expressions.

The reader macro mentioned in the article

(cond
  (= (some-function something) expected-value)
  #_=> (do
         (do-this)
         (and-also-do-that))
  (another-predicate something-else)
  #_=> (try
         (do-another-thing)
         (catch Exception _
           (println "Whoops!"))))

it looks nicer, but of course it’s a hack… what do you think?

1 Like

It does not comport with the philosophy of minimalism in syntax.
It is hard to type.
And it will be overtaken by editors’ Spec-driven style guide after Spec gels.
In summary,… hmm… it looks so nice… Three stars.

I like the one without #_=>. They mention Emacs will re-indent it, but I think you could create a custom indent rule for Emacs which would do it.

Good idea! Do you have an idea on how to create a rule for this?

Otherwise, I prefer

(cond 
  a?
  , some-a
  b?
  , some-b
  :else
  , toobad)
1 Like

Personally I do something like:

  (cond
      ;; first case: explain a
      a?
      some-a

      ;; second case: explain b
      b?
      some-b

      :else
      toobad)

That is, newlines and comments. The rationale is that either the condition or the expression is complex, requiring at least one word of explanation.

2 Likes

This requires newlines, that appear to be a no-no in the style bible…

well, I don’t follow such strict (IMHO silly when generalized) rules… (and I’ve not heard of any style “bible”). I would at least keep the comments in my proposition.

1 Like

I also use a blank line to separate pairs when it is complex.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.