Maintaining C++ Codes

Some Ground rules:
1. Keep class data members private.

2. About global name space:
– Avoid data with external linkage at file scope: put all global variables in a structure, class, namespace, etc. This is to avoid name collison.
– Avoid free functions at file scope in .h files, avoid free functions with external linkage in .c files.
– Avoid enumerations, typedefs, and constants at file scope in .h files.
– Avoid using preprocessor macros in .h files, except as include guards.
– Only classes, structures, unions, free operator functions should be declared at file scope in a .h file; only classes, structures, unions, and inline functions should be defined at file scope in a .h file.

3. Place a unique and predictable include guard around the contents of each header file.

4. Place a redundant include guard around each preprocessor include directive in every header file.

5. Documentation the interfaces so that they are usable by others; have at least one other developer review each interface.

6. Use a consistent method to highlight class data member, const, statics. E.g.  adding a prefix d_ to class data members, or s_ for static data, ALL CAPS for const.

7. Don’t use “using namespace” directive in header files. It forfeit the purpose of having namespaces.

taken from: Large Scale C++ Software Design by John Lakos, 1996. Thinking in C++, 2nd Edition.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: