# Cond indentation/readabliity, continuation syntax proposal for expr

**URL:** https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825
**Category:** General Questions
**Tags:** editors
**Created:** [July 6, 2021, 6:41pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825 "2021-07-06T18:41:32Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![MicahElliott](https://clojureverse.org/user_avatar/clojureverse.org/micahelliott/32/2931_2.png) [@MicahElliott](https://clojureverse.org/u/MicahElliott)
#### Post date: [July 6, 2021, 6:41pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/1 "2021-07-06T18:41:32Z")

</div>

Hi! Wanted to get some feedback on an annoyance and possible fix for `cond` and similar forms…

I frequently encounter/write code that runs into line length/spacing issues. I think this impacts most of us, whether you prefer 80 chars line max or 120 or whatever. Example:

```auto
  ;; Typical nice case; alignment helps
  (cond
    (short1) (short2)
    (still-short1) (still-short2)
    :else (short3))

  ;; Problematic longer case; lines can get VERY long
  (cond
    (some-really-long-call :to "whatever") (do-something-also-long)
    (some-other-thing :with "even" :more "args") (some-other-action)
    :else (the-last-possible-thing))

  ;; Break into newlines, but hard to tell TEST from EXPR lines! (what I usually do)
  (cond
    (some-really-long-call :to "whatever")
    (do-something-also-long)
    (some-other-thing :with "even" :more "args")
    (some-other-action)
    :else
    (the-last-possible-thing))

  ;; Readable and typical, but just blew up from nice 3 lines to 9; big problem IMO
  (cond

    (some-really-long-call :to "whatever")
    (do-something-also-long)
    ;; Maybe a comment here to separate, or just another wasteful blank line
    (some-other-thing :with "even" :more "args")
    (some-other-action)

    :else
    (the-last-possible-thing))

  ;; How about this??
  (cond
    (some-really-long-call :to "whatever")
    #_> (do-something-also-long)
    (some-other-thing :with "even" :more "args")
    #_> (some-other-action)
    :else
    #_> (the-last-possible-thing))

```

Thoughts on the `#_>` comment/indicator? Any big problems you could see it causing? Any alternative you like better? It works well with Emacs’ default clojure formatting, and also [cljfmt](https://github.com/weavejester/cljfmt) (should try with zprint and other editors). It also looks pretty good with a ligature/unicode substitution (though I don’t think these arrows are the ideal substitution), like:

 ![Screen Shot 2021-07-06 at 11.12.39 AM](https://clojureverse.org/uploads/default/original/2X/9/9f0d8ada9d495beb0b585628acf34432dd7e4aee.png)

We could come up with some other macro like `#>` but maybe not worth it since `#_` works for cases I can think of, and is still short.

This should also probably apply to `case`, `condp`, `cond->`, and maybe others. It’s even almost nicely congruent with `condp`’s `:>>`.

Here’s the Emacs setup ([thanks](https://endlessparentheses.com/using-prettify-symbols-in-clojure-and-elisp-without-breaking-indentation.html) to Malabarba!) that makes the pretty arrow (I assume can be done similarly for other editors):

```auto
(global-prettify-symbols-mode)
(setq prettify-symbols-alist '(("#_>" . (?\s (Br . Bl) ?\s (Br . Bl) ?\s
					     (Bc . Br) #x21a6 (Bc . Bl) #x21a6))))

```

_(Yes, we could change all our editors and tools to indent the EXPR line by a couple chars, but I can’t imagine getting teams to agree how to do that. This proposal alone may also be difficult to get people to agree on, but at least it seems like a harmless, optional convention that doesn’t break anything.)_

---

<div class="post-metadata">

### Author: ![seancorfield](https://clojureverse.org/user_avatar/clojureverse.org/seancorfield/32/195_2.png) [@seancorfield](https://clojureverse.org/u/seancorfield)
#### Post date: [July 6, 2021, 6:53pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/2 "2021-07-06T18:53:21Z")

</div>

If I have long conditions and need to have my `cond` formatted as alternating lines of condition/expression, I just add blank lines between each pair to add readability. I haven’t found anything else that is both simple enough and readable enough to be worth the effort of introducing a custom convention.

---

<div class="post-metadata">

### Author: ![MicahElliott](https://clojureverse.org/user_avatar/clojureverse.org/micahelliott/32/2931_2.png) [@MicahElliott](https://clojureverse.org/u/MicahElliott)
#### Post date: [July 6, 2021, 7:03pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/3 "2021-07-06T19:03:04Z")

</div>

I agree that having to introduce a convention is not ideal. Though I’m considering trying it.

My first approach to this was trying to get my editor to do some sort of overlay (and not need any special comment addition) to make the TEST line stand out, but I’d need to do some research to get it to work (if even possible), like:

 ![Screen Shot 2021-07-06 at 12.00.26 PM](https://clojureverse.org/uploads/default/original/2X/2/2335e49d3a7d35151d0f72d98584f4fa1a60aa9f.png)

Actually, I think a `§` (section sign) or `¶` (pilcrow) is a better/good symbol than the triangle.

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [July 6, 2021, 10:41pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/4 "2021-07-06T22:41:58Z")

</div>

I think in terms of code, it shouldn’t cause issues that I can think of.

Now as too if I like it better than proper indent or spaces not sure.

You could also try with a comma:

```auto
(cond
    (some-really-long-call :to "whatever")
    , (do-something-also-long)
    (some-other-thing :with "even" :more "args")
    , (some-other-action)
    :else
    , (the-last-possible-thing))

;; Or

(cond
    (some-really-long-call :to "whatever")
    (do-something-also-long)
    ,
    (some-other-thing :with "even" :more "args")
    (some-other-action)
    ,
    :else
    (the-last-possible-thing))

;; Or

(cond
    (some-really-long-call :to "whatever")
    (do-something-also-long),
    (some-other-thing :with "even" :more "args")
    (some-other-action),
    :else
    (the-last-possible-thing))

```

---

<div class="post-metadata">

### Author: ![MicahElliott](https://clojureverse.org/user_avatar/clojureverse.org/micahelliott/32/2931_2.png) [@MicahElliott](https://clojureverse.org/u/MicahElliott)
#### Post date: [July 6, 2021, 11:23pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/5 "2021-07-06T23:23:41Z")

</div>

Thanks @didibus! The comma is pretty good and simple. I definitely prefer (and I think like) your first example. It’s a little trickier to create a “pretty symbol” for, but maybe it’s simple enough that it does the job and doesn’t need a glyph. Could even make it a double-comma.

Oh but alas, cljfmt is pretty particular about commas and does not like it (even though Emacs treats it as expected).

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [July 6, 2021, 11:51pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/6 "2021-07-06T23:51:33Z")

</div>

> [@MicahElliott](#):
>
> Oh but alas, cljfmt is pretty particular about commas

What does it do with it?

---

<div class="post-metadata">

### Author: ![MicahElliott](https://clojureverse.org/user_avatar/clojureverse.org/micahelliott/32/2931_2.png) [@MicahElliott](https://clojureverse.org/u/MicahElliott)
#### Post date: [July 7, 2021, 12:08am UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/7 "2021-07-07T00:08:06Z")

</div>

It treats commas as whitespace, so ignores them, and then complains that the EXPR lines should not be indented. Looks like [using](https://github.com/weavejester/cljfmt#configuration) `:remove-surrounding-whitespace?` option does not affect this.

```auto
 (cond
   (some-really-long-call :to "whatever")
- , (do-something-also-long)
+ (do-something-also-long)
   (some-other-thing :with "even" :more "args")
- , (some-other-action)
+ (some-other-action)
   :else
- , (the-last-possible-thing
- :foo))
+ (the-last-possible-thing
+ :foo))

```

---

<div class="post-metadata">

### Author: ![didibus](https://clojureverse.org/user_avatar/clojureverse.org/didibus/32/951_2.png) [@didibus](https://clojureverse.org/u/didibus)
#### Post date: [July 7, 2021, 12:18am UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/8 "2021-07-07T00:18:16Z")

</div>

Ah, interesting thanks.

---

<div class="post-metadata">

### Author: ![Serioga](https://clojureverse.org/user_avatar/clojureverse.org/serioga/32/5290_2.png) [@Serioga](https://clojureverse.org/u/Serioga)
#### Post date: [July 7, 2021, 7:02am UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/9 "2021-07-07T07:02:45Z")

</div>

Cursive moves commas to the end of previous line when reformats code.

---

<div class="post-metadata">

### Author: ![jr0cket](https://clojureverse.org/user_avatar/clojureverse.org/jr0cket/32/670_2.png) [@jr0cket](https://clojureverse.org/u/jr0cket)
#### Post date: [July 14, 2021, 11:09pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/10 "2021-07-14T23:09:24Z")

</div>

Feels like adding complexity to anyone reading the codebase. It could easily lead to questions like

Is this a new form of reader macro added onto the language…

Where is this macro defined, is it on the project or in a library that was imported.

Introducing additional ‘convention’ that can easily be mistaken for syntax seem to add complexity

A blank line does not suffer this complexity

---

<div class="post-metadata">

### Author: ![camdez](https://clojureverse.org/user_avatar/clojureverse.org/camdez/32/1373_2.png) [@camdez](https://clojureverse.org/u/camdez)
#### Post date: [August 8, 2021, 2:43am UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/11 "2021-08-08T02:43:17Z")

</div>

Sorry for resurrecting an old thread, but I was excited to see @didibus mention using commas here. I do exactly this. These days I’m an engineering team of one at CollBox, so I don’t yet know how this will go over with other engineers, but I’ve been test-driving this for a few months now and I’m pretty happy with it.

I actually use commas whenever I want to violate the normal Clojure indentation standards. Typically that comes in three cases:

1. The `cond` example described above:

```auto
(cond
  (extremely-long-condition with some lengthy args)
  , :foo
  (another-extremely-long-condition here)
  , :bar)

```

But that’s probably my rarest usage of it. More commonly I use this with…

1. Datomic queries with multiple `:where` clauses that I want to align, but I like having them indented after the `:where` keyword like the rest of the key-value “pairs”:

```auto
(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:

```auto
(let [name "Bilbo Baggins"
      email "bilbo@shire.org"
      confirmed? false
      something-annoyingly-long
      , 42]
  ; ...
  )

```

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_.

---

<div class="post-metadata">

### Author: ![PEZ](https://clojureverse.org/user_avatar/clojureverse.org/pez/32/1187_2.png) [@PEZ](https://clojureverse.org/u/PEZ)
#### Post date: [August 9, 2021, 8:20am UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/12 "2021-08-09T08:20:32Z")

</div>

I think it’s cool to align things up in ways that makes the code easy to read.

However, different formatters will treat this differently. I know Calva, which uses `clj-fmt` will just delete those commas, as if they where whitespace.

---

<div class="post-metadata">

### Author: ![system](https://clojureverse.org/uploads/default/original/2X/5/51079bf9e4b7d9466242c06cf1e43b9f8bd6da14.png) [@system](https://clojureverse.org/u/system)
#### Post date: [February 7, 2022, 8:20pm UTC](https://clojureverse.org/t/cond-indentation-readabliity-continuation-syntax-proposal-for-expr/7825/13 "2022-02-07T20:20:57Z")

</div>

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