Programming in C++

Table of Contents

Advice for programming in C++

  1. When you program, you create a concrete representation of the ideas in your solution to someproblem. Let the structure of the program reflect those ideas as directly as possible:

    1. If you can think of ‘‘it’’ as a separate idea, make it a class.

    2. If you can think of ‘‘it’’ as a separate entity, make it an object of some class.

    3. If two classes have a common interface, make that interface an abstract class.

    4. If the implementations of two classes have something significant in common, make that commonality a base class.

    5. If a class is a container of objects, make it a template.

    6. If a function implements an algorithm for a container, make it a template function implementing the algorithm for a family of containers.

    7. If a set of classes, templates, etc., are logically related, place them in a common namespace.

  2. When you define either a class that does not implement a mathematical entity like a matrix or acomplex number or a low-level type such as a linked list:

    1. Don’t use global data (use members).
    2. Don’t use global functions.
    3. Don’t use public data members.
    4. Don’t use friends, except to avoid [a] or [c].
    5. Don’t put a ‘‘type field’’ in a class; use virtual functions.
    6. Don’t use inline functions, except as a significant optimization.