clojure-style-guide icon indicating copy to clipboard operation
clojure-style-guide copied to clipboard

Indentation of keywords in head position of vector

Open drcode opened this issue 11 years ago • 10 comments

I know this isn't a widely accepted Clojure indentation rule yet, but consider the following code:

(defn foo []
  (html [:div {:class "bar"}
         [:span "first child"]
         [:span "second child"]]))

I think in 99.9% of cases having a keyword as the initial position of a vector indicates that the vector is representing a syntax expression for a DSL. In this case, it makes sense to indent the other items in the vector in the same manner as function arguments:

(defn foo []
  (html [:div {:class "bar"}
              [:span "first child"]
              [:span "second child"]]))

In any case I've ever come across this situation in my own code, this would be the more sensible way to do indentation. If, per chance, other Clojurists agree with my thinking, I think it would be an awesome addition to your style guide.

drcode avatar Jan 04 '13 21:01 drcode

:+1: I like that indention.

btw you can specify clojure highlighting in comments on github, like so:

(defn foo []
  (html [:div {:class "bar"}
              [:span "first child"]
              [:span "second child"]]))

Gonzih avatar Jan 06 '13 13:01 Gonzih

Personally, I'm fine with the standard indentation on this one. It's still a bunch of items in a vector, and I think it should look that way.

uvtc avatar Jan 06 '13 18:01 uvtc

I think the 99% claim needs some evidence :)

The counter example is of course something like:

(def colors [:red :yellow :pink :green
             :purple :orange :blue])

Is this for some reason nonsensical?

joelittlejohn avatar Jan 30 '13 17:01 joelittlejohn

joelittlejohn: How do you decide to put a newline at ":green"? I would either put all the colors on one line or put them all in separate lines... In either of those cases the issue is moot.

(FYI, I know some folks follow an 80 character line length rule- I would agree that if you follow that rule it would conflict with my indentation rule, but does anyone do this anymore?)

drcode avatar Jan 30 '13 18:01 drcode

Some examples:

https://github.com/clojure/clojure/blob/1.3.x/test/clojure/test_clojure/predicates.clj#L97

https://github.com/overtone/overtone/blob/release/0.8.0/src/overtone/examples/gui/keynome.clj#L6 https://github.com/overtone/overtone/blob/release/0.8.0/src/overtone/examples/compositions/blues.clj#L47

joelittlejohn avatar Jan 30 '13 18:01 joelittlejohn

joelittlejohn: I woudln't write code like shown in your examples. However, I agree having these use cases in current code is a good argument against my position.

drcode avatar Jan 30 '13 18:01 drcode

johnlittlejohn, I don't think your example above is nonsensical. It just looks like you didn't want your line to be too long. Now, I think this looks bad:

(def colors [:red    :yellow
             :pink   :green
             :purple :orange])

because it makes those items look like pairs or entries in a map, when they're not.

uvtc avatar Jan 30 '13 18:01 uvtc

Just a heads-up, but this is never going to happen in clojure-mode; sorry.

technomancy avatar Feb 14 '13 23:02 technomancy

Here is a case from hiccup that I encounter from time to time:

Current:

[:ol [:li "1"]
 [:li "2"]
 [:li "3"]]

Also current (but slightly sad):

[:ol
 [:li "1"]
 [:li "2"]
 [:li "3"]]

Better?:

[:ol [:li "1"]
     [:li "2"]
     [:li "3"]]

harold avatar Apr 04 '19 16:04 harold

@harold I prefer your Also current (but slightly sad): example :stuck_out_tongue: Your last example produces a lot of unnecessary horizontal indention that can get wasteful very quickly.

joelittlejohn avatar Apr 04 '19 17:04 joelittlejohn