March 13, 1999
| 1 | Comments |
| 1.1 | File Headers |
| 1.2 | Class Declarations |
| 1.3 | Function Definition Separators |
| 1.3.1 | Function Definitions |
| 1.3.2 | Member Function Definitions |
| 1.3.3 | Member Function Groups |
| 2 | Declarations |
| 2.1 | Variables |
| 2.2 | Constants |
| 2.3 | Functions |
| 2.4 | User Defined Types |
| 2.4.1 | Enumerated |
| 2.4.2 | Structures |
| 2.4.3 | Classes |
| 3 | Constructs |
| 3.1 | if - else - else if |
| 3.2 | do - while |
| 3.3 | switch - case - default |
| 3.4 | for |
| 4 | Indentation |
| 5 | Header Files |
| 6 | Code Reviews |
//////////////////////////////////////////////////////////////////////////////// // file : string.hpp // // dumping ground for string utilities // // Author Date Description // ------ ---- ----------- // D. Bowman 5/22/94 Creation //////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// a simple class declaration
//
class CSimple {
int
m_iNumber;
}; // end class Simple
////////////////////////////////////////////////////////////////////////////////
// another simple class declaration
//
class CSimple2 {
int
m_iNumber;
}; // end class Simple
////////////////////////////////////////////////////////////////////////////////
void
fnLower(char *pchString)
{
for (int iIndex = 0; pchString[iIndex]; iIndex++) pchString[iIndex] =
tolower(pchString[iIndex]);
} // end fnLower()
////////////////////////////////////////////////////////////////////////////////
void
fnUpper(char *pchString)
{
for (int iIndex = 0; pchString[iIndex]; iIndex++) pchString[iIndex] =
toupper(pchString[iIndex]);
} // end fnUpper()
//########################################################################-- protected member function separator--
//************************************************************************-- public member function separator--
//========================================================================
template <class DATA>
CList<DATA>::CList()
{
m_pData = 0;
m_pHead = 0;
m_pNode = 0;
m_pTail = 0;
m_bFailed = 0;
} // end CList::CList()
//========================================================================
template <class DATA>
CList<DATA>::~CList()
{
m_fnFirst();
while (m_fnRemove());
} // end CList::~CList()
int iDay_Of_Week, // encouraged iDayOfWeek; // discouragedPrefix name declarations with the following sequence. Write pointer names with the letter 'p' followed by the type prefix.
| type | prefix |
| char | ch |
| unsigned char | uch |
| char buffer | sz |
| short int | si |
| int | i |
| long | l |
| long int | li |
| unsigned short int | usi |
| unsigned int | ui |
| unsigned long | ul |
| unsigned long int | uli |
| float | f |
| double | d |
| long double | ld |
| usigned float | uf |
| unsigned double | ud |
| unsigned long double | uld |
| bool | b |
| functions | fn |
| classes | C |
| class templates | C |
| class members | m_ |
| pointers | p |
int iNumber = 0, *piNumber = 0; long int liNumber = 0, *pliNumber = 0; float fNumber = 0.0, *pfNumber = 0; char chLetter, *pchBuffer1 = new char[256], szBuffer2[256];
const int NUMBER = 500, BUFFER_WIDTH = 256;
...
static void
fnLower(char *pchString);
...
void
fnLower(char *pchString)
{
for (int iIndex = 0; pchString[iIndex]; iIndex++) pchString[iIndex] =
tolower(pchString[iIndex]);
} // end fnLower()
...
typedef enum {
STATIONARY,
FREE_FLIGHT,
DIE,
COLLISION
} EVENT;
typedef struct {
float
fx,
fy,
fz;
} VERTEX;
Formally naming a structure is only necessary during a linked list declaration.
The typedef provided name is used thereafter.
typedef struct node {
int
iCount;
char
szBuffer[80];
struct node
*pPrev,
*pNext;
} NODE;
NODE
*pHead = 0,
*pTail = 0;
| bool
m_fnFailed() {return m_bFailed;}; |
m_bFailed is set to true at the beginning of the constructor. It is set to false when the constructor completes the instance initialization. |
| char
*m_fnResult() {return m_pchResult;}; |
m_pchResult points to a message that describes the condition that resulted in failure. This may also be used to indicate other failure results for other public and protected member functions. |
template <class DATA>
class CList {
public:
// public data
DATA
*m_pData;
// public methods
CList();
~CList();
bool
m_fnAppend(DATA *pData),
m_fnFirst(),
m_fnLast(),
m_fnPrevious(),
m_fnNext(),
m_fnRemove();
bool
m_fnFailed() {return m_bFailed;};
char
*m_fnResult() {return m_pchResult;};
private:
// private types
typedef struct node {
DATA
Data;
struct node
*pPrev,
*pNext;
} NODE;
// private data
bool
m_bFailed;
char
*m_pchResult;
NODE
*m_pHead,
*m_pNode,
*m_pTail;
}; // end CList
if (bState) return iSetting; // encouraged
...
if (bState)
return iSetting; // discouraged
...
if (bState) {
cout << "\nNew Settings";
return iSetting;
} // end if
else return 0;
...
if (!m_pNode) return 0;
NODE
*pNode = m_pNode;
// head of list
if (m_pNode == m_pHead) {
// check for null list condition
if (m_pNode == m_pTail) {
m_pTail = 0;
m_pHead = 0;
m_pNode = 0;
} // end if
else {
m_pHead = m_pHead->pNext;
m_pHead->pPrev = 0;
} // end else
} // end if
// tail of list
else if (m_pNode == m_pTail) {
m_pTail = m_pTail->pPrev;
m_pTail->pNext = 0;
} // end if
// middle of list
else {
m_pNode->pPrev->pNext = pNode->pNext;
m_pNode->pNext->pPrev = pNode->pPrev;
} // end else
...
...
while (m_fnRemove());
...
while (m_fnRemove()) cout << "\nremoving node";
...
while (m_fnRemove()) {
cout <<"\nremoving node";
iNode_Count++;
} // end while
...
do {
cout << "\nremoving node";
iNode_Count++;
} while (m_fnRemove()); // end while
...
...
switch (m_chVariable[iIndex]) {
case '\n' :
case ' ' :
case '.' :
case '\\' :
bContinue = 0;
chTerminator = m_chVariable[iIndex];
m_chVariable[iIndex] = '\0';
break;
default:
cout << "\nno match";
} // end switch
...
...
for (int iIndex = 0; iIndex < VARIABLE_ID_RANGE; iIndex++) {
if (fnIs_Equal(m_chVariable, m_chVariable_ID_Text[iIndex])) {
bSuccess++;
m_pout->write(m_chVariable_ID_Value[iIndex],
fnLength_Of(m_chVariable_ID_Value[iIndex]));
break;
} // end if
} // end for
...
Variable declarations within headers are not allowed. This prevents multiply defined symbols.
Conduct code reviews at least every two weeks. Pick a day of the week so that it becomes a habit for all team members. Exclude middle and upper management from the review, unless they have contributed code for review. In that event, show no mercy.