On Beauty in Code

I was thinking about the topic of beautiful code this morning. There’s a lot of disagreement about what constitutes beauty in code. I’ve watched Marcel Molina Jr. talk about “Plato and Pythagoras”:molina. O’Reilly has published a “whole book on the subject”:oreilly. On the other hand, “Jeff Atwood thinks that there’s no such thing.”:atwood. I disagree with Jeff on this – I definitely think there is such a thing as beautiful code. But I’m not sure if my idea of what makes code beautiful is the same as others’.

What do I even mean when I say something is “beautiful”, anyway? Beauty is in the eye of the beholder, after all – there is no objective standard. For my purposes, I consider something beautiful when it triggers a certain emotional response. A flower is beautiful because I enjoy the simple act of observing it for its own sake.


When it comes to functional human-created objects, I’ve noticed that certain properties tend to trigger this emotional reaction. Here are a few products I find beautiful:

  • The “Mini Mag-Lite”:maglite
  • The “Fisher Bullet Pen”:fisher
  • “Chaco Sandals”:chaco
  • The “Apple MacBook Pro”:macbook

These are all products which are extroardinarily well engineered for their respective tasks. Out of that engineering emerges a kind of clean, austere beauty. I guess you could say my esthetic runs toward the “Bauhaus school”:bauhaus of thought.


By contrast, a lot of the code I have seen held up as “beautiful” has more in common with “MC Escher”:escher or a “Zen garden”:zen. In the case of the former, the beauty is all about intricate, brain-twisting “cleverness”:camping. In the latter case, “divine purity of expression”:fibonacci takes precedence over more worldly concerns.

To me, beautiful code – code that I can enjoy reading for its own sake – has the quality of expressing its function simply and clearly. Like a Mag-Lite, its form reflects its function in a way that is elegant, straightforward, and easy to grasp.


One of the projects that has recently impressed me as having this property in its code is the “Ramaze”:ramaze web framework. The Ramaze “source code”:ramaze_source is clean, straightforward, well-commented (but not excessively so). Most methods are only a few lines long, and the lines themselves are short. White space is in abundance, setting off stanzas of code. And the logic itself is usually easy to follow.

Routing is traditionally a thorny area in web application frameworks. Here is the Ramaze routing code, in its entirety (minus some documentation):

There is only one method of any length, @#resolve@. And that method has an easily recognizable cadence – if path matches some predicate, then perform some transformation, and return. Else move on to the next clearly-delineated stanza. This is another quality of beautiful code: each method body has a recognizable, almost archetypal “shape” which is not littered by special cases and digressions.


If you enjoy reading code, I recommend taking a stroll through the Ramaze “source code”:ramaze_source. It is very nicely presented online in a custom source browser. And most of it demonstrates a similar clean elegance to the code above.

p{clear: left}. So that’s an example of what beautiful code means to me. What about you? What code do you consider beautiful?