Posts Tagged ‘macros’

10 reasons to learn Clojure (or any other Lisp)

January 8, 2010

Horror and disgust. Or, more precisely, (horror (and (disgust))). That’s what I felt when I first saw Lisp code. What masochistic idiot could possibly enjoy using such a language? Reading Paul Graham’s essay Revenge of the Nerds was a turning point. Lisp turned out to be a bit like chocolate mousse. At first sight, it might look like dog shit. But once you put aside your prejudices, it turns out to be delicious! Learning Lisp is a bit like learning how to drive a Ferrari, when you only knew how to drive a wheelchair. It’s difficult, but once you get it, it will open up a whole new world for you.

Why should you start learning Lisp? Let me give you some concrete reasons. Some personal frustrations that you might recognise. Frustrations you wouldn’t have had with Lisp. Here are my 10 reasons to learn Clojure (or any other Lisp):

  1. Tired of boilerplate? Tired of having to write the same seven lines of code for each getter and setter? Tired of writing try-catch-finally-catch-finally clauses? Lisp lets you abstract all boilerplate code in simple macros. Once and for ever.
  2. Boilerplate code is no problem? Because “your IDE can generate it anyway”? How often did you change a single line of generated code, because it just didn’t do what you needed? And how often did you have problems, because after that you had to generate the same code again? How often did you have half generated half adjusted code that was unreadable and unmaintainable? Wouldn’t you rather declare a getter and setter in two words, instead of having to generate all this blurring code again and again?
  3. Had to wait for years and years until Sun finally built in an Enhanced For-Each Loop in Java? In Lisp, you’d have created your own for each-loop already years ago. In just a few lines of code. And you’d have used it over and over again.
  4. Libraries are reusable program code. Design patterns are reusable architecture. Libraries can be written once and reused every time. Design patterns have to rewritten again and again. Not in Lisp. In Lisp you’d declare the pattern in a macro and you’d only have to write the actual code.
  5. Thrilled about Domain Specific Languages? In Lisp, embedding languages is the most natural thing to do. And Lisp has been around for over 50 years already. Keep that in mind, the next time a person tells you that the new and overly hyped language X is so great because it supports DSLs so well.
  6. It’s functional! Somehow the idea of reusability got attached to object-oriented programming in the 1980s, and no amount of evidence to the contrary seems to be able to shake it free. But where objects are like big lego blocks, that only fit together in a very specific way, functions can be passed to other functions, combined and reapplied in many ways. And of course, not everything has to be a function.
  7. Were you happy that in your language “everything is an object”? Then look at horrible Java interfaces like ActionListener and think again. An ActionListener is just a function. Wouldn’t you like to treat it as such?
  8. Were you happily surprised when you heard about Aspect Oriented Programming? Were you happy with all the new possibilities it gave you? In Lisp you wouldn’t have needed it. Did you want to add logging to every function? Why not write your own operator for defining a function, that adds logging instantly?
  9. Write less, do more. In chapter 11 of Practical Common Lisp, Peter Seibel writes a unit test framework in only 26 lines of (very readable) code. Less code means less opportunities for bugs and less maintenance. Less code means less work.
  10. Not convinced yet? Then note that Even Jennifer Aniston has quit using other programming languages and now prefers Clojure (see Mark Volkmann’s Clojure Page).

Are there no drawbacks? Sure, learning it will be hard. Writing your first Lisp macros will definitely give you lots of frustrations. But you’ll be rewarded with more power than you’ve ever had in any other language. Think big. Think Lisp. Start today.