One Night With Clojure Makes a Scala Guy Humble
I do most of my work these days in Scala, but I have long been curious about Clojure, partly because of the mythology around Lisp and it’s potential superior productivity and possibility of self-generating code (Clojure is a Lisp dialect).
I’ve been reading about Clojure from time to time, tried little snippets from time to time, but never made any serious efforts to get deep into the language. On Sunday I took the plunge in rewriting a small, but important payments application I have had running in production for a few years in different guises. This was the third incarnation of the app, the first one was in Java from 2008, second in Scala from 2009, and now this, I still have the codebase of all three apps, so I could contrast and compare the results.
A simple metric generated with cloc, namely Lines of Code really blew me away about Clojure:
- Java version: 755 lines of code (LOC)
- Scala version: 419 LOC’s*
- Clojure version: 57 LOC’s
Yup, that’s right, the Clojure version is more than 10 times less code, and about 7-8 times less than it’s Scala equivalent. In fairness though, the Scala number is a bit misleading: it was written before I got really familiar with Scala, and could probably be condensed to about 150-200 LOC’s. But still, it seems Clojure has a 3-4x advantage on even Scala.
At first, the LISP syntax of Clojure looks bewildering to say the least for someone originating from a non-Lisp background. However, once you get your head around the basics and the basic forms, it is actually both very simple and compact at the same time. My guesstimate is that it would be a lot quicker to master Clojure than it would be to master Scala if you started today.
Using Clojure was a humbling experience, I’m certainly looking forward to doing more things in Clojure going forward. I strongly suspect my company will become a mixed Scala/Clojure “shop” as time goes on and I find the sweet spots for the different languages.
I’m not going to go and say that “Clojure is better than Scala” - I think they are both excellent languages, and I don’t really have a strong preference, other than looking forward to do more in Clojure as and when I find reasonable sized things to do to learn more (especially before making definite statements on pros and cons).
If you have yet to seriously check out Clojure, I strongly suggest that you do! The first few hours may be frustrating and bewildering (you have been warned), but once you get over it, it is worth the effort. If you’re looking for a good build system, Leiningen seems to be it, which certainly works better and simpler than something like Maven. There are a number of books available on Clojure, and this tutorial seems a good start for beginners as well.
Oh, and my new Clojure app went into production the same day I started writing it.