Can Codeq 2 solve Clojure’s weaknesses?

adrian smith
3 min readFeb 23, 2020

--

A few days ago there was a thread on Hacker News where the comments were discussing Clojure

There are several valid comments there about refactoring in Clojure being risky in large codebases and tooling as a hurdle because Clojure demands more from its editors than most languages

If we consider heavy tooling and risky refactors the weaknesses of Clojure for the purposes of this article we can move on to look at:

The Unison language

The Unison language which is a relatively new language, if you haven’t heard of it I recommend watching this video:

The idea of hash addressable code

For those of you who didn’t watch that video, it shows off the idea that if you can identify code fragments by a hash of the source code then you can have an immutable infrastructure for talking about code in a global way

Why is an identifier for code important? Well if you have an identifier you can store it in a database, you can associate additional information to it, the code becomes tractable and we can easily communicate it over the wire, As it turns out that has a lot of benefits the same way the simple idea of immutability gives a lot of cool features for Datomic

Codeq? What?

So Codeq (pronounced “co-deck”) is a Rich Hickey thing: https://github.com/Datomic/codeq from the far-flung past of 2014 it is the idea that code goes into git and from there it can be semantically stored in a Datomic database as a set of codeqs, from there you could programmatically ask question on your codebase past and current using a query language (better explanation)

That’s cool but why would you want that? What practical benefits could we derive? We can answer this question by looking at Unison’s benefits since they are also storing information on the code base past and current which is a less powerful idea than having a full query language..

Unison (Clojure?) Benefits

Fearless refactoring

So imagine a running REPL where when you redefine a function in a breaking way to improve it and REPL will add a new hash but the rest of the system still sees the old hash version of the function, the REPL can tell you where you need to look in order for all references of your function to be upgraded to the latest version, by first checking if your change was breaking (Rich Hickey has a good talk on this) and then by scanning the code base for code that references to the old hash

Knowing about locations of breakage is very re-assuring from a feedback point of view and one that should hopefully instil more confidence, check the Unison video to see how this happens there

Taking responsibility away from IDEs

Ideally, we should be able to use text editors as lightweight as something like Sublime Text, let’s consider something like renaming a function, in unison you use the unison “code database” to find and replace all hashes to the function a code database could be used for things like searching text, symbol finding, refactoring, static analysis etc that would hopefully allow someone to use something as basic as Sublime Text for intensive work

Fix problems on a global scale

If a lot of Codeq databases were available by open source projects a central service could make it possible to recommend code enhancements by scanning other codebases for similar hashes and then returning the hashes that those code fragments commonly get upgraded to historically

Test caching

When you store the history of your codebase, you can tell what code changed since the last time you ran your unit tests which means some unit tests won’t need re-running (if they’re pure), which should speed up releases that depending on automated tests running

Open questions

There are tons of open questions for how this would work in practice in Clojure but there have been rumours that Rich has been working on Codeq 2, If Clojure was backed by Codeq 2 at development time we could perhaps get rid of some of Clojure’s greatest “weaknesses” by offloading responsibility for refactoring help, static analysis, security scanning and many others onto a code database

I don’t think anyone in the wider community knows what Rich has been working on for Codeq 2 but if Codeq is closer to the language & “easy” with a good feedback loop I think Clojure could have something more powerful than Unison

--

--