Liking cljdoc? Tell your friends :D

Style

style/apply-str

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Check for round-about clojure.string/join.

Examples

; avoid
(apply str x)

; prefer
(clojure.string/join x)

style/apply-str-interpose

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Check for round-about clojure.string/join.

Examples

; avoid
(apply str (interpose "," x))

; prefer
(clojure.string/join "," x)

style/apply-str-reverse

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Check for round-about clojure.string/reverse.

Examples

; avoid
(apply str (reverse x))

; prefer
(clojure.string/reverse x)

style/assoc-assoc

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Layering assoc calls are hard to read. assoc-in is known and idiomatic.

Examples

; avoid
(assoc coll :key1 (assoc (:key2 coll) :key2 new-val))
(assoc coll :key1 (assoc (coll :key2) :key2 new-val))
(assoc coll :key1 (assoc (get coll :key2) :key2 new-val))

; prefer
(assoc-in coll [:key1 :key2] new-val)

style/cond-else

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.10.1.85

It's nice when the default branch is consistent.

Examples

; avoid
(cond
  (< 10 num) (println 10)
  (< 5 num) (println 5)
  true (println 0))

; prefer
(cond
  (< 10 num) (println 10)
  (< 5 num) (println 5)
  :else (println 0))

Reference


style/conj-vector

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

vector is succinct and meaningful.

Examples

; avoid
(conj [] :a b {:c 1})

; prefer
(vector :a b {:c 1})

style/def-fn

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue1.01.19.0

(defn []) is preferable over (def (fn [])). Extrapolate to closures too.

Examples

