Skip to content

Commit a4fdbac

Browse files
authored
Merge pull request #270 from bbatsov/add-functions-section-examples
Add examples and rationale to Functions and Idioms rules
2 parents e706f98 + 80c6ef9 commit a4fdbac

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

README.adoc

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,11 +1236,25 @@ arities N > K provide a fold of the K arity over varargs.
12361236
=== Function Length [[function-length]]
12371237

12381238
Avoid functions longer than 10 LOC (lines of code). Ideally, most
1239-
functions will be shorter than 5 LOC.
1239+
functions will be shorter than 5 LOC. Short functions are easier to
1240+
understand, test, and compose.
12401241

12411242
=== Function Positional Parameters Limit [[function-positional-parameter-limit]]
12421243

12431244
Avoid parameter lists with more than three or four positional parameters.
1245+
Long parameter lists are hard to remember and easy to get wrong at the
1246+
call site. Prefer a map of options instead.
1247+
1248+
[source,clojure]
1249+
----
1250+
;; good
1251+
(defn create-user [{:keys [name email role admin?]}]
1252+
...)
1253+
1254+
;; bad - callers must remember the exact order
1255+
(defn create-user [name email role admin?]
1256+
...)
1257+
----
12441258

12451259
=== Pre and Post Conditions [[pre-post-conditions]]
12461260

@@ -1268,19 +1282,58 @@ Avoid the use of namespace-manipulating functions like `require` and
12681282
`refer`. They are entirely unnecessary outside of a REPL
12691283
environment.
12701284

1285+
[source,clojure]
1286+
----
1287+
;; good - use the ns form
1288+
(ns my-app.core
1289+
(:require [clojure.string :as str]))
1290+
1291+
;; bad - calling require as a function in source files
1292+
(require '[clojure.string :as str])
1293+
----
1294+
12711295
=== Forward References [[forward-references]]
12721296

1273-
Avoid forward references. They are occasionally necessary, but such occasions
1274-
are rare in practice.
1297+
Avoid forward references. They are occasionally necessary, but such occasions
1298+
are rare in practice. Forward references make code harder to follow
1299+
top-down and can mask circular dependencies.
12751300

12761301
=== Declare [[declare]]
12771302

12781303
Use `declare` to enable forward references when forward references are
12791304
necessary.
12801305

1306+
[source,clojure]
1307+
----
1308+
;; good - when a forward reference is truly needed, use declare
1309+
(declare process-children)
1310+
1311+
(defn process-node [node]
1312+
(when (:children node)
1313+
(process-children node)))
1314+
1315+
(defn process-children [node]
1316+
(mapv process-node (:children node)))
1317+
----
1318+
12811319
=== Higher-order Functions [[higher-order-fns]]
12821320

12831321
Prefer higher-order functions like `map` to `loop/recur`.
1322+
Higher-order functions are more declarative — they express *what* you
1323+
want, not *how* to iterate — and compose naturally with the rest of
1324+
the sequence library.
1325+
1326+
[source,clojure]
1327+
----
1328+
;; good
1329+
(map inc [1 2 3 4 5])
1330+
1331+
;; bad - manual loop to do the same thing
1332+
(loop [xs [1 2 3 4 5] result []]
1333+
(if (empty? xs)
1334+
result
1335+
(recur (rest xs) (conj result (inc (first xs))))))
1336+
----
12841337

12851338
=== Vars Inside Functions [[dont-def-vars-inside-fns]]
12861339

0 commit comments

Comments
 (0)