Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cond with large clauses

Tags:

clojure

What is the recommended method formatting large clauses inside a cond statement in Clojure?

Ex:

(cond
 (> (large-function large-arg1
                    large-arg2
                    large-arg3)
    long-var-name))
 (->> (iterate #(* % 6) 1)
      (take 10)
      (apply +))
 (> (large-function large-arg4
                    large-arg5
                    large-arg6)
    long-var-name))
 (->> (iterate #(* % 6) 1)
      (take 10)
      (apply +))
 (> (large-function large-arg7
                    large-arg8
                    large-arg9)
    long-var-name))
 (->> (iterate #(* % 6) 1)
      (take 10)
      (apply +)))

If the result is on the same line as the condition, the result ends up way too indented, but if the result and the condition are different lines, it becomes too easy to lose track of conditions vs results. This seems like a situation where Common Lisp style cond would be useful, but adding the parentheses (probably brackets in Clojure's case) doesn't seem like the recommended course.

like image 826
Retief Avatar asked Feb 01 '12 23:02

Retief


1 Answers

In many cases questions like this default to "format it the way emacs does". Not that I inherently support this philosophy. It is ultimatly up to your sense of aesthetics

in short conds i like:

(cond
  (clause1) (action)
  (clause2) (action)

for really long ones like yours I like to add some extra newlines as visual delimiters:

(cond
   (clause)
   (action)

   (clause2)
   (action2)

so i would format your code:

(cond
 (> (large-function large-arg1
                    large-arg2
                    large-arg3)
    long-var-name)
 (->> (iterate #(* % 6) 1)
      (take 10)
      (apply +))

 (> (large-function large-arg4
                    large-arg5
                    large-arg6)
    long-var-name)
 (->> (iterate #(* % 6) 1)
      (take 10)
      (apply +))

 (> (large-function large-arg7
                    large-arg8
                    large-arg9)
    long-var-name)
 (->> (iterate #(* % 6) 1)
      (take 10)
      (apply +)))
like image 165
Arthur Ulfeldt Avatar answered Nov 13 '22 04:11

Arthur Ulfeldt