Skip to content

Commit fcdd6c5

Browse files
authored
Added destructuring section to new-guidelines.md (#18731)
Adds a section explaining the use cases of destructuring function parameters to improve code style and reduce the chance of bugs.
1 parent 27e177f commit fcdd6c5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: src/quo/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ The convention is `:size-<number>`, e.g size `20` is `:size-20`
106106
- Try to make all other vars private because they should almost never be used
107107
directly.
108108

109+
## Default value when destructuring
110+
111+
Too often callers pass nil values because values can be wrapped in a `when` for example.
112+
In this case, the default value is not applied, because :or macro will use default only when the value is absent.
113+
Instead, use `(or (:value props) some-default-value)` in a `let` expression or as a parameter value.
114+
115+
```clojure
116+
;; bad (unreliable)
117+
(defn- view-internal
118+
[{:keys [auto-focus?
119+
init-value
120+
return-key-type]
121+
:or {auto-focus? false
122+
init-value 0
123+
return-key-type :done}}]
124+
...)
125+
126+
;; good
127+
(defn- view-internal
128+
[{:keys [theme size something] :as props}]
129+
(let [auto-focus? (or (:auto-focus? props) false)
130+
init-value (or (:init-value props) 0)
131+
return-key-type (or (:return-key-type props) :done)]
132+
...))
133+
```
134+
109135
## Component tests
110136

111137
We don't attempt to write component tests verifying how components look on the

0 commit comments

Comments
 (0)