C++ Coding Standard
Purpose To guide implementation of C++ programs
Program Headers Begin all programs with a descriptive header.
Header Format /******************************************************************/
/* Program Assignment: the program number */
/* Name: your name */
/* Date: the date you started developing the program */
/* Description: a short description of the program and what it does */
/******************************************************************/
Listing Contents Provide a summary of the listing contents
Contents /******************************************************************/
Example /* Listing Contents: */
/* Reuse instructions */
/* Modification instructions */
/* Compilation instructions */
/* Includes */
/* Class declarations: */
/* CData */
/* ASet */
/* Source code in c:/classes/[Link]: */
/* CData */
/* CData() */
/* Empty() */
/******************************************************************/
(continued)
C++ Coding Standard (continued)
Reuse - Describe how the program is used: declaration format, parameter values, types,
Instructions and formats.
- Provide warnings of illegal values, overflow conditions, or other conditions that
could potentially result in improper operation.
Reuse Instruction /******************************************************************/
Example /* Reuse instructions */
/* int PrintLine(char *line_of_character) */
/* Purpose: to print string, ‘line_of_character’, on one print line */
/* Limitations: the line length must not exceed LINE_LENGTH */
/* Return 0 if printer not ready to print, else 1 */
/******************************************************************/
Identifiers Use descriptive names for all variable, function names, constants, and other
identifiers. Avoid abbreviations or single-letter variables.
Identifier Int number_of_students; /* This is GOOD */
Example Float: x4, j, ftave; /* This is BAD */
Comments - Document the code so the reader can understand its operation.
- Comments should explain both the purpose and behavior of the code.
- Comment variable declarations to indicate their purpose.
Good Comment If(record_count > limit) /* have all records been processed? */
Bad Comment If(record_count > limit) /* check if record count exceeds limit */
Major Sections Precede major program sections by a block comment that describes the processing
done in the next section.
Example /******************************************************************/
/* The program section examines the contents of the array ‘grades’ and calcu- */
/* lates the average class grade. */
/******************************************************************/
Blank Spaces - Write programs with sufficient spacing so they do not appear crowded.
- Separate every program construct with at least one space.
Indenting - Indent each brace level from the preceding level.
- Open and close braces should be on lines by themselves and aligned.
Indenting while (miss_distance > threshold)
Example {
success_code = move_robot (target _location);
if (success_code == MOVE_FAILED)
{
printf(“The robot move has failed.\n”);
}
}
Capitalization - Capitalize all defines.
- Lowercase all other identifiers and reserved words.
- To make them readable, user messages may use mixed case.
Capitalization #define DEFAULT-NUMBER-OF-STUDENTS 15
Examples int class-size = DEFAULT-NUMBER-OF-STUDENTS;