/ archive / Clojure_Style_Guide.md
Clojure_Style_Guide.md
 1  This documents lists a set of conventions used by the development team,
 2  refer to <https://github.com/bbatsov/clojure-style-guide> for anything
 3  not addressed in here.
 4  
 5  ## Variable naming
 6  
 7  ### Use of '
 8  
 9  Avoid using appending `'`(prime) to variables names, as this can be hard
10  to notice especially if visually impaired.
11  
12  ``` clojure
13  
14    (defn do-stuff [thing]
15      (let [thing' (do-some-other-stuff thing)]))
16  ```
17  
18  Prefer threading, meaningful names or when not possible more readable
19  variable names.
20  
21  ## Imported namespaces
22  
23  ### Use of `:as` and `:refer`
24  
25  Avoid using `:refer`, use `:as` instead
26  
27  ### Namespaces aliases
28  
29  Use dot separated names when ambiguous, for example:
30  `status.ui.utils.clocks :as utils.clocks` as opposed to `:as clocks` or
31  `:as utils-clocks`
32  
33  ## Re-frame
34  
35  ### Prefer function composition instead of multiple dispatches
36  
37  Prefer using function composition instead of dispatching when possible.
38  You can leverage the `merge-fx` macro, which assumes the function will
39  take `cofx` as the last positional parameter and returns a map of `fxs`
40  (or `nil`) which will be merged back.
41  
42  In practice, functions/handlers should look like:
43  
44  ``` clojure
45  
46    (defn do-stuff [cofx]
47      (merge-fx cofx
48        db-fn
49        some-other-fx-fn))
50  ```
51  
52  Opposed to:
53  
54  ``` clojure
55  
56  (defn do-stuff [fx]
57      (-> fx
58        (update  :db db-fn)
59        (merge {:dispatch [:some-other-fx]})
60  ```
61  
62  Therefore when writing new functions:
63  
64  1\) Have them return a map of `fxs`.
65  
66  2\) Have `cofx` as the last parameter, in order to be able for others to
67  use them with `merge-fx`.
68  
69  3\) Operate on `cofx` rather than `db`, so that new functions can be
70  threaded.
71  
72  You can read <https://github.com/status-im/ideas/issues/31> for a
73  rationale.