Programmer's Book Review

C/C++ Users Journal July, 2005

By Jeffrey L. Taylor

Jeffrey Taylor worked for 30 years in software development. Currently, he is an independent journalist and editor in Austin, Texas, and can be contacted at jeff.taylor@ieee.org.

C++ Common Knowledge: Essential Intermediate Programming
Stephen C. Dewhurst
Pearson Education, 2005
250 pp., $29.99
ISBN 0321321928

The stated goal of Stephen Dewhurst's C++ Common Knowledge is "what every professional C++ programmer needs to know—pared to its essentials so it can be efficiently and accurately absorbed." There are two parts to what Dewhurst is attempting: what to teach and how to teach. He has chosen well "what to teach." Some of the last items may be considered advanced C++ programming, but maybe they shouldn't be. Long division was once a graduate student topic. Now it is for grade schoolers. On the "how to teach," he has also succeeded. I have read the more advanced items elsewhere and did not get much out of them except stretch marks on my brain. By sticking to just the essentials, Dewhurst conveys the core of the concept. A case of less is more. Too much elaboration too early confuses rather than enlightens. With only 250 pages, there isn't room to go on too long.

Writing is an odd business. You can start in on a subject you know and have it fall apart in your hands. For instance, I liked Dewhurst's previous book, C++ Gotchas, but every draft of a review fell apart. After reading C++ Common Knowledge, I understood why this happened. It is hard to write a review that hangs together if the book does not. C++ Gotchas is a collection of items. With C++ Common Knowledge, however, there is a straightforward progression of items that flow one into the next with no wild leaps. C++ Gotchas is like a seminar series with a new speaker each week. C++ Common Knowledge is like a course organized by one person. Some items contain ideas or quotes from other people, but each item generally connects with the previous and following items.

C++ Common Knowledge is aimed at: C experts with little C++ knowledge, new hires with C++ knowledge but little experience, Java programmers moving to C++, and C++ program maintainers who need more breadth. I would add C++ programmers who used to be proficient, but the state of the practice has advanced from under them while they worked in other areas. All groups share a common trait, that bits of the book will be familiar ground. Don't let familiarity breed complacency. Mixed in with the customary are new ways of looking at old items, for instance: "References are Aliases, Not Pointers" (item 5). I admit to this one, references are just pointers with simpler syntax, right? Not quite. And the differences influence code design choices. The key differences are that pointers can be NULL, references must reference (alias) something, and references cannot change what they refer to. Unless lookups cannot fail, they should return pointers, not references.

Part of the brilliance of C++'s design is that numerous features can be used in ways the original designers never anticipated. (Of course, any language can be abused in ways the designers neither anticipated nor would approve of.) Bjarne Stroustrup admits that the decision to have "private," "protected," and "public" affect access—not visibility—was serendipitous. He now sees that the other way around, changing a function from public to private (or the reverse) could quietly change the semantics of a program. For instance, a different function could be called instead of just refusing to compile.

C++ metaprogramming, the topic of the last dozen or so points, was not anticipated when C++ was initially designed 20 years ago. It has been found that some fairly sophisticated conditional compilation can be done without the preprocessor. A class can be queried for certain properties and appropriate code generated at compile time instead of conditionally executed at runtime. These points are a good introduction to the topic because just the core ideas are covered. Having finally understood the essential ideas, I am ready to return to Andrei Alexandrescu's Modern C++ Design and David Abrahams and Aleksy Gurtovoy's C++ Template Metaprogramming.

The insights gained in familiar topics probably will not need to be reread. I expect to return to items new to me several times as I try them out. C++ Common Knowledge is a good book to advance the state of the practice. There are several others—Herb Sutter's books come to mind—each with its own unique contributions and new slants on old topics. I haven't found just one "desert island" selection, but C++ Common Knowledge would be in my desert island box.