关于编程风格和文档的扩展指南

来源:互联网 发布:找工作可靠的软件 编辑:程序博客网 时间:2024/06/08 09:40

Supplement E: Expanded Guidelines on Programming Style and Documentation  

For Introduction to C++ Programming

Y. Daniel Liang

liang@armstrong.edu

Introduction

Programming style deals with the appearance of your program. If you were to write an entire program on one line, it would compile and run properly, but doing this would be bad programming style because the program would be hard to read. Documentation consists of explanatory remarks and comments for the program. Programming style and documentation are as important as coding. Good programming style and appropriate documentation reduce the chance of errors and make programs easy to read. This handout provides more detailed guidelines on programming style and documentation that supplements the brief guidelines presented in Chapter 2, "Primitive Data Types and Operations." First, here is a sample code:

/** * Class: CSCI2490 C++ Programming  * Instructor: Y. Daniel Liang * Description: (Give a brief description for Exercise 1) * Due: 1/18/2001 * @author John F. Smith * @version 1.0 */
#include <iostream>using namespace std;
int main(){  // Display Welcome to C++ to the console  cout << "Welcome to C++!" << endl;  return 0;}

Appropriate Comments and Comment Styles

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. In a long program, you should also include comments to introduce each major step and to explain anything that is difficult to read. It is important to make your comments concise so that you do not crowd the program or make it difficult to read.

For commenting the steps inside a function, use line comments (//).

Naming Conventions

Make sure that the meanings of the descriptive names you choose for variables, constants, classes, and functions are straightforward. Names are case-sensitive. Listed below are the conventions for naming variables, functions, and classes.

·       For variables and functions, always use lowercase. If the name consists of several words, concatenate them into one, making the first word lowercase and capitalizing the first letter of each subsequent word in the name; for example, the variables radius and area and the funciton readDouble.

·       For class names, capitalize the first letter of each word in the name; for example, the class name ComputeArea.

·       All letters in constants should be capitalized, and underscores should be used between words; for example, the constant PI and constant MAX_VALUE.

·       Use singulars for variables representing single items such as student and count. Use plurals for arrays or collections. For example,

      Student students[4]; and Count counts[10];

TIP: It is important to become familiar with the naming conventions. Understanding them will help you to understand C++ programs. If you stick with the naming conventions, other programmers will be more willing to accept your program.

 

TIP: Do not choose class names that are already used in the C++ standard library. For example, since the pow function is defined in C++, you should not name your function pow.

Proper Indentation and Spacing

A consistent indentation style makes programs clear and easy to read. Indentation is used to illustrate structural relationships among the program’s components or statements. The C++ compiler can read the program even if all of the statements are in a straight line, but it is easier to read and maintain code that is aligned properly. You should indent each subcomponent or statement two spaces more than the structure within which it is nested.  

Use a space to separate parameters in a function. Do not leave spaces  before or after parentheses in a function. For example, aFunction(a1, a2) is preferred, whereas aFunction( a1, a2 ) is not a good style.

A single space should be added on both sides of a binary operator, as shown in the following statement:

boolen b = 3 + 4 * 4 > 5 * (4 + 3) - ++i;

A single space line should be used to separate segments of the code to make the program easier to read.

Block Styles

A block is a group of statements surrounded by braces. A block can be written in many ways. For example, the following are equivalent:

int main(){  // Display Welcome to C++ to the console  cout << "Welcome to C++!" << endl;  return 0;}
 
int main() {  // Display Welcome to C++ to the console  cout << "Welcome to C++!" << endl;  return 0;}

The former is referred to as the next-line style, and the latter, as the end-of-line style. The next-of-line block style is used for C++ programs, while the next-line style is often preferred for Java programs.

if-else Style

  if (cond1) 
  {
    // Statements
  }
  else if (cond2) 
  {
    // Statements
  }
  else if (cond3) 
  {
    // Statements
  }
  else 
  {
    // Statements
  }

for loop Style

  for (int i = 1; i < n; i++) 
  {
    // Statements
  }

while loop Style

  while (i < n) 
  {
    // Statements
  }

do-while loop Style

  do 
  {
    // Statements
  } while (i < n);

try-catch Style

  try 
  {
    // Statements
  } 
  catch (Exception ex)
  {
    // Handler
  } 

Summary

  • Use line comments to describe steps inside a function.
  • Indent your statement two spaces.
  • Use Next-Line style.
  • Leave a blank line before a comment line or a comment paragraph.
1 0