Why Clojure?
Tags: clojure, functional
Published: 2020-08-02 20:16:11

_The language is the compiler. What could go wrong?_

If you are braced for a hard sell of Clojure then be at ease. Instead I am going to document what interests me in the language. And why I am starting to use it in a hobby project.

# Ideas

I was initially attracted to Clojure because it is a Lisp that embraces functional programming. You can probably find a million articles about the benefits of functional programming (immutability and pure functions) so I won't rehash that. But why Lisp?

For me it is the malleability. The language is the compiler. I have the power to add new capabilities to the language for my problem space, without waiting on language developers. For example, other programming paradigms -- including object oriented and functional -- have been implemented on top of Lisp. This malleability wasn't a feature on the backlog that the language developers had to spend millions of man hours to code. It is due to the nature of Lisp code being just lists of data. I probably will not be adding any fundamentally new features to the language itself, but this also means that my everyday business code has a lot of capability at its disposal.

Lofty ideas only get me so far. I also need practical reasons to undertake such a time investment.

# Dynamism

For many years I have used statically typed languages. I like types. They bring order to the chaotic universe. But sometimes types can become a little tyrannical.

I usually write our architectural plumbing code, because I am old and I didn't repeat my first year's experience too many times and the team entrusts it to me. In this kind of code I use a "context" type. It keeps settings and operation details (the user, their organization, and so on). This chunk of data gets transformed multiple times (ex. before/after user auth) as it makes its way through the system. At some point, I realized that I was spending a lot of time doing Type Tetris rather than running the plumbing. And now I cannot unsee that, and I notice it in other places as well.

> There is a lot to be said for finding joy in solving the problem rather than finding joy in the tools used in solving the problem. As I get further in my career, I keep trending toward the first one.

# Precise types

_Hang on, but you just said..._

I like types. They are quite useful. For example, at the borders of a system to define data between systems. So I was put off by the idea of going to a completely dynamic language. Then in my research I ran across a fascinating article called [Typed Lisp, A Primer](https://alhassy.github.io/TypedLisp.html). I am not very familiar with Lisp, so some of it went over my head. But the main message I received was that types in Lisp are just functions which check qualities about the data.

That means I can opt into types. It also means that types can be far more precise than what we get with statically typed languages. For example, I can define a type that is not just an integer, but an integer in a specific range. And I can require incoming data to conform to that by declaration.

So I still get to have types where I want them and in a more precise form. And I can have dynamism where it benefits me.

> Clojure's typing library is called Spec.

# Conclusion

The laws of the universe dictate that there is no perfect tool, so I am confident that I will discover trade-offs in my journey through Clojure. But I find it incredibly useful to see problems from different perspectives. And I expect that learning Clojure will add some new lenses for me to look through.

I'll try to keep you posted on what I learn.