My Notes on “More Effective C++” by Scott Meyers
4 May, 2009 § Leave a comment
More Effective C++ (MECPP) is an extension on Effective C++ (ECPP), both written by Scott Meyers. The topics in MECPP don’t require that you have read ECPP, but many Items in MECPP refer to Items within ECPP, so it helps to have read them in order.
Some of the Items in the book are quite applicable, yet others mention topics that are seldom of use, and if so, are probably a good indication of a bad architectural design in your codebase.
I’ll cover what I have deemed the more useful Items in the book.
Item 1: Distinguish between pointers and references. This is very important because references won’t act polymorphically, meaning that if you pass a derived object by reference and cast it to the base class in the process, you can slice off information about the object.
Item 2: Prefer C++-style casts. This was already mentioned in Item 27 of ECPP, but is reinforced within Item 2.
Item 4: Avoid gratuitous default constructors. If an object lacks meaning without a certain variable defined, then it is OK to create a class lacking a default constructor. This does have some downsides to it, such as creating an array of these objects on the heap is not the same as when there is a default constructor.
Item 5: Be wary of user-defined conversion functions. Strange program behavior can occur when implicit type conversions occur that are not anticipated. Many of these occur from single argument constructors. To solve this, a constructor should be made explicit. When a constructor is
explicit, you are telling the C++ compiler to not allow implicit type conversions.
Item 13: Catch exceptions by reference. When exceptions are thrown, a copy of the exception is created. There is no way around this. If you catch the exception by reference, you can get rid of an extra copy (only one copy), yet if you catch it by value, there will be two copies.
The 80-20 rule states that 80 percent of the slowdown in your product is derived from 20 percent of the code. It can become too easy to try to optimize many parts of your code that have no bearing on the overall speed of your product. These optimizations are more than likely going to over complicate the codebase and not increase the speed of the program any noticeable amount.
I previously worked on an 80-20 problem with a website for a product that I work on. The website was dramatically slow as the database got larger, and the pages would make multiple requests for each item. As the database got larger, and more records were stored, the site became unusable. To solve this, I worked on implementing some of the topics that are covered by the YSlow tool, by Yahoo! This tool points out that many website performance issues can be solved with a minimal amount of work, and have a low chance of negative side effects. In about two weeks, the performance of the website increased by 75 percent and was more than usable when large amounts of records were entered in the database.
Item 25: Virtualizing constructors and non-member functions. In this Item, Scott covers how you can use virtual constructors to implement a Clone idiom. This idiom allows you to take Items that are casted to the base class and make copies of them, regardless of the derived class. This is pretty useful, and if I come across a time to implement this idiom, I’d like to give it a try.
Item 28: Smart pointers. This was covered also in ECPP, so I felt it was a little redundant, but still very useful.
Item 30: Proxy classes. I’d like to come back to this at some point and give a better explanation than I can at the moment.
Overall, I think this book is very useful and is a great addition to any C++ programmers book shelf. The books written by Scott Meyers contain some very esoteric knowledge about C++, and can help you think outside of the box and also solve odd issues with your code.