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

Narrow or wide formatting?

Open nickcernis opened this issue 6 years ago • 2 comments

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.

nickcernis avatar Nov 18 '17 17:11 nickcernis

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:

  • it frees up more horizontal space to be used (one may use e.g. 80 characters max)
  • it's data. I'd say it's great to have different styles for code and data. Once one follows this consistently, visual scanning of codebases becomes a pleasure

vemv avatar Nov 23 '17 03:11 vemv

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.

bbatsov avatar Aug 27 '18 07:08 bbatsov