Home / The Ten Commandments for C++ Programmers
I don’t know who wrote this. If you do, please tell me so I can give
credit.
- Thou shalt not rely solely on the compiler-generated default methods
for construction, destruction, copy construction, or assignment for
any but the simplest of classes. Thou shalt scrupulously forge these
"foremost four" methods for any non-trivial class, yea, even though thy
implementation be but trivial. Further, thou shalt declare and define
thy destructor as virtual such that others may become heir to the fruits
of your labors with ease.
- Thou shalt not violate the ‘is-a’ rule by abusing the inheritance
mechanism for thine own twisted perversions. Denounce with vigor the
primordial OO publications wherein this sinful behavior was epitomized.
- Thou shalt not rely on any implementation-dependent behavior of a
compiler, operating system, nor hardware environment, lest your code be
forever caged within that dungeon whilst others’ code scampers
unfettered through architectures as yet undreamt of.
- Thou shalt not augment the interface of a class at the "concrete" level
without most prudent deliberation. Such ill-begotten practices imprison thy
clients unjustly into your classes, and foment unspeakable unrest when code
maintenance and extension are required.
- Thou shalt restrict thy friendship to truly worthy contemporaries.
Beware, for thou art exposing thyself wellnigh as rudely as from a trench
coat.
- Thou shalt not abuse your implementation data by making it public or
static except in the rarest of circumstances. Thy bits are thine own; share
them not with others.
- Thou shalt not suffer dangling pointers or references to be harbored
within your objects. These are nefarious and precarious agents of random and
wanton destruction. While ye may escape their wrath, thy fellow programmer
may verily be ravaged by them.
- Thou shalt make use of available class libraries as conscientiously as
possible. Code reuse, not just thine own but that of your clients as well,
is the holy grail of OO. A seemly set of class libraries will put you on a
path where you may be enlightened.
- 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 IO.
- Thou shalt avoid designing "big" classes--classes with both an unseemly
abundance of instance data and tortuosly complicated interfaces. Such
accursed classes are the unclean and unwieldy result of poor design. Be
chastened, seek truth, and design again, that thy userid may be enshrined
among the enumeration of the most purely virtuous.