Book Review: Beautiful Code - Leading Programmers Explain How They Think


by Chris Douce

Beautiful Code - Leading Programmers Explain How They Think

Edited by Andy Oram and Greg Wilson
O'Reilly, 2007
ISBN 978-0-596-51004-6

I think I first discovered the idea of beauty, or at least aesthetics in software when I was told about that famous book The Art of Computer Programming by Donald Knuth. In my undergraduate days I was astonished by elegance when I was presented with parsing algorithms during my compiler classes during my undergraduate days. I was then astonished when I finally 'got' the quicksort algorithm. Ever since, I have always had an interest in the elegance of software – not necessarily in terms of algorithmic sense or in terms of elegance of embedded systems where every instruction may yield a saving, but in how code can communicate to both reader and machine.

When I saw this book advertised, I put in an order straight away – it's title struck a chord. But no sooner had the book arrived in the university library for collection, some bounder had already taken it out!

There is no other way to say this: Beautiful Code is a big book. Just shy of five hundred and fifty pages and thirty three chapters, there is a lot to get through. As a result I have to confess that I have chosen my reading. There is something here for Perl scripters, accessibility developers, testing fanatics, debuggers, biologists, developers working with concurrency, embedded systems designers and those working with mathematical algorithms.

Kernighan kicks off on page 3 by writing 'solving the right problem is a big step toward creating a beautiful program'. This is later echoed by Kowlawa who write 'code should accurately and efficiently perform the task that it was designed to complete, in such a way that there are no ambiguities as to how it should behave'.

Jon Bentley quotes Antoine de Saint-Exupéry who 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", p.29, a sentiment that is, of course, efficiently reflected in the abbreviation DRY.

There were other treats in store for me too. Whilst reading a chapter on Javascript (p.130) that illustrated how simplified Javascript interpreter could be implemented using simplified Javascript interpreter, I recalled being bewildered at a lecture that presented how elegantly you could write a Prolog interpreter using a Prolog interpreter. Zeller, on p.463 describes debugging a debugger, of course!

Beauty and elegance can also be found during testing: 'the main purpose of tests is to instill, reinforce, or reconfirm our confidence that the code works properly and efficiently', Savola, p.86. Tests should be clear to developers other than the tester as well as building confidence and understanding.

There are examples of programming in the large too. The book is also complete with forays into the world of service-oriented architecture and concurrency (anyone remember Occam?).

There is much that resonates. For example, 'ideally, everything that you need to understand a piece of code should fit on a single screen. If not, the reader of the code will be forced at best to hop around from screen to screen in the hopes of understanding the code', p.255. 'I believe that beautiful code is short code, and I find that lengthy, complicated code is generally quite ugly', p.261. Gulhati, p.163, is after my heart by when he writes, '.. success and beauty in an economic sense depends directly on the code being flexible enough to evolve over time and meet the requirements of its users, and do it again and again and again over the course of many years'.

As I have hinted at earlier, for me, programming beauty should be a compromise that expresses ideas elegantly for both the machine and for the reader. I strongly believe that programmers should be kind to their peers. Helping others to understand your own work will in turn benefit the organisation that you work for. Aspiring to (and hopefully achieving) beauty can save money.

Some aspects of beauty are ultimately intertwined with common sense: use local scope, minimise side effects, don't use global variables, and much of these sentiments are echoed throughout the book.

I like Beautiful Code a lot. I can see myself looking at some chapters again and again. This said, I think a more firm and clearer chapter on databases would have been welcome. Correctly designed databases are a joy to the eye of the beholder! The whole book might have been stronger had it been subjected to slightly more vigorous editing.

This said, I recommend Beautiful Code. There are some cracking chapters. Plus, all royalties are said to go to Amnesty International. You can't get fairer than that.

As ever, Wikipedia has a number of cracking pages that are a treat for the browser: