The Ten Commandments for C++ Programmers
| 22nd July 2010 |
From the book “Practical C++ Programming” by Steve Oualline, The Ten Commandments for C++ Programmers written by Phin Straite.
1. Thou shalt not rely on the compiler default methods for construction, destruction, copy construction, or assignment for any but the simplest of classes. Thou shalt forget these “big four” methods for any nontrivial class.
2. Thou shalt declare and define thy destructor as virtual such that others may become heir to the fruits of your labors.
3. Thou shalt not violate the “is-a” rule by abusing the inheritance mechanism for thine own twisted perversions.
4. Thou shalt not rely on any implementation-dependent behavior of a compiler, operating system, or hardware environment, lest thy code be forever caged within that dungeon.
5. Thou shalt not augment the interface of a class at the lowest level without most prudent deliberation. Such ill-begotten practices imprison thy clients unjustly into thy classes and create unrest when code maintenance and extension are required.
6. Thou shalt restrict thy friendship to truly worthy contemporaries. Beware, for thou art exposing thyself rudely as from a trenchcoat.
7. Thou shalt not abuse thy implementation data by making it public or static except in the rarest of circumstances. Thy data are thine own; share it not with others.
8. Thou shalt not suffer dangling pointers or references to be harbored within thy objects. They are nefarious and precarious agents of random and wanton destruction.
9. Thou shalt make use of available class libraries as conscientiously as possible. Code reuse, not just thine own but that of thy clients as well, is the Holy Grail of OO.
10. Thou shalt forever forswear the use of the vile printf/scanf, rather favoring the flowing streams. Cast off thy vile C cloak and partake of the wondrous fruit of flexible and extensible I/O.



#3,#7,and#9 are not C++-specific. Just plain ol’ oop.
#7: ‘struct’ is ‘class’ for public data members
#8 applies to object references in general (therefore java, etc.). Dangling pointer are trouble in non-oo languages as well (C, Pascal, etc).
#1: occasionally the default impls are fine. Just know what you’re saying (see: Meyers, Saks).
It’s a tricky and maligned language for sure. Not sure that the convoluted English or the “10 commandments” format will win over any new “converts” however.
[Reply]
Kevin Reply:
July 28th, 2010 at 7:57 pm
Thanks for your comment, stevej. You have mentioned valid points there.
This list of the ten commandments for C++ programmers has been taken from the “Practical C++ Programming” book.
The purpose of these points is not to have C++ converts. Rather it provides some basic points that C++ programmers could trip over and so should keep in mind while C++ programming.
[Reply]