Enabled | Added |
---|---|
true | 0.1 |
It's nice when the default branch is consistent.
; bad
(cond
(< 10 num) (println 10)
(< 5 num) (println 5)
true (println 0))
; good
(cond
(< 10 num) (println 10)
(< 5 num) (println 5)
:else (println 0))
Enabled | Added |
---|---|
true | 0.1 |
(defn [])
is preferable over (def (fn []))
. Extrapolate to closures.
# bad
(def check-inclusion
(let [allowed #{:a :b :c}]
(fn [i] (contains? allowed i))))
# good
(let [allowed #{:a :b :c}]
(defn check-inclusion [i]
(contains? allowed i)))
# bad
(def some-func
(fn [i] (+ i 100)))
# good
(defn some-func [i]
(+ i 100))
Enabled | Added |
---|---|
true | 0.1 |
new
is discouraged for dot usage.
; bad
(new java.util.ArrayList 100)
; good
(java.util.ArrayList. 100)
Enabled | Added |
---|---|
true | 0.1 |
Use boolean
if you must return true
or false
from an expression.
# bad
(if some-val true false)
(if (some-func) true false)
# good
(boolean some-val)
(boolean (some-func))
Enabled | Added |
---|---|
true | 0.1 |
Prefer clojure.math to interop.
# bad
Math/PI
(Math/atan 45)
# good
clojure.math/PI
(clojure.math/atan 45)
Enabled | Added |
---|---|
true | 0.1 |
cond
checking against the same value in every branch is a code smell.
This rule uses the first test-expr as the template to compare against each other test-expr. It has a number of conditions it checks as it runs:
cond
is well-formed (aka even number of args).cond
has more than 1 pair.true
or a keyword.Provided all of that is true, then the middle arguments of the test-exprs are
gathered and rendered into a condp
.
# bad
(cond
(= 1 x) :one
(= 2 x) :two
(= 3 x) :three
(= 4 x) :four)
# good
(condp = x
1 :one
2 :two
3 :three
4 :four)
# bad
(cond
(= 1 x) :one
(= 2 x) :two
(= 3 x) :three
:else :big)
# good
(condp = x
1 :one
2 :two
3 :three
:big)
Enabled | Added |
---|---|
true | 0.1 |
vary-meta
works like swap!, so no need to access and overwrite in two steps.
; bad
(with-meta x (assoc (meta x) :filename filename))
; good
(vary-meta x assoc :filename filename)
Enabled | Added |
---|---|
true | 0.1 |
Directly nested lets can be merged into a single let block.
# bad
(let [a 1]
(let [b 2]
(println a b)))
(let [a 1
b 2]
(println a b))
Enabled | Added |
---|---|
true | 0.1 |
Sets can be used as functions and they're converted to static items when they contain constants, making them fairly fast. However, they're not as fast as [[case]] and their meaning is less clear at first glance.
# bad
(#{'a 'b 'c} elem)
# good
(case elem (a b c) elem nil)
Enabled | Added |
---|---|
true | 0.1 |
assoc-in
loops over the args, calling assoc
for each key. If given a single key,
just call assoc
directly instead for performance and readability improvements.
; bad
(assoc-in coll [:k] 10)
; good
(assoc coll :k 10)
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close