mirror of
https://github.com/penpot/penpot.git
synced 2026-11-25 20:30:35 +00:00
3.0 KiB
3.0 KiB
Clojure Core
How to shape a function in this codebase. Each rule came from a review round; do not re-derive from taste.
- Preconditions belong to the boundary, not to the core. A function documents what it needs (hints, docstring) and assumes it. Absence checks live where the value enters the system: the caller that resolves optional objects,
ig/init-key, the request handler — includingnilthat is optional-by-design (when-let [cs (some-> server ...)]at the entry point, as withConnectorStatisticswhen the server option is off). A(when (and (some? a) (some? b)) ...)inside a leaf function means the guard is in the wrong place — if you cannot getaorb, you should not be calling it. - A static set of operations is written statically. With a fixed, small set (four metrics) write the four calls. Do not build a collection and iterate it (
doseqover a literal vector): it allocates, adds indirection and hides each operation. Use a collection when the set is dynamic (config, registry, input). - Default to no
!; existing!is legacy being removed gradually. Do not introduce new!names except the allowed cases documented inmem:clojure/idioms(add and justify the special case there first). Allowed: mutating public fns inapp.db(insert!,update!,delete!,run!,tx-run!, ...) and fns whose contract is to work over transients (stdlibconj!/assoc!/dissoc!/persistent!and custom helpers over them). Existing!elsewhere (e.g.emit!,send!,submit!,validate-file!) stays until its refactor; keep a family consistent in the meantime. Reporting helpers (log, sample-and-publish) never take!: the mutation happens in the!API they call. - Functions that obtain data start with
get-orfetch-. A reader must see at a glance that the function reads (get-file,get-snapshot,get-file-json); a bare noun hides it (file-jsonsays nothing about reading). New helpers that fetch by id follow this, never acurrent-prefix: the id parameter already says which one it is. ?marks a predicate, not a data prop. A var or function that answers a question takes?(export-error?,(defn editable? …)); a map key that names a property does not (:export-error, never:export-error?), even when its value is a boolean. Existing?keys are legacy to remove gradually; do not add a new one.- Do not shape production code for tests. No return values added just to assert them; assert on the observable effect. When preconditions move, move the tests with them.
- All code-facing text MUST be in English — identifiers, comments, docstrings, test descriptions. No exceptions; see the hard rule in
AGENTS.md.
Reference implementation: the http metrics samplers in app.http (sample-worker-metrics / sample-connector-metrics are guard-free; sample-http-metrics guards at the boundary).
See also: mem:clojure/idioms (language behaviors and naming exceptions), mem:clojure/logging (structured logging), mem:testing (TDD and test conventions).