Programming in C++: Rules and Recommendations

Copyright (C) 1990-1992 by

Ellemtel Telecommunication Systems Laboratories

For copyright information, see the home page.

20 Summary of Rules

Rule 0 Every time a rule is broken, this must be clearly documented.

Rule 1 Include files in C++ always have the file name extension ".hh".

Rule 2 Implementation files in C++ always have the file name extension ".cc".

Rule 3 Inline definition files always have the file name extension ".icc".

Rule 4 Every file that contains source code must be documented with an introductory comment that provides information on the file name and its contents.

Rule 5 All files must include copyright information.

Rule 6 All comments are to be written in English.

Rule 7 Every include file must contain a mechanism that prevents multiple inclusions of the file.

Rule 8 When the following kinds of definitions are used (in implementation files or in other include files), they must be included as separate include files:

Rule 9 Definitions of classes that are only accessed via pointers (*) or references (&) shall not be included as include files.

Rule 10 Never specify relative UNIX names in #include directives.

Rule 11 Every implementation file is to include the relevant files that contain:

Rule 12 The identifier of every globally visible class, enumeration type, type definition, function, constant, and variable in a class library is to begin with a prefix that is unique for the library.

Rule 13 The names of variables, constants, and functions are to begin with a lowercase letter.

Rule 14 The names of abstract data types, structures, typedefs, and enumerated types are to begin with an uppercase letter.

Rule 15 In names which consist of more than one word, the words are written together and each word that follows the first is begun with an uppercase letter.

Rule 16 Do not use identifiers which begin with one or two underscores (`_' or `__').

Rule 17 A name that begins with an uppercase letter is to appear directly after its prefix.

Rule 18 A name that begins with a lowercase letter is to be separated from its prefix using an underscore (`_').

Rule 19 A name is to be separated from its suffix using an underscore (`_').

Rule 20 The public, protected, and private sections of a class are to be declared in that order (the public section is declared before the protected section which is declared before the private section).

Rule 21 No member functions are to be defined within the class definition.

Rule 22 Never specify public or protected member data in a class.

Rule 23 A member function that does not affect the state of an object (its instance variables) is to be declared const.

Rule 24 If the behaviour of an object is dependent on data outside the object, this data is not to be modified by const member functions.

Rule 25 A class which uses "new" to allocate instances managed by the class, must define a copy constructor.

Rule 26 All classes which are used as base classes and which have virtual functions, must define a virtual destructor.

Rule 27 A class which uses "new" to allocate instances managed by the class, must define an assignment operator.

Rule 28 An assignment operator which performs a destructive action must be protected from performing this action on the object upon which it is operating.

Rule 29 A public member function must never return a non-const reference or pointer to member data.

Rule 30 A public member function must never return a non-const reference or pointer to data outside an object, unless the object shares the data with other objects.

Rule 31 Do not use unspecified function arguments (ellipsis notation).

Rule 32 The names of formal arguments to functions are to be specified and are to be the same both in the function declaration and in the function definition.

Rule 33 Always specify the return type of a function explicitly.

Rule 34 A public function must never return a reference or a pointer to a local variable.

Rule 35 Do not use the preprocessor directive #define to obtain more efficient code; instead, use inline functions.

Rule 36 Constants are to be defined using const or enum; never using #define.

Rule 37 Avoid the use of numeric values in code; use symbolic values instead.

Rule 38 Variables are to be declared with the smallest possible scope.

Rule 39 Each variable is to be declared in a separate declaration statement.

Rule 40 Every variable that is declared is to be given a value before it is used.

Rule 41 If possible, always use initialization instead of assignment.

Rule 42 Do not compare a pointer to NULL or assign NULL to a pointer; use 0 instead.

Rule 43 Never use explicit type conversions (casts).

Rule 44 Do not write code which depends on functions that use implicit type conversions.

Rule 45 Never convert pointers to objects of a derived class to pointers to objects of a virtual base class.

Rule 46 Never convert a const to a non-const.

Rule 47 The code following a case label must always be terminated by a break statement.

Rule 48 A switch statement must always contain a default branch which handles unexpected cases.

Rule 49 Never use goto.

Rule 50 Do not use malloc, realloc or free.

Rule 51 Always provide empty brackets ("[]") for delete when deallocating arrays.

Next section: Summary of Recommendations

Robert M. Adams' Home Page