Stephen Ramsay    Home

Beautiful Code

Non-programmers are often bemused to hear programmers speak of beauty and elegance in code. It’s a strangely ineffable assignation, yet one that every serious programmer can make. Some pieces of code are not just utile, but dulci. And nothing thrills the heart of an engineer like the moment of beauty.

Paul Graham offers a code example in his book On Lisp (out of print but available on line) that strikes me this way. It’s just a tiny little “counter” procedure:

(defun make-adder (n)
  #’(lambda (x) (+ x n)))

This function takes a number, but instead of returning an incremented value, it returns a closure that will add that number to any subsequent function call. Hence:

(setq add2 (make-adder 2)
  add10 (make-adder 10))
(funcall add2 5)
(funcall add10 3)

Graham calls this an example of a “utility function,” but I must admit that when I first saw it, my reaction wasn’t “Wow, what a useful thing!” or “Ah, now I understand how lexical closures work!” My reaction was simply, “Good heavens, that’s gorgeous.”

But why? Perhaps it’s some convergence between beauty and cleverness. Cleverness isn’t always a virtue in programming (particularly since such moments often exchange clarity for concision), but sometimes things manage to be both clever and beautiful — clever because they’re beautiful, and beautiful because they’re clever. And “cleverness” is often (though not always) a component of art. The Mona Lisa exudes a kind ineffable cleverness, as does the astonishing photorealism of Old Masters paintings, and in both cases, it’s not merely craft that produces this sensation.

Beauty in code is not a function of utility at all (though the two often accompany one another). This code sample (found both in Sitaram’s Teach Yourself Scheme in Fixnum Days and Dybvig’s The Scheme Programming Language) also strikes me as beautiful. It’s a set of recursive co-routines for deciding whether a number is even or odd:

(define is-even?
  (lambda (n)
    (if (= n 0) #t
      (is-odd? (- n 1)))))

(define is-odd?
  (lambda (n)
    (if (= n 0) #f
      (is-even? (- n 1)))))

These co-routines pass the number back and forth; you get your answer, because the function that returns the answer will be the last procedure called — the last player with the football, as it were.

In one sense, the code is perfectly practical, since it’s prefatory to a discussion of letrec — a variety of conditional that allows you to assign local variables recursively. But in another sense, it’s entirely silly. Scheme, after all, provides both odd? procedures. But ah, what a beautiful piece of code. It’s not, “Wow, isn’t Lisp great!” or “Ah, now I understand letrec!” It’s just “What a beautiful piece of code.” For me, at least.

Hand a geek a pocket watch, and he or she will immediately open up the back and stare at the mechanism for an hour. And if you could persuade that person to speak out loud, they’d be saying things like, “Look at that! And that! Wow, that’s just…beautiful!” And that really is something like an aesthetic experience that is in a way indistinguishable from the experience of art.

I don’t mean to argue that Lisp is inherently beautiful; the examples are in Lisp because that’s my current obsession, and I’m reading a lot of Lisp code these days. In fact, I think what I’m talking about here transcends language. It’s certainly not a matter of syntax. One could easily imagine beautiful C, Java, or Fortran. I had certainly seen lots of examples of beautiful code before I knew anything at all about Lisp.

Got some beautiful code (in any language)? I’d love to hear about it.

blog comments powered by Disqus