P.J. Plauger is senior editor of C/C++ Users Journal. He is convener of the ISO C standards committee, WG14, and active on the C++ committee, WG21. His latest books are The Draft Standard C++ Library, and Programming on Purpose (three volumes), all published by Prentice-Hall. You can reach him at pjp@plauger.com.
This is a dangerous book. It purports to show 405 tips that "lead you past the traps and pitfalls of the language and show you how you might improve your programs," according to the Introduction. (I give no page numbers in this review because the book has nary a numbered page.) As such, it is obviously modeled on Effective C++, the excellent book by Scott Meyers (Addison-Wesley, 1992) that is organized around 50 "items." But where Meyers is always on the money, Porter's book short changes you most of the time. Even worse, it is also laced with counterfeit advice.
Some of the tips are just plain wrong. Tip 23 tells us, "There is No Need to Use the enum Keyword When Declaring Instances of an enum." True enough for C++, perhaps, but it's in a section ostensibly aimed at C programmers. Then Tip 25 says, "The Equivalence Between Arrays and Pointers to Arrays Is Only Valid for Function Arguments." Even if you gloss over the erroneous statement of equivalence, the stated restriction is still untrue.
Other tips are so badly stated as to be vacuous. Tip 68: "In Nested if Statements, a Dangling else Statement Goes with the Preceding if Statements." Where else? Or they are a matter of considerable debate, as with Tip 69: "A switch Statement Is More Efficient Than Nested if-else Statements." Or Tip 77: "It Is Faster to Use an Element Pointer Rather Than a Subscript when Scanning Arrays." As a general rule, if Porter makes a statement about efficiency, you can be reasonably certain his view is microscopic and his experience extremely limited. In fact, I soon learned to be wary of any generalization he makes about "most" computers or compilers.
When he is not outright wrong, Porter confuses with fuzzy writing. The first page of tips, for example, uses the following terms for objects with dynamic lifetimes: functions variables [sic], function-local variables, items on the stack, an item declared in a function, and local items. Given the level at which the advice is nominally aimed, such colloquial inexactitude is intolerable. (I personally find it unacceptable even in advanced writings on C and C++.)
He purports to give advice about standards conformance, but it is clear the author is vague about what's in the C Standard. Chapter 11 is called "The Standard C Library," and the Introduction says it covers "the ANSI C functions." But Tip 299 is about "Using swab" to Reverse Byte Order." That function is not in the Standard C library. Nor is the function putenv, discussed in Tip 301. Tip 302 has an archaic description of the character classification functions. Tip 303 misspells the tmpnam function. Tip 304 has a parochial description of the POSIX (not Standard C) function fstat. Tip 305 gives erroneous information about determining file lengths. And so on, and so on.
It is tempting to say that this book contains an error on every page, but I can't say that for sure. For one thing, C++ is sufficiently complex and unstandardized to date to offer lots of wiggle room. It could be that you could go for pages on end in that area and get advice that's as good as anybody else's. But I wouldn't bet the farm on it.
The author has obviously accreted considerable lore over years of writing C code, primarily under MS-DOS it seems. It frightens me, however, that he can be so far off base in so many critical areas and still consider himself an expert. It saddens me that major publishers still let stuff like this get into print and occupy precious shelf space in book stores. It depresses me that thousands of innocents will still buy this book and be befuddled by such meretricious advice.
Normally, I try to find some redeeming social value in any book I review. Snob that I am, I have to concede that a given book usually has something of value to somebody, even if it falls short of my lofty standards. In this case, however, I can't. Rather, I wish for once that Tipper Gore could push through mandatory warning labels for books on C and C++ that actively mislead. Kids, what you are seeing is performed by an untrained professional. Don't try this at home.
Title: The Best C/C++ Tips Ever
Author: Anthony Porter
Publisher: McGraw-Hill
Pages: approximately 500
Price: $29.95
ISBN: 0-07-881820-6