bbatsov / clojure-style-guide

A community coding style guide for the Clojure programming language
https://guide.clojure.style
4k stars 279 forks source link

Indentation of keywords in head position of vector #14

Open drcode opened 11 years ago

drcode commented 11 years ago

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.

Gonzih commented 11 years ago

:+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"]]))
uvtc commented 11 years ago

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.

joelittlejohn commented 11 years ago

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?

drcode commented 11 years ago

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?)

joelittlejohn commented 11 years ago

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

drcode commented 11 years ago

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.

uvtc commented 11 years ago

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.

technomancy commented 11 years ago

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

harold commented 5 years ago

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"]]
joelittlejohn commented 5 years ago

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