Sunday, July 26, 2009

Coming to Clojure

I tried to avoid it. I really did. I had bookmarked Rich Hickey's video, "Clojure for Lisp Programmers" and it sat there on my delicious list like a fresh packet of heroin on a junkie's dresser.

I didn't want to get immersed in a new language. About a year ago, I'd gone through a bad Haskell addiction and I had nothing to show for it but a stack of monad tutorials and a bunch of puzzle code. And I was busy: I had a job, after all, and a family and a billion activities. Plus I was busy working on a side project using some real technologies: Ruby on Rails and Facebook. Technologies that let you build deployable applications without building big teams.

But one fateful day in November, I opened that video. An hour later, it was pretty much all over. From watching Rich present Clojure, it was clear that he got it. He got both why people love Lisp and why people who love Lisp end up using Python, Haskell, Ruby, or even Java.

When Rich talked about Clojure, he talked about a elegant and coherent set of design decisions. I didn't really believe it, but I had to try it. Once I tried it, I was hooked. There'd be a lot more of those little packets on my dresser.

What Clojure is and why to use it has been widely discussed. And no one presents it better than Rich himself. But I feel compelled to write down those things that make it the language I want to use.

Clojure is a Lisp, first of all. It is very different from Common Lisp or Scheme, but it is clearly part of the family. But where Common Lisp feels like a classic 70's muscle car, powerful perhaps but filled with compromises from a bygone era, Clojure feels like what we expect from a high-end sport sedan in the 21st century: all fit and finish, every button in the right place, and zip when you need it.

Clojure has abandoned the primacy of the cons cell, which has dominated Lisps from the beginning, and built a model that gets leverage from sequences, but supports multiple first-class data types (lists, vectors, and maps) that can be entered as primitive data directly (as you'd expect in, say, modern languages like Python or Ruby).

Clojure makes functional natural. Lisp has always made functional programming seem like the right strategy, with first-order functions, map, apply and so on. Clojure takes this an important step forward with persistent data structures built right into the language. With these structures, the programmer doesn't feel like he needs to break the functional paradigm for efficiency reasons when, for example, updating a vector. The cost of creating a copy of a vector with a new value is a slot is so low as to seem free. (The functional theory is that the incremental cost of this copy is offset by the reduced bookkeeping, etc., that's required.)

Clojure is built on the JVM. In truth, this part has it's pros and cons. Being on the JVM means that Clojure is able to leverage wealth of libraries, gets excellent compiler technologies for free, and is instantly portable to most platforms. However, it also means programmers need to work with libraries based on the OO paradigm rather than the functional paradigm, some platforms end up being off-limits (can you say iPhone?), and the dreaded Java startup environment which comes with classpath hell and other features that mean it's hard to use Clojure as a scripting language the same way you'd use Python. The one factor that definitely puts JVM hosting into the plus column is the design of the Java interop functionality. This makes leveraging Java libraries incredibly easy and natural.

Clojure has community. One of the things that everyone who comes to Clojure notices is the feeling of community that exists among the Clojure programmers. Interaction in the various forums is helpful and respectful. People genuinely seem to appreciate each other. This is not an accident. Rich has been very careful is his own statements not to be competitive or incendiary (or to position Clojure above everything else) and he has followed that up with some mentoring (and occasional moderation) of the community. For me, this community spirit drives my desire to contribute to Clojure. I know this is a huge factor for other contributors as well.

Clojure has many other features (software transactional memory, agents, ...) which are nicely implemented and give you a lot of power, but these were the ones I wanted to highlight about my decision to make Clojure my first choice when I can choose a programming language.

I know Clojure is not the right thing for everybody (Lisp may not be your thing, the JVM may be a deal breaker, or whatever). We live during a period in which it's easy to choose from among many great options when it comes to programming environments.

But for myself and many others who have found Clojure, we feel like we've found a special sweet spot in the programming world.