formatter
formatter copied to clipboard
Empty lines between forms
How many empty lines should go between forms? A few options here:
- Don't touch this at all, leave any newlines between forms alone
- One line between each form
- Two lines between each form
Originally posted by @PEZ in https://github.com/clj-commons/formatter/issues/2#issuecomment-445526899
Between top level forms Some people suggest to use two lines. Having tried that, I have kind of started to like it. But I am not using it for all forms.
(def foo :foo)
(def bar :bar)
(defn re-pos-first
"Find position of first match of `re` in `s`"
[re s]
(if-let [m (.match s re)]
(.-index m)
-1))
(defn split-into-lines
[s]
(clojure.string/split s #"\r?\n" -1))
(defn enclosing? [text]
(let [ast (cljify (paredit/parse text))
children (:children ast)]
(and (= 1 (count children))
(= "list" (:type (first children))))))
I think the pattern I follow might be that forms that are often one-liners get zero lines between them and otherwise I opt for two. Maybe the formatter could use these rules:
- No empty lines between one-liner forms
- X (whatever we agree on) empty newlines between multi-line forms, as well as between one-liner forms and multi-line forms.
The Clojure Style guide says to use empty lines between forms (though doesn't explicitly say only one line), with the exception of grouping related defs together. I've asked for clarification in https://github.com/bbatsov/clojure-style-guide/issues/170.
One formatting rule I could see is:
- Remove multiple newlines between each form leaving only one (or two if that's what's picked)
- For any forms which don't have newlines between them, leave them as-is.
While I do see some code using two lines between each form (including the internal style guide at work), the most common idiom I see in the community is to have a single line between each form. cljfmt also has :remove-consecutive-blank-lines? to preserve just a single line between forms.
Personally, I like to add whitespace between certain forms when I'm beginning a new section of code. Something like:
;;
;; Section 1
;;
(def foo ...)
(def bar ...)
;;
;; Section 2
;;
(def asdf ...)
(def jkl ...)
That and after really long functions, breaking up the file via white space can be nice.