Thoughts from the office by Ed Ball
Friday, August 20, 2004

If you want to write exceptional C++, you must read Herb Sutter’s books, and Exceptional C++ Style is no exception. (Har!) Like its predecessors, Exceptional C++ and More Exceptional C++, the book is organized into items, each of which has a few questions followed by detailed answers that present the most up-to-date ideas for writing good C++ code. I like Herb Sutter’s writing style – and, of course, his expertise – so I had no trouble reading this book from start to finish.

The book can also be skimmed fairly easily, since each item has a nice summary at the end, as well as highlighted guidelines throughout. The most interesting stuff from my perspective:

  • I always forget about vector<>::at(). In particular, I forget that it is bounds-checked. Of course, that explains why I never use it – very few STL functions are bounds-checked in any way, so it’s good to get into the habit of realizing that, if you do something wrong, you’re going to crash, not get a nice exception. It’s speed over safety in the STL.
  • Avoid template specialization of function templates. (That’s an oversimplification; read item 7 for how to do this the “right way.”)
  • Never use protected data members. It’s tempting, but there’s always a better way.
  • “The most important thing to get right is the interface. Everything else can be fixed later.” I totally agree.
  • “Prefer to make interfaces nonvirtual.” That is, try to avoid public virtual functions. In fact, “prefer to make virtual functions private” unless “derived classes need to invoke the base implementation of a virtual function,” in which case they can be protected. Absolutely.
  • “A base class destructor should be either public and virtual, or protected and nonvirtual.” This isn’t instinctive to me yet, but I believe it.
  • “There’s often little point in checking for new failure anyway.” It’s about time somebody admitted that allocations never fail – and if they do, you’re not going to be able to recover.
  • Nearly useless keywords: inline, auto, register.
  • “Remember that floating-point math is weird and deeply strange.” I understand the pitfalls of integer arithmetic, but almost nobody really understands floating-point math.
  • “Avoid macros.” Common C++ knowledge for ages, yet I still find them being used when an inline function would do just fine. Stop it!
  • “Prefer writing functions as nonmember nonfriends.” The true C++ guru provides only the absolute minimum number of member functions for a class; the rest of the functionality is provided by nonmember functions. Unfortunately, STL fails this guideline, so we don’t have a good example to follow.

I have loved C++ ever since I first started using it. I hope that C++/CLI will enable me to productively use C++ far into the future; C# is cool, but not as cool as C++. If you are a C++ guru, or aspire to be one, this book and its predecessors come highly recommended.

8/20/2004 9:10:53 AM (Pacific Daylight Time, UTC-07:00) | Comments [0] | Books#
Search
Archive
Links
Categories
Administration
Blogroll