Chapter 3. The Most Beautiful Code I Never Wrote

Jon Bentley

I once heard a master programmer praised with the phrase, “He adds function by deleting code.” Antoine de Saint-Exupéry, the French writer and aviator, expressed this sentiment more generally when he said, “A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away.” In software, the most beautiful code, the most beautiful functions, and the most beautiful programs are sometimes not there at all.

It is, of course, difficult to talk about things that aren’t there. This chapter attempts this daunting task by presenting a novel analysis of the runtime of the classic Quicksort program. The first section sets the stage by reviewing Quicksort from a personal perspective. The subsequent section is the meat of this chapter. We’ll start by adding one counter to the program, then manipulate the code to make it smaller and smaller and yet more and more powerful until just a few lines of code completely capture its average runtime. The third section summarizes the techniques, and presents a particularly succinct analysis of the cost of binary search trees. The final two sections draw insights from the chapter to help you write more elegant programs.

The Most Beautiful Code I Ever Wrote

When Greg Wilson first described the idea of this book, I asked myself what was the most beautiful code I had ever written. After this delicious question rolled around my brain for the ...

Get Beautiful Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.