Introducing Standard Clojure Style
tl;dr
- there is a new Clojure source code formatter
- it has no config options and follows simple formatting rules
- it is very fast
- written in ~4,000 lines of JavaScript with no dependencies
- it is stable enough for most projects
- try it out and report bugs!
Background and Context
Back in 2018 Daniel Compton created a Clojureverse thread proposing to build a common formatter for Clojure code similar to gofmt or JavaScript’s prettier. This thread generated a lot of useful discussion, but ultimately no single tool was created.
For the last year or so I have been working on Standard Clojure Style, which is my take on this problem. You can read more about the genesis of the project at Issue #1.
Current Status: ready for most projects
As of v0.11.0, I think the library is stable enough for most Clojure projects. If you have node.js installed on your system you can try it out with the following commands:
# go to a Clojure project directory
cd ~/my-clojure-project
# check out a clean git branch so you can revert any changes made by the tool
git checkout -b testing-standard-clj
# check your files
# NOTE: your directory names may be different, please adjust accordingly
npx @chrisoakman/standard-clojure-style check src-clj/ src-cljs/ test/
# format your files
npx @chrisoakman/standard-clojure-style fix src-clj/ src-cljs/ test/
# review the changes and report any bugs upstream
Please see the project README
for more details about CLI options, installation, etc.
It would be helpful to run it on as many codebases as possible to collect bugs, edge cases, etc. Please create an issue on the repo if you run into something that breaks your codebase.
Many thanks to several volunteers who have already done this and reported bugs
Performance is a feature
Standard Clojure Style is fast. Some quick benchmarking on my 2023 MacBook Pro M3:
- chessboard2 src-cljs/ - 36 files in 85.39ms
- Metabase src/ - 688 files in 1774.31ms
- Portal src/ - 120 files in 339.25ms
Results will vary of course, but it is doubtful that Standard Clojure Style will be a slow point in your development tooling.
Goals and Next Steps
It is not a goal of Standard Clojure Style to be “the one style that everyone has to use”.
Rather, the goal is to encourage community adoption of a common code style by offering easy-to-use tooling that works well enough and gets out of your way.
If you are currently using zprint, cljfmt, or another tool and are happy with the result, then Standard Clojure Style may not offer a compelling reason to change.
If you are currently using nothing, or starting a new project, then consider adopting Standard Clojure Style and see how it works for you / your team.
The initial implementation is written in JavaScript, but the code is written in such a way that it is designed to be easily portable between programming languages. This is inspired by how Parinfer works (see parinfer.py, parinfer-lua, etc), and I hope to port the Standard Clojure Style algorithm to multiple languages in the future once the implementation is stable.
The long-term goal of Standard Clojure Style is to “meet you where you are”: as a CLI tool, on your continuous integration build, in your editor, on the web, etc.
Upcoming Conj 2024 talk
I will be giving a lightning talk about Standard Clojure Style this Thursday at Clojure/conj 2024. Please come say hello if you will be at the Conj
Thanks for reading!
Chris Oakman