# A Coding Convention for C++ Code

This document was borrowed and slightly modified from the Berkeley Systems, Inc. coding conventions. If you're looking for something more comprehensive, try the Ellemtel C++ Rules and Recommendations:

- HTML, one page, 146K
- Postscript, 385K

See also, Christopher Lott's collection of C and C++ style guides and the Mozilla portability guide.

## Purpose

To make it easier for us to read each other's code, here are a few guidelines to follow when writing C++ code.

## Definitions

- Member functions are functions that are associated with classes.
- Data members are variables that are part of classes.

## Identifier Names

- All variable and constant identifiers begin with a lowercase letter.
- Global variables begin with the (for example, theWindows).
- Constants begin with lowercase c (for example, cMaxWindows).
- Class data members begin with its (for example, itsCanvas).
- Static data members begin with their (for example, theirTotal).
- Both member and non-member functions begin with an uppercase letter (for example, TextStyle).
- Names of classes begin with an uppercase letter (for example, Canvas).

## Class Declarations

Classes are declared in the following order: public member functions, protected member functions, private member functions, protected data members, and private data members. No data members may be declared public. For example:

    class Application {
    public:
        Application();
        ~Application();
        void Run();
    protected:
        void EventLoop();
    private:
        EventRecord itsLastMouseEvent;
    }

## Control Structures

The control structures are as follows:

    if (expression) {
        statements;
    } else {
        statements;
    }

    for (expression; expression; expression) {
        statements;
    }

    do {
        statements;
    } while (expression);

    while (expression) {
        statements;
    }

    switch (expression) {
    case constant:
        statements;
        break;
    default:
        statements;
        break;
    }

    /*
     * long comments which need more than one line use
     * this block comment style, if you're writing C
     */

    //
    // if you're writing C++, your block comments should look more
    // like this -- it's ugly to mix commenting styles
    //

    statement;           /* short C comments */
    statement;           // short C++ comments

## File Names

C++ source files use the suffix .cc. C++ header files use the suffix .h.

Last modified: Fri Oct 15 15:14:12 CDT 1999