溪边树

象一棵树,栽在溪水旁。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Good Programming Practice--Chapter2

Posted on 2010-01-21 14:39  溪边树  阅读(264)  评论(0编辑  收藏  举报

All the following words are from the book "C++ How to Program, Fifth Edition", H.M. Deitel. Any commercial using is firmly denied.

Good Programming Practice 2.1

Every program should begin with a comment that describes the purpose of the program, author, date and time.

Good Programming Practice 2.2

Use blank lines and space characters to enhance program readability

Good Programming Practice 2.3

Many programmers make the last character printed by a function a new line (\n). This ensures that the function will leave the screen cursor positioned at the beginning of a new line. Conventions of this nature encourage software reusability -- a key goal in software development.

Good Programming Practice 2.4

Indent the entire body of each function one level within the braces that delimit the body of the function. This makes a program's functional structure stand out and helps make the program easier to read.

Set a convention for the size of indent you prefer, then apply it uniformly. The tab key may be used to create indents, but tab stops may vary. We recommend using either 1/4-inch tab stops or (preferably) three spaces to form a level of indent.

Good Programming Practice 2.5

Place a space after each comma (, ) to make programs more readable. For example, int number1, number2, sum;

Some programmers prefer to declare each variable on a seperate line. This format allows for easy insertion of a descriptive comment next to each declaration.

Good Programming Practice 2.6

Choosing meaninful identifiers helps make a program self-documenting -- a person can understand the program simply by reading it rather than having to refer to manuals or comments.

Avoid using abbreviations in identifiers. This promotes program readabiligy.

Good Programming Practice 2.7

Avoid identifiers that begin with underscores and double underscores, because C++ compilers may use names like that for their own purposes internally. This will prevent names you choose from being confused with names the compilers choose.

Good Programming Practice 2.8

Always place a blank line between a declaration and ajacent executable statements. This makes the declarations stand out in the program and contributes to program clarity.

If you prefer to place declarations at the beginning of a function, separate them from the executable statements in that function with one blank line to highlight where the declarations end and the executable statements begin.

Good Programming Practice 2.9

Place spaces on either side of a binary operator. This makes the operator stand out and makes the program more readable. For example,

sum = number1 + number2;

cout << "Sum is " << sum << endl;

But, the unary increment (++) or decrement operators should be palced next to their operands, with no intervening spaces.

Good Programming Practice 2.10

Using redundant parentheses in complex arithmatic expressions can make the expressions clearer. For example, instead of using

y = a * x * x + b * x + c;

we use: y = ( a * x * x ) + ( b * x ) + c;  

Good Programming Practice 2.11

A lengthy statement may be spread over several lines. If a single statement must be split across lines, choose meaningful breaking points, such as after a comma in a comma-separated list, or afteran operator in a lengty expression. If a statement is split across tow or more lines, indent all subsequent lines and left-align the group.