E.M. Forster, in his essay Aspects of the Novel, draws a distinction between "flat" and "round" characters in literature (two types that I suspect pretty much all of us can intuitively identify from our own reading lives), in the following deceptively simple manner:

"The test of a round character is whether it is capable of surprising in a convincing way."

I love that quote.  It's so concise, but it gets at the heart of something profound about the reading experience (and also at the challenge of creating such characters).

Recently, it occurred to me that maybe there's an analogous way to describe the notion of "beauty" in code.  Here's my bid:

Beautiful code is code that surprises us with its simplicity

Meaning: you've been working in some problem space, and have built some complex solutions, which, you feel, are about as good as can be done.  Then, someone comes along, and shows you a solution that somehow makes all the complexity go away.  Those moments are electrifying -- that's beauty in code, I think.

(Side note: I think you could counter-define "clever" code as that which surprises us, but not by simplicity).

Once that occurred to me as a general statement, all sorts of examples came to mind:

  •  My very first paid programming job involved parsing some dates out of a text file. Which, of course, I did with C, which I had just learned that year in school.  The logic was pretty complex, but I was proud that I mostly got it right.  Then, someone showed me regular expressions, and it was like the scales fell from my eyes.  All that complexity, just gone, and in its place this concise, elegant description of a pattern.  What an incredibly beautiful idea.

  • Yoav and I were recently talking about how we both have this very specific, vivid memory of sitting in front of our computers watching the original Ruby on Rails screencast -- the one where David Heinemeier Hansson builds a blog server from scratch in 15 minutes.  I'd been creating database-backed websites for years, but he had somehow come up with this way that was just so insanely concise and simple.  It was like magic.

  • List comprehensions (for me, in python) -- I remember telling a friend how great they were, and excitedly writing out on a nearby blackboard something like..

    [ transform(x) for x in lst if is_valid(x) ]

    ...and just staring at it and thinking about how much was going on for each small piece of that line.  I'm so used to that idiom now, but what a lovely, simple thing.

  • The "Road to the Generic Server" section in Joe Armstrong's Programming Erlang.  Just lifted the top of my head off and put it back on in a different way.  He approaches writing a server from this (to me) odd angle, and then goes on to show how incredibly powerful it is, again, in this amazingly simple way.

I could go on, but I'm curious for other people's takes.  What do you think of that proposed definition of beautiful code?  Are there examples of surprising simplicity that you find particularly exhilarating?

so-hiring

Recommended Articles

Join our subscribers

Sign up here and we'll keep you updated on the latest in product, UX, and engineering from HubSpot.

Subscribe to the newsletter