Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous
Up: NOTE 167 AIPS++ Programming: STANDARDS AND GUIDELINES Previous: Documentation and Naming

Subsections


Coding

Forward Declarations

#include only those header files your class absolutely requires. Use forward declarations when that will suffice.14

Protected Data Members (*)

Avoid protected data members, using protected member functions for access to private data instead.

Access to Private Data

Do not return pointers or references to private data, except to accomodate Fortran or C libraries, or if efficiency absolutely requires it.15

Label All Virtual Functions Explicitly

Virtual functions in a derived class shall be explicitly labelled with the virtual keyword in the class declaration, even though this is not required by the language.

Document Loose Ends

Indicate unfinished or questionable code with the documentation extractor's tag <todo>.

Standard Class Member Functions

Provide definitions for all of the standard member functions which the compiler would (otherwise) generate automatically: default constructor16, destructor17, copy constructor, and the assignment operator. For any of these which you really wish to disallow, declare them private, and create minimal implementations (if any at all). For example:
   ...
   private:
      SomeClass () {;}   // disable the default constructor
   ...

The Order of Function Arguments

Functions shall be declared so that their arguments (parameters) appear in this order: output parameters first; input/output parameters next; input parameters last.18 Functions which return only one value should usually use the function's return-value to return that value, rather than add an additional output parameter to the parameter list. If a function modifies its single argument in place, then that argument is best understood as an input/output argument.19

Formal Arguments and Default Parameters

Specify formal arguments and default parameters in the function declaration; use the same names in the function definition.20

One argument constructors

Constructors with one argument should be made explicit, or have an explanation why automatic conversion is wanted (as an example: Unit(String) is an obvious candidate for automatic conversion, to be able to write e.g. ``km/s'' rather than Unit(``km/s'')).

Return Types

Specify explicit return types for all functions. Return an appropriate integer value from main.

const Functions

All functions that do not change the state of the class should be declared const. If only the ``hidden'' state (like a cache) changes, the function should be declared const, but with a mutable cache variable. mutable should be used carefully.

Compound Statements

Multi-line compound statements should have braces. E.g.
if (condition) {
   statement1;
} else {
   statement2;
};

Template arguments

Minimize the number of template arguments by utilising the typename construct for user classes, and the traits methodology for fundamental types.21

Casting

Only C++ style casting is allowed. The use of reinterpret_cast is not allowed. Allowed are: static_cast, const_cast (but be aware of what you are doing) and dynamic_cast.

Macros

(*) Macros (especially #if type, should be limited to the include guard. Other cases should be checked with the QAG, who often can point out other solutions.

Exceptions

Exceptions should be reserved for truly exceptional circumstances, and not used to replace status flags or condition tests. Exceptions should be documented with the <thrown> tag (and maybe with the thrown declaration specifier).

Compiler Warnings

All code should compile without producing warnings from the compiler. Most compilers do not produce many spurious warnings any more.


next up previous
Up: NOTE 167 AIPS++ Programming: STANDARDS AND GUIDELINES Previous: Documentation and Naming
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15