“Effective C++”, 3rd edition by Scott Meyers

Effective C++

"Effective C++", 3rd ed.

At the last agile roundtable, I picked up a copy of Effective C++: 55 Specific Ways to Improve Your Programs and Designs, 3rd edition, by Scott Meyers. There are some programming books that I read in chunks over time, like Design Patterns, or Refactoring and then there are books that I prefer to absorb in one big intellectual gulp. “Effective C++” is in the latter category.

At 272 pages and about 3/4 of an inch thick, “Effective C++” is not in the “tome” category of programming book. Yet its not exactly a “light read”, either. This book is organized as a series of 55 specific items about C++ described in a short essay and accompanying code examples. The items are clumped together into chapters around a theme for the items. You could read the items in any order on an as-needed basis if you were venturing into a new area for the first time, or you can read all the items in order. The chapters are titled as follows:

  1. Accustoming Yourself to C++
  2. Constructors, Destructors, and Assignment Operators
  3. Resource Management
  4. Designs and Declarations
  5. Implementations
  6. Inheritance and Object-Oriented Design
  7. Templates and Generic Programming
  8. Customizing new and delete
  9. Miscellany

The specific items are as follows:

  1. View C++ as a federation of languages.
  2. Prefer consts, enums and inlines to #defines.
  3. Use const whenever possible.
  4. Make sure that objects are initialized before they’re used.
  5. Know what functions C++ silently writes and calls.
  6. Explicitly disallow the use of compiler-generated functions you do not want.
  7. Declare destructors virtual in polymorphic base classes.
  8. Prevent exceptions from leaving destructors.
  9. Never call virtual functions during construction or destruction.
  10. Have assignment operators return a reference to *this.
  11. Handle assignment to self in operator=.
  12. Copy all parts of an object.
  13. Use objects to manage resources.
  14. Think carefully about copying behavior in resource-managing classes.
  15. Provide access to raw resources in resource-managing classes.
  16. Use the same form in corresponding uses of new and delete.
  17. Store newed objects in smart pointers in standalone statements.
  18. Make interfaces easy to use correctly and hard to use incorrectly.
  19. Treat class design as type design.
  20. Prefer pass-by-reference-to-const to pass-by-value.
  21. Don’t try to return a reference when you must return an object.
  22. Declare data members private.
  23. Prefer non-member non-friend functions over member functions.
  24. Declare non-member functions when type conversions should apply to all parameters.
  25. Consider support for a non-throwing swap.
  26. Postpone variable definitions as long as possible.
  27. Minimize casting.
  28. Avoid returning “handles” to object internals.
  29. Strive for exception-safe code.
  30. Understand the ins and outs of inlining.
  31. Minimize compilation dependencies between files.
  32. Make sure public inheritance models “is-a”.
  33. Avoid hiding inherited names.
  34. Differentiate between inheritance of interface and inheritance of implementation.
  35. Consider alternatives to virtual functions.
  36. Never redefine an inherited non-virtual function.
  37. Never redefine a function’s inherited default parameter value.
  38. Model “has-a” or “is-implemented-in-terms-of” through composition.
  39. Use private inheritance judiciously.
  40. Use multiple inheritance judiciously.
  41. Understand implicit interfaces and compile-time polymorphism.
  42. Understand the two meanings of typename.
  43. Know how to access names in templatized base classes.
  44. Factor parameter-independent code out of templates.
  45. Use member function templates to accept “all compatible types”.
  46. Define non-member functions inside templates when type conversions are desired.
  47. Use traits classes for information about types.
  48. Be aware of template metaprogramming.
  49. Understand the behavior of the new-handler.
  50. Understand when it makes sense to replace new and delete.
  51. Adhere to convention when writing new and delete.
  52. Write placement delete if you write placement new.
  53. Pay attention to compiler warnings.
  54. Familiarize yourself with the standard library, including TR1.
  55. Familiarize yourself with Boost.

I was familiar in one way or another with most of these items, but its good to have them all in one place instead of sprinkled throughout other books. I particularly enjoyed the chapters on “Inheritance and Object-Oriented Design” and “Customizing new and delete”. I found the coverage of multiple inheritance and private inheritance — two features unique to C++ compared to languages like Java or C# — to be quite useful.

There were a few items that taught me new things about my old friend C++. Item 12: Copy all parts of an object reminded me that my copy constructors of derived classes need to call the copy constructor of the base class and that my assignment operators of derived classees need to call the assignment operators of the base class. This is one of those mistakes that is all too easy to make and when you read the item you think “well, duh, of course you have to take care of the base class!”, yet there is nothing in the compiler that will warn you about it if you don’t.

Item 17: Store newed objects in smart pointers in standalone statements points out one of those subtle ways that resource leaks can sneak into your application as a result of trying to cram too much into a single logical statement. I tend not to like gratuitous variable declarations in functions and methods, but this is a case to remember as it may look gratuitous at first glance but it really does serve a purpose.

Item 27: Minimize casting once again reminds us once again how casts are a code smell for C++ and are best avoided. Why, just today I was reviewing some code in a product where I work and found a horrid and fragile construction that compiled cleanly because the programmer used a C-style cast to sweep things under the rug. The code in question is what I call “working by accident instead of working by design”; any number of seemingly minor changes to the source will cause this code to fail and yet still compile cleanly, all thanks to the use of a C-style cast.

Overall I would recommend this book to anyone who programs in C++ on a regular basis. If you are doing simple C++ programming on an infrequent basis, you can probably get by without this book. Still, you will find that some of the mystery of C++ revealed by this book, even if you only program infrequently in C++. Item 1: View C++ as a federation of languages reminds us that C++ is much, much more than just an object-oriented version of C, or “C with classes”. The interesting work happening in Boost and template metaprogramming is an example of this: C++ supports many programming paradigms, while other languages support one (Pascal for procedural programming, C#/Java for object-oriented programming, etc.).

One Response to ““Effective C++”, 3rd edition by Scott Meyers”

  1. Robert Matthews Says:

    One of my favorite C++ books, right up there with “Modern C++ Design” by Andrei Alexandrescu. They have a couple “Effective” books for C# in this same series.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: