[2010-02-24] “Code Complete”

Books filled with good practical advice about constructing software are rare. Code Complete by Steve McConnell is a well-written rarity in this field and has a well-deserved reputation as a classic. It is one of those books that every computer programmer ought to have read. I had read the first edition, published in 1993, as a budding programmer and the book left a lasting impression on me. With the benefit of several years of experience, I find myself agreeing almost entirely with the updated second edition, published in 2004. When someone asks me “How do I write good code?”, I point them to this book without hesitation.

The book is about “software construction” - as the author notes, this includes coding and debugging, detailed design, construction planning, unit testing, integration, integration testing, etc. The bulk of the book focusses on coding since most of the effort in construction is spent in coding. The emphasis of the book is towards creating a toolbox of effective software construction techniques and on the importance of using the right tool for a given job. In particular, the author repeatedly advises you to programme into a language rather than in it.

An endearing quality of the book is that instead of pontificating, the author offers copious references to papers, articles and books to back up his claims. The tone of the book is entirely non-condescending and you can see that the advice is coming from a wary professional with years of experience working on real projects with real people rather than a wily snake-oil salesman. Rookie programmers will do well to heed the advice in the book and experienced programmers will find themselves nodding every now and then as they read the book.

At around 960 pages the book might seem intimidatingly big, but it is worth spending the time needed to read it from cover to cover. The book is divided into several parts, each part comprising several cohesive chapters. Each chapter begins with an overview of the material covered in the chapter and ends with pointers to additional resources, a summary of the key points of the chapter and a check-list to help you verify if your code adheres to the guidelines prescribed in the chapter. The side-margins of a page are used to provide cross-references to material covered elsewhere in the book, insightful or funny quotes, a short URL on the book's web-site that contains additional material and icons to bring something to your attention. These icons are “Hard Data” (typically a reference to a study backing some claim), “Key Point” and “Coding Horror” (an example of badly-written code). This structure makes it easy to find the material you're looking for as you refer to the book from time to time.

The focus of the book is on imperative programming and it mainly uses code in Visual Basic, C, C++ and Java as examples. Functional programming enthusiasts might particularly take offence to the material on recursion where the author seems quite disinclined to use it and advises minimising its usage. The author gives an example of a recursive implementation of a factorial function and says that it is slow and has an unpredictable impact on memory. In fact, the code uses a tail-call and is therefore a prime candidate for tail-call optimisation using a decent compiler. A good programmer can use recursion to create neat functions without adverse effects in many a case.

The author rightly points out the perils of premature optimisation of programmes, especially that done without proper measurements to identify the real bottlenecks. The primary emphasis should be on creating correct and maintainable code with performance improvements taken up at the end, if it is needed. I generally agree with this, but would point out that many a time you would barely have time to tackle performance in the end if you're working on a tight schedule (as most of us are forced to do). Many programmes also have a flat profile in that there are no obvious bottlenecks and yet the programme is unacceptably slow. If you have automated regression-testing set up with comprehensive code-coverage (as the author recommends elsewhere in the book), performance becomes important even if it doesn't matter during a single run of the programme or during a typical user-interaction. In such cases, it might be better to keep an eye on the performance of a programme during most of its construction.

There are at least three important aspects of software construction that the author unfortunately leaves out of this otherwise excellent and comprehensive book: security, supportability and concurrency. Writing secure code is difficult, but very important and precious few programmers are aware of the necessary techniques. The supportability of software determines how effectively you are able to diagnose problems encountered during its deployment - logging, state-dumps, etc. are techniques that help here. Concurrency is important but is made difficult by constructs available in mainstream programming languages. To be fair to the author, it is only relatively-recently that security and concurrency have come into increased focus with the Internet and many-core CPUs being the prime drivers respectively.

With an additional 10 years of experience gained before the publication of the second edition of this book, the author has slightly revised some of his earlier recommendations and freely admits it. For example, the first edition recommended a pretty-formatting of code where the “=” operators of consecutive assignment statements line up like this:

int foo       = bar;
boolean snafu = wombat;

It turns out that while this looks pretty in the beginning, it leads to additional effort as the code changes (e.g. variables change names) during subsequent maintenance. It is not much worse to have a simpler formatting that does not place unnecessary burden on maintenance. The second edition also covers object-oriented programming and agile development methods, but presents them in an even-handed pragmatic manner. Much of the advice from the first edition remains intact in the second edition.

The quality of the Indian reprint, published by WP Publishers, is unfortunately quite disappointing. It has very thin pages because of which some of the text and graphics on the opposite side of a page show through. The ink doesn't have a good consistency and fades a bit in some places. These make it a bit hard to read this reprint. At a price of Rs 600, they could have done a better job.

If you are a computer programmer and wish to improve yourself, get this book and read it.

Other Posts from 2010