bbatsov / clojure-style-guide

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

Narrow or wide formatting? #161

Open nickcernis opened 6 years ago

nickcernis commented 6 years ago

Should Clojurists favour narrow formatting with top-level forms on new lines, like this:

(def asym-hobbit-body-parts
  [{:name "head" :size 3}
   {:name "left-eye" :size 1}
   {:name "left-ear" :size 1}
   {:name "mouth" :size 1}
   {:name "nose" :size 1}
   {:name "neck" :size 2}
   {:name "left-shoulder" :size 3}
   {:name "left-upper-arm" :size 3}
   {:name "chest" :size 10}
   {:name "back" :size 10}
   {:name "left-forearm" :size 3}
   {:name "abdomen" :size 6}
   {:name "left-kidney" :size 1}
   {:name "left-hand" :size 2}
   {:name "left-knee" :size 2}
   {:name "left-thigh" :size 4}
   {:name "left-lower-leg" :size 3}
   {:name "left-achilles" :size 1}
   {:name "left-foot" :size 2}])

Or wider formatting that avoids splitting forms with line breaks unless they extend beyond 80 characters, like this?

(def asym-hobbit-body-parts [{:name "head" :size 3}
                             {:name "left-eye" :size 1}
                             {:name "left-ear" :size 1}
                             {:name "mouth" :size 1}
                             {:name "nose" :size 1}
                             {:name "neck" :size 2}
                             {:name "left-shoulder" :size 3}
                             {:name "left-upper-arm" :size 3}
                             {:name "chest" :size 10}
                             {:name "back" :size 10}
                             {:name "left-forearm" :size 3}
                             {:name "abdomen" :size 6}
                             {:name "left-kidney" :size 1}
                             {:name "left-hand" :size 2}
                             {:name "left-knee" :size 2}
                             {:name "left-thigh" :size 4}
                             {:name "left-lower-leg" :size 3}
                             {:name "left-achilles" :size 1}
                             {:name "left-foot" :size 2}])

I find the first version easier to scan but don't see any general advice in the guide about this.

vemv commented 6 years ago

Funny, I just raised this issue independently:

https://github.com/bbatsov/clojure-style-guide/issues/162

Mine is concerned with fn calls though. I'd say for calls, the generally better choice is quite clear. For data/defs (such as your example) there's more room for subjectivity.

In your example, I'd prefer the former as:

bbatsov commented 6 years ago

I think I've seen way more usages of the first style in the wild. Someone has to check this, though. Generally I don't have any issues with recommending this in the guide.