Online Book Reader

Home Category

Beautiful Code [102]

By Root 5157 0
of randomizing Quicksort on a set of distinct elements therefore gives us the average number of comparisons to insert randomly permuted distinct elements into a binary search tree.

The Most Beautiful Code I Never Wrote > What Is Writing?

3.4. What Is Writing?

In a weak sense, I "wrote" Example 3-2 through Example 3-12 of the program. I wrote them first in scribbled notes, then on a chalkboard in front of undergraduates, and eventually in this chapter. I derived the programs systematically, I have spent considerable time analyzing them, and I believe that they are correct. Apart from the spreadsheet implementation of Example 3-11, though, I have never run any of the examples as a computer program.

In almost two decades at Bell Labs, I learned from many teachers (and especially from Brian Kernighan, whose chapter on the teaching of programming appears as Chapter 1 of this book) that "writing" a program to be displayed in public involves much more than typing symbols. One implements the program in code, runs it first on a few test cases, then builds thorough scaffolding, drivers, and a library of cases to beat on it systematically. Ideally, one mechanically includes the compiled source code into the text without human intervention. I wrote Example 3-1 (and all the code in Programming Pearls) in that strong sense.

As a point of honor, I wanted to keep my title honest by never implementing Example 3-2 through Example 3-12. Almost four decades of computer programming have left me with deep respect for the difficulty of the craft (well, more precisely, abject fear of bugs). I compromised by implementing Example 3-11 in a spreadsheet, and I tossed in an additional column that gave the closed-form solution. Imagine my delight (and relief) when the two matched exactly! And so I offer the world these beautiful unwritten programs, with some confidence in their correctness, yet painfully aware of the possibility of undiscovered error. I hope that the deep beauty I find in them will be unmarred by superficial blemishes.

In my discomfort at presenting these unwritten programs, I take consolation from the insight of Alan Perlis, who said, "Is it possible that software is not like anything else, that it is meant to be discarded: that the whole point is to see it as a soap bubble?"

The Most Beautiful Code I Never Wrote > Conclusion

3.5. Conclusion

Beauty has many sources. This chapter has concentrated on the beauty conferred by simplicity, elegance, and concision. The following aphorisms all express this overarching theme:

Strive to add function by deleting code.

A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away. (Saint-Exupéry)

In software, the most beautiful code, the most beautiful functions, and the most beautiful programs are sometimes not there at all.

Vigorous writing is concise. Omit needless words. (Strunk and White)

The cheapest, fastest, and most reliable components of a computer system are those that aren't there. (Bell)

Endeavor to do more and more with less and less.

If I had more time, I would have written you a shorter letter. (Pascal)

The Inventor's Paradox: The more ambitious plan may have more chance of success. (Pólya)

Simplicity does not precede complexity, but follows it. (Perlis)

Less is more. (Browning)

Make everything as simple as possible, but no simpler. (Einstein)

Software should sometimes be seen as a soap bubble. (Perlis)

Seek beauty through simplicity.

Here endeth the lesson. Go thou and do likewise.

For those who desire more concrete hints, here are some ideas grouped into three main categories.

Analysis of programs

One way to gain insight into the behavior of a program is to instrument it and then run it on representative data, as in Example 3-2. Often, though, we are less concerned with the program as a whole than with individual aspects. In this case, for instance, we considered only the number of comparisons that Quicksort uses on the average and ignored many other

Return Main Page Previous Page Next Page

®Online Book Reader