; avoid
(def check-inclusion
  (let [allowed #{:a :b :c}]
    (fn [i] (contains? allowed i))))

; prefer
(let [allowed #{:a :b :c}]
  (defn check-inclusion [i]
    (contains? allowed i)))

; avoid
(def some-func
  (fn [i] (+ i 100)))

; prefer
(defn some-func [i]
  (+ i 100))

style/eq-false

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

false? exists so use it.

Examples

; avoid
(= false x)
(= x false)

; prefer
(false? x)

style/eq-nil

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

nil? exists so use it.

Examples

; avoid
(= nil x)
(= x nil)

; prefer
(nil? x)

style/eq-true

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

true? exists so use it.

Examples

; avoid
(= true x)
(= x true)

; prefer
(true? x)

style/eq-zero

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

zero? exists so use it.

Examples

; avoid
(= 0 num)
(= num 0)
(== 0 num)
(== num 0)

; prefer
(zero? num)

style/filter-complement

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Check for (filter (complement pred) coll).

Examples

; avoid
(filter (complement even?) coll)

; prefer
(remove even? coll)

style/filter-vec-filterv

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

filterv is preferable for using transients.

Examples

; avoid
(vec (filter pred coll))

; prefer
(filterv pred coll)

style/first-first

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

ffirst is succinct and meaningful.

Examples

; avoid
(first (first coll))

; prefer
(ffirst coll)

style/first-next

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

fnext is succinct and meaningful.

Examples

; avoid
(first (next coll))

; prefer
(fnext coll)

style/is-eq-order

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue1.15.01.18.0

clojure.test/is expects =-based assertions to put the expected value first.

This rule uses two checks on the = call to determine if it should issue a diagnostic:

  • Is the first argument a symbol or an unquoted list? (A variable/local or a call.)
  • Is the second argument a nil, boolean, char, number, keyword, or string?

Examples

; avoid
(is (= status 200))
(is (= (my-plus 1 2) 3))

; prefer
(is (= 200 status))
(is (= 3 (my-plus 1 2)))

; non-issues
(is (= (hash-map :a 1) {:a 1}))
(is (= (hash-set :a 1) #{:a 1}))

style/let-do

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

let has an implicit do, so use it.

Examples

; avoid
(let [a 1 b 2] (do (println a) (println b)))

; prefer
(let [a 1 b 2] (println a) (println b))

style/mapcat-apply-apply

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Check for (apply concat (apply map x y)).

Examples

; avoid
(apply concat (apply map x y))

; prefer
(mapcat x y)

style/mapcat-concat-map

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Check for (apply concat (map x y z)).

Examples

; avoid
(apply concat (map x y))
(apply concat (map x y z))

; prefer
(mapcat x y)
(mapcat x y z)

style/minus-one

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Checks for simple -1 that should use clojure.core/dec.

Examples

; avoid
(- x 1)

; prefer
(dec x)

style/minus-zero

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Checks for x - 0.

Examples

; avoid
(- x 0)

; prefer
x

style/multiple-arity-order

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue1.3.01.3.0

Sort the arities of a function from fewest to most arguments.

Examples

; avoid
(defn foo
  ([x] (foo x 1))
  ([x y & more] (reduce foo (+ x y) more))
  ([x y] (+ x y)))

; prefer
(defn foo
  ([x] (foo x 1))
  ([x y] (+ x y))
  ([x y & more] (reduce foo (+ x y) more)))

Reference


style/multiply-by-one

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Checks for (* x 1).

Examples

; avoid
(* x 1)
(* 1 x)

; prefer
x

style/multiply-by-zero

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Checks for (* x 0).

Examples

; avoid
(* x 0)
(* 0 x)

; prefer
0

style/neg-checks

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

neg? exists so use it.

Examples

; avoid
(< num 0)
(> 0 num)

; prefer
(neg? num)

style/nested-addition

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Checks for simple nested additions.

Examples

; avoid
(+ x (+ y z))
(+ x (+ y z a))

; prefer
(+ x y z)
(+ x y z a)

style/nested-multiply

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Checks for simple nested multiply.

Examples

; avoid
(* x (* y z))
(* x (* y z a))

; prefer
(* x y z)
(* x y z a)

style/new-object

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.1.691.15.2

new special form is discouraged for dot usage.

NOTE: The style :method-value requires Clojure version 1.12+.

Examples

; avoid
(new java.util.ArrayList 100)

; prefer (chosen style :dot (default))
(java.util.ArrayList. 100)

; avoid (chosen style :method-value)
(java.util.ArrayList. 100)

; prefer (chosen style :method-value)
(java.util.ArrayList/new 100)

Configurable Attributes

NameDefaultOptions
:chosen-style:dot:dot, :method-value

style/next-first

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

nfirst is succinct and meaningful.

Examples

; avoid
(next (first coll))

; prefer
(nfirst coll)

style/next-next

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

nnext is succinct and meaningful.

Examples

; avoid
(next (next coll))

; prefer
(nnext coll)

style/not-eq

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

not= exists, so use it.

Examples

; avoid
(not (= num1 num2))

; prefer
(not= num1 num2)

Reference


style/not-nil?

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

some? exists so use it.

Examples

; avoid
(not (nil? x))

; prefer
(some? x)

style/not-some-pred

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

not-any? is succinct and meaningful.

Examples

; avoid
(not (some even? coll))

; prefer
(not-any? even? coll)

style/plus-one

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Checks for simple +1 that should use clojure.core/inc.

Examples

; avoid
(+ x 1)
(+ 1 x)

; prefer
(inc x)

style/plus-zero

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Checks for x + 0.

Examples

; avoid
(+ x 0)
(+ 0 x)

; prefer
x

style/pos-checks

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

pos? exists so use it.

Examples

; avoid
(< 0 num)
(> num 0)

; prefer
(pos? num)

style/prefer-boolean

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.1.690.1.69

Use boolean if you must return true or false from an expression.

Examples

; avoid
(if some-val true false)
(if (some-func) true false)

; prefer
(boolean some-val)
(boolean (some-func))

Reference


style/prefer-clj-math

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruefalse0.1.690.1.69

NOTE: Requires Clojure version 1.11.0.

Prefer clojure.math to interop.

Examples

; avoid
Math/PI
(Math/atan 45)

; prefer
clojure.math/PI
(clojure.math/atan 45)

Reference


style/prefer-clj-string

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue1.9.01.9.0

Prefer clojure.string to interop.

methodclojure.string
.containsclojure.string/includes?
.endsWithclojure.string/ends-with?
.replaceclojure.string/replace
.splitclojure.string/split
.startsWithclojure.string/starts-with?
.toLowerCaseclojure.string/lower-case
.toUpperCaseclojure.string/upper-case
.trimclojure.string/trim

Examples

; avoid
(.toUpperCase "hello world")

; prefer
(clojure.string/upper-case "hello world")

Reference


style/prefer-condp

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truefalsefalse0.1.691.0

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:

  • The cond is well-formed (aka even number of args).
  • The cond has more than 1 pair.
  • The first test-expr is a list with 3 forms.
  • The function of every test-expr must match the test-expr of the first test-expr.
  • The last test-expr isn't checked if it is true or a keyword.
  • The last argument of every test-expr must match the last argument of the first test-expr.

Provided all of that is true, then the middle arguments of the test-exprs are gathered and rendered into a condp.

Safety

It's possible that the check isn't written correctly, so be wary of the suggested output.

Examples

; avoid
(cond
  (= 1 x) :one
  (= 2 x) :two
  (= 3 x) :three
  (= 4 x) :four)

; prefer
(condp = x
  1 :one
  2 :two
  3 :three
  4 :four)

; avoid
(cond
  (= 1 x) :one
  (= 2 x) :two
  (= 3 x) :three
  :else :big)

; prefer
(condp = x
  1 :one
  2 :two
  3 :three
  :big)

Reference


style/prefer-for-with-literals

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue1.15.01.17.0

The core builder functions are helpful when creating an object from an opaque sequence, but are much less readable when used in maps to get around issues with anonymous function syntax peculiarities.

Examples

; avoid
(map #(hash-map :a 1 :b %) (range 10))

; prefer
(for [item (range 10)] {:a 1 :b item})

style/prefer-vary-meta

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.10.1.69

vary-meta works like swap!, so no need to access and overwrite in two steps.

Examples

; avoid
(with-meta x (assoc (meta x) :filename filename))

; prefer
(vary-meta x assoc :filename filename)

style/reduce-str

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue1.111.11

reduce calls the provided function on every element in the provided collection. Because of how str is implemented, a new string is created every time it's called. Better to rely on clojure.string/join's efficient StringBuilder and collection traversal.

Additionally, the 2-arity form of reduce returns the first item without calling str on it if it only has one item total, which is generally not what is expected when calling str on something.

Examples

; avoid
(reduce str x)
(reduce str "" x)

; prefer
(clojure.string/join x)

style/redundant-let

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.1.690.1.69

Directly nested lets can be merged into a single let block.

Examples

; avoid
(let [a 1]
  (let [b 2]
    (println a b)))

(let [a 1
      b 2]
  (println a b))

style/redundant-nested-call

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruefalse1.19.01.19.0

Some clojure.core functions and macros take a variable number of args, so there's no need to nest calls.

note

This can have performance implications in certain hot-loops.

Examples

; avoid
(+ 1 2 (+ 3 4))
(comp :foo :bar (comp :qux :ply))

; prefer
(+ 1 2 3 4)
(comp :foo :bar :qux :ply)

style/redundant-regex-constructor

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue1.10.01.10.0

Clojure regex literals (#"") are passed to java.util.regex.Pattern/compile at read time. re-pattern checks if the given arg is a Pattern, making it a no-op when given a regex literal.

Examples

; avoid
(re-pattern #".*")

; prefer
#".*"

style/set-literal-as-fn

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
falsetruetrue0.1.1191.11

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.

Examples

; avoid
(#{'a 'b 'c} elem)

; prefer
(case elem (a b c) elem nil)

style/single-key-in

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.1.690.1.69

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.

Examples

; avoid
(assoc-in coll [:k] 10)

; prefer
(assoc coll :k 10)

style/tostring

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Convert (.toString) to (str).

Examples

; avoid
(.toString x)

; prefer
(str x)

style/trivial-for

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue1.111.11

for is a complex and weighty macro. When simply applying a function to each element, better to rely on other built-ins.

Examples

; avoid
(for [item items]
  (f item))

; prefer
(map f items)

Reference


style/update-in-assoc

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

update-in-ing an assoc with the same key are hard to read. assoc-in is known and idiomatic.

Examples

; avoid
(update-in coll [:a :b] assoc 5)

; prefer
(assoc-in coll [:a :b] 5)

style/useless-do

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.1.691.2.0

A single item in a do is a no-op. However, it is sometimes necessary to wrap expressions in dos to avoid issues, so do surrounding ~@something will be skipped as well as #(do something).

Examples

; avoid
(do coll)

; prefer
coll

; skipped
(do ~@body)
#(do [%1 %2])

style/when-do

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.1.691.2.0

when already defines an implicit do. Rely on it.

Examples

; avoid
(when x (do (println :a) (println :b) :c))

; prefer
(when x (println :a) (println :b) :c)

style/when-not-call

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

when-not exists so use it lol.

Examples

; avoid
(when (not x) :a :b :c)

; prefer
(when-not x :a :b :c)

style/when-not-do

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

when-not already defines an implicit do. Rely on it.

Examples

; avoid
(when-not x (do (println :a) (println :b) :c))

; prefer
(when-not x (println :a) (println :b) :c)

style/when-not-empty?

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.10.1.69

seq returns nil when given an empty collection. empty? is implemented as (not (seq coll)) so it's best and fastest to use seq directly.

Examples

; avoid
(when-not (empty? ?x) &&. ?y)

; prefer
(when (seq ?x) &&. ?y)

style/when-not-not

Enabled by defaultSafeAutocorrectVersion AddedVersion Updated
truetruetrue0.11.2.0

Two nots cancel each other out.

Examples

; avoid
(when-not (not x) y z)

; prefer
(when x y z)

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close