Seeking Clojure

Over the last three months I’ve been trying to learn Clojure on evenings and weekend. I started this roller coaster ride of joy and frustration  on the recommendation of my close friend and Clojurian @otfrom.

The start of this emotional experience is rooted in a conversation with @otfrom over a glass of wine at the Open Data Institute. The glass of wine may have been a major contributor to my decision to take the plunge into Lisp for the JVM. The conversation went something like this:

  • Me: I’m keen to learn functional programming as I think it has advantages over Object Orientation, especially in the brave new world of ‘big data’ and the increasing emphasis on massively concurrent processing. I’ve started reading a book on Scala because as a Java developer I thought it would be a good place to start.
  • @otfrom: Sounds like a good idea. Scala is a good language if you want to dip your toe in the world of functional programming.
  • Me: Yeah, I thought as it’s like Java I could encourage our other Java developers to start learning and using it.
  • @otfrom: OK. Only thing is, do you think that if they’re struggling to learn they might just abandon the functional style and just start writing Java without semi colons?
  • Me: ..Oh sh*t, you’re right. The whole point of this is to learn functional programming not write OO in a different language.

So I committed to learning Clojure as it’s very hard to write without using a functional style and the default data structures are all immutable. I bought a book, hit some web sites and fired up a REPL (Read Eval Print Loop – Clojure’s interactive runtime environment).

As I started reading I got excited about the possibilities presented by using immutable data structures to simplify reasoning about concurrent processing, the built in support for concurrency, how Clojure code (text) is data and data can become code (someone earned Nerd points by calling this ‘homoiconicity’) so building Domain Specific Languages is simple. I also found getting a Clojure environment up and running with Leiningen was so simple I almost fell off the sofa.

However, as much in equal measure with the growing fascination, I started to get frustrated that in the time I spent writing one lovingly crafted line of Clojure, that would take 30 lines of Java code, I could have written hundreds of lines of Java! Although getting immediate feed back from entering example code into the REPL was satisfying I missed the warm comforting feeling of TDD. I couldn’t figure out how to make this damn language bend to my will for even the simplest tasks.

Interestingly enough I had no problem with the commonly voiced concern that Clojure has too many parenthesis ‘( )’. you could equally say Java has too many braces ‘{ }’ and parenthesis!

However, I persevered as I have already been through a mind shift when moving from procedural programming with Fortran and COBOL (yes, I’m that old!) to writing OO with Java.

Slowly, painstakingly I worked through examples, read more of my book and continued to step on bear traps.

About two weeks ago I decided to try and create a simple example REST API in Clojure. I had already played with a couple of tutorials about Compojure (I found the Heroku Blog was a good place to start) so I thought I’d use that and I picked Liberator for the REST based library.

I was still keening from the loss of Test Driven Development in my life. In my experiments so far I’d toyed with Clojure Test but I hadn’t written enough code or learned enough to iterate through test-first, fail, pass, refactor.. So as part of this exercise I committed to trying TDD using Midje.

Once I started using Midje I began to smile. I felt more comfortable. I was still using the REPL to confirm my poor knowledge of Clojure but only enough to write Midje ‘facts’. Running a REPL in EMacs in a window side by side with my code and using Cider to interactively execute my tests as I wrote each Clojure sExpression felt comforting and satisfying.

I still took ages to code but I was getting faster. More importantly I felt happier and I am having fun.

To be continued….

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s