Clojure style and idiom — use when writing or reviewing Clojure code in any dialect, choosing between idiomatic forms, or judging layout, naming, threading, and destructuring.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Clojure Style?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/just-sultanov-clojure-style)
---
name: clojure-style
description: Clojure style and idiom — use when writing or reviewing Clojure code in any dialect, choosing between idiomatic forms, or judging layout, naming, threading, and destructuring.
license: CC-BY-3.0
---
# Clojure Style
Apply the community Clojure style guide to code you write or review. The
canonical guide is <https://guide.clojure.style>; this skill turns it into a
pass over a file or diff. The text is adapted from
[bbatsov/clojure-style-guide](https://github.com/bbatsov/clojure-style-guide)
under CC BY 3.0; see [NOTICE.md](NOTICE.md).
This skill covers the baseline that holds on every supported release. The Java
interop added in Clojure 1.12 and the map destructuring added in 1.13 alpha
live in `clojure-versions`; apply this skill's baseline interop rules and take
the new forms from there.
## Ground rules
- **Match the code around you first.** Project convention beats this guide;
when neither applies, use this guide. When a file already deviates
consistently, follow the file and mention the deviation rather than
reformatting it.
- **Touch only what the task needs.** Reformatting an unrelated region hides
the real change. A whole-file format belongs in its own commit.
- **Replace the old form.** Do not keep the previous version commented out
beside the new one.
## Dialects
These rules target Clojure on the JVM. They apply to Babashka,
ClojureScript, ClojureCLR, ClojureDart, Clojerl, jank, let-go, Basilisp, and
older cljx code, except where a rule names Java interop or a JVM-only
namespace. Read [dialects.md](references/dialects.md) before applying the
interop, math, `ns`, or state rules to a non-JVM file.
## Workflow
1. **Read the project conventions.** Check `.cljfmt.edn`,
`.clj-kondo/config.edn`, `.editorconfig`, and a neighbouring namespace.
Identify the dialect from the file extension, then note the line width,
alias style, and whether the project uses semantic indentation.
Done when you can state the project's dialect, width, indent, and alias
conventions.
2. **Shape the code.** Apply the layout rules below to every form you add or
change. Let `cljfmt` settle mechanical details when the project has it.
Done when new forms are indistinguishable in layout from their neighbours.
3. **Name by role.** Classify every new public name with the naming table.
Done when each name follows its row in the table and each `?`, `!`, `*`,
`->`, or `_` carries its documented meaning.
4. **Choose the idiomatic form.** Scan the replacement table and apply a row
only when its precondition holds. Open the linked reference when a rule
needs its rationale or an edge case.
Done when no applicable row of the table remains.
5. **Verify.** Run the project's formatter and linter when available, then walk
the review checklist.
Done when both tools report nothing new and every checklist item is
accounted for.
## Layout essentials
- 80 columns where feasible, never beyond 120.
- Two spaces indent the body of a form. Arguments align with the first
argument; use a single space when the first line has no arguments.
```clojure
;; good
(filter even?
(range 1 10))
(filter
even?
(range 1 10))
(when something
(something-else))
```
- Align `let` bindings and map keys vertically; a single space between key and
value is also acceptable.
- Gather trailing parentheses on the last line. A rich `(comment ...)` block
may keep its closing paren on its own line.
- One blank line between top-level forms; group related `def`s without blank
lines. No blank lines inside a definition, except to separate `cond` pairs.
- Spaces, never tabs. Unix newlines. Final newline. No trailing whitespace.
- One namespace per file.
- `defn`: docstring after the name, then the argument vector, then the body.
Multi-arity functions align each arity and order them from fewest to most
arguments.
- Comments use `;;;;` for headings, `;;;` for top-level notes, `;;` for a code
fragment, and `;` for a margin note. Put one space after the semicolons.
- Public vars get docstrings; the first line is a complete, capitalised
sentence.
## Naming
| Construct | Rule | Example |
| ------------------------------------- | -------------------------------------- | -------------------------------- |
| Vars, functions, arguments, constants | `lisp-case`, no sigils for constants | `max-size`, `save-user` |
| Protocols, records, types | `CapitalCase`; keep acronyms uppercase | `XMLParser`, `LdapURIConnection` |
| Predicate returning a boolean | trailing `?` | `palindrome?` |
| Side-effecting or STM-unsafe function | trailing `!` | `save-user!` |
| Dynamic var | `*earmuffs*` | `*out*` |
| Conversion function | `->` | `f->c` |
| Unused binding | `_` or `_name` | `[_ _ c]` |
| Namespace | `project.module`; `lisp-case` segments | `my.app.http-server` |
## Replacement table
| Instead of | Write | Precondition |
| ------------------------------------ | ----------------------------------- | ----------------------------------------------------------- |
| `(if p (do a b))` | `(when p a b)` | no else branch |
| `(if-not p (do a b))` | `(when-not p a b)` | no else branch |
| `(if (not p) a b)` | `(if-not p a b)` | — |
| `(when (not p) a)` | `(when-not p a)` | — |
| `(let [x e] (if x a b))` | `(if-let [x e] a b)` | `x` is not used in `b` |
| `(let [x e] (when x a))` | `(when-let [x e] a)` | — |
| `(if p true false)` | `(boolean p)` | — |
| `(not (= a b))` | `(not= a b)` | — |
| `(+ x 1)`, `(- x 1)` | `(inc x)`, `(dec x)` | `x` is a number |
| `(> x 0)`, `(< x 0)`, `(= x 0)` | `(pos? x)`, `(neg? x)`, `(zero? x)` | `x` is a number; `zero?` throws on `nil` |
| `(and (> x lo) (< x hi))` | `(< lo x hi)` | all operands are numbers |
| `(print (format ...))` | `(printf ...)` | — |
| `(into [] xs)` | `(vec xs)` | — |
| `(get m :k)` | `(:k m)`; never `(m :k)` | — |
| `(filter #(pred? %) xs)` | `(filter pred? xs)` | — |
| `(cons a (cons b xs))` | `(list* a b xs)` | — |
| `(vector 1 2 3)`, `(hash-set 1 2 3)` | `[1 2 3]`, `#{1 2 3}` | — |
| `'(1 2 3)` used as data | `[1 2 3]` | the value is data, not a code list |
| `(nth xs 0)` for destructuring | `(let [[x] xs] ...)` | `xs` is known non-empty, or `nil` is acceptable |
| `(cond ... true default)` | `(cond ... :else default)` | — |
| repeated `(= x c)` in `cond` | `condp`, or `case` for constants | `case` tests are unevaluated literals |
| `(-> x (f) (:k))` | `(-> x f :k)` | — |
| `(. obj method a)` | `(.method obj a)` | — |
| `(new Foo a)` | `(Foo. a)` | — |
| `(. Math pow a b)` | `(Math/pow a b)` | — |
| `(String/join ...)` | `(str/join ...)` | — |
| `(Math/pow ...)` | `(clojure.math/pow ...)` | Clojure 1.11+ |
| `(Foo. a b)` for a record | `(->Foo a b)` | the record's positional factory exists |
| `try`/`finally` to close a resource | `with-open` | the resource is `AutoCloseable` and closing is the only job |
| `(catch Throwable t ...)` | catch the specific exception class | you do not need to catch `Error` |
A rewrite that changes an exception into `nil`, turns a non-boolean into a
boolean, or picks a different numeric value is a behaviour change, not style;
apply the row only when its precondition holds. Rows naming Java classes,
`clojure.math`, or `with-open` are JVM guidance. On other dialects, use the
host's interop and error types; see [dialects.md](references/dialects.md).
## Checklist
- [ ] No tabs, trailing whitespace, or missing final newline.
- [ ] Lines fit the project limit; long forms wrap at argument boundaries.
- [ ] Bodies indent two spaces; arguments align; trailing parens gathered.
- [ ] `ns` uses sorted `:require`s, idiomatic aliases, and no `:use`.
- [ ] Names follow the suffix and sigil rules; unused bindings use `_`.
- [ ] Single-branch conditionals use `when`, `when-not`, `if-let`, or
`when-let`.
- [ ] Collections use literals and the right type; vectors hold sequences.
- [ ] No `def` inside a function, no shadowing of `clojure.core`, no `(m :k)`.
- [ ] Public vars are documented; docstrings are placed and indented correctly.
- [ ] Host interop and state constructs match the dialect; JVM-only forms are
not forced onto non-JVM code.
- [ ] `cljfmt` and `clj-kondo` report nothing new.
## Related skills
- `clojure-versions` — version gates, the 1.12 Java interop and 1.13 alpha
destructuring, and the upgrade procedure. Read it before applying a form
that a release added.
- `clojure-tooling` — installing and configuring cljfmt, clj-kondo,
clojure-lsp, and the format and lint steps of CI.
- `clojure-editing` — whole-form structural edits with the bundled `form`
CLI; the tool owns brackets, layout, and pair parity while the agent owns
the text.
- `clojure-testing` — test layout and naming, `deftest` shape, fixtures,
and the test runners; the Testing rules of the guide live there since
0.10.0.
## References
| File | Read when |
| --------------------------------------------- | ------------------------------------------------------------ |
| [layout.md](references/layout.md) | line width, indentation, argument and binding alignment |
| [whitespace.md](references/whitespace.md) | tabs, brackets, commas, line endings, blank lines, parens |
| [namespaces.md](references/namespaces.md) | writing an `ns` form, aliases, one file per namespace |
| [naming.md](references/naming.md) | naming a var, protocol, record, argument, or unused binding |
| [functions.md](references/functions.md) | shaping a `defn`, arities, fn literals, composition |
| [conditionals.md](references/conditionals.md) | `when`/`if-let`, `cond`, `condp`, `case`, booleans |
| [collections.md](references/collections.md) | vectors, maps, sets, destructuring, empty checks, transients |
| [state.md](references/state.md) | `def`/`declare`, vars, refs, agents, atoms |
| [threading.md](references/threading.md) | `->`/`->>` and optional parentheses |
| [strings-math.md](references/strings-math.md) | `clojure.string`, `clojure.math`, `inc`/`dec`, comparisons |
| [interop.md](references/interop.md) | Java interop: methods, fields, construction (JVM) |
| [exceptions.md](references/exceptions.md) | `ex-info`, `with-open`, catching |
| [records.md](references/records.md) | constructing records and types, custom constructors |
| [macros.md](references/macros.md) | writing or reviewing a macro |
| [docstrings.md](references/docstrings.md) | docstring placement, content, and formatting |
| [comments.md](references/comments.md) | comment levels, `#_`, `TODO`/`FIXME` annotations |
| [metadata.md](references/metadata.md) | metadata placement, `:added`, `:deprecated`, `:no-doc` |
| [dialects.md](references/dialects.md) | non-JVM file, unfamiliar extension, host interop or state |
Tool installation and configuration live in the `clojure-tooling` skill.