Two weeks ago I fell in love…
What? What are you talking about? Weren't you already engaged in a relationship?? And what about your children??? What an asshole!!!!
Hang on, hang on. Before my own lynching gets worse, here’s the nuance: I’ve fallen in love, yes; but not with a person. I’ve falling in love with a programming language. I’m in love with Clojure.
How did I met Clojure?
It could be said that it was all Uncle Bob‘s fault. After reading this post of his, I was curious about Clojure, so I did some research. One of the most appealing videos I found on YouTube were two in which Rich Hickey, Clojure’s creator, gives a presentation in two parts. I wasn’t sure about being capable of resisting until the end of the videos (more than 3 hours if you add up the two videos). Boy, if I was capable… what a discovery…
Is that a bird? A plane? No: it’s Rich Hickey!
Probably, it would be more accurate to say that, more than falling in love with Clojure, I fell in love with the way in which Rich Hickey undresses the language: that mix of exotic elegance, pragmatism, aim, humor and erudition only geniuses like Mr. Hickey have.
Take a brief look at the beginning of this video. I fell under the spell of this small detail: the interviewer shows its personality by wearing a cowboy hat during the interview. Rich Hickey looks more formal. But wait, take a look at the paper glasses: the interviewer has his glass (of water?) on the table. On the contrary, Rich Hickey uses the tiny table just as a perch for his elbow, leaving his glass on the floor, away from undesired accidents. This small detail, in my opinion, is the best portrait sketch of Mr Hickey.
Clojure’s four legs
I’m just starting my (polygamous, I must admit) relationship with Clojure, so don’t expect from me to give you an experienced view about it (click here to read a relatively brief paper of Clojure’s history by Rich Hickey). For the moment, I can only give a bridegroom‘s point of view: I love Clojure’s looks and I love the fact that Clojure cares for me.
And how do you know that Clojure cares for you?
Rich Hickey stated in a Clojure’s presentation that Clojure’s four legs are:
- Functional (Clojure is a functional programming language).
- Lisp (Clojure is a Lisp dialect).
- Hosted on Java (taking advantage from the JVM’s Memory model and Garbage Collector).
- Concurrency (more on this later below).
If I had to humanize Clojure, reducing it to two legs, I would say that, at least from my ‘Javafied’ background, these are the two things that attracted me the most from Clojure:
- Immutability
- Concurrency
Immutability
Imagine a blank bill which could hold, in its whole lifetime, a different value: now of 5 dollars, then of 10 bucks, even it could sometimes end up having a surreal 3 dollars value. This would be a mutable bill. This would be a nightmare in the real world, wouldn’t it? Well, imagine no more: most of the OO-natured languages (like Java), at their core, embrace the concept of mutability in the form of mutable objects: its data structures, such as an array, a list, etc. are mutable. It’s you, the programmer, the one responsible for making sure that you manage correctly the state of your objects (or the third-party objects that you use, for that matter).
Clojure takes care of that for you: all of its data structures are immutable, by default!! If you had a 5 dollar bill and now you spent 4 dollars, it won’t be like erasing the five from your bill and drawing a 1 on it: you won’t have the five dollar bill anymore, you will have a brand new 1 dollar bill. You will also be sure that when you wake up, your dollar hasn’t changed its value because, you see, some other neighbour had a reference to your one dollar bill and used it to pay for his son’s candy.
And if you’re worried that immutability in Clojure comes with the burden of bad performance, rest assured: it’s not so. Probably the main reason for this lies in the fact that, in Clojure, data structures are not only immutable but persistent. This means that, for example, when a new element is added to a data structure, in Carin Meier’s words (author of Living Clojure), Clojure internally will do smart creations of new versions (of that structure) by using structural sharing.
Concurrency
In a similar way in which James Gosling ‘s Java originally was kind of C++’s “remedy” in automatically taking care of most of the pain associated to the manual allocation/deallocation of the memory pointers, I have the feeling that Rich Hickey has done the same thing with Clojure, concerning Java’s concurrency : with Java, the language gives you the tools: basically, locks. Again, you are left alone with those locks.
On the contrary, with Clojure, you don’t have to worry about the internal locks because Mr. Hickey has done that work for us. You only have to know which part of your business is concurrent and use language constructs like atoms, refs and agents to do it. Clojure will manage the locks internally. Watch this fantastic video to deepen into this topic.
Java 8 and Clojure
Of course, Java 8’s functional paradigm made a huge change in Java, allowing the language to mature in all these respects. In fact, I have the feeling that Java’s developers did take more than a look into Clojure’s design and solutions to implement Java 8’s functional programming topics. But I may be wrong, it’s just a hunch…
Anyway, as a Java developer, I’m very thankful for Java 8’s achievements. Don’t be jealous, my dear Java! It’s just that what in Java feels like elegant makeup, in Clojure it feels like natural elegance.
This post’s closure
Rest assured that I’ll talk more in the near future about it. For now, that’s it. Enough of talking about Clojure. It’s time to take a walk with my new girlfriend and play with her. With my immutable and persistent love…