Programming Style

来源:互联网 发布:阿里云centos图形界面 编辑:程序博客网 时间:2024/05/17 21:47

Names

Precept:
Always name your classes, variables and functions with the greatest care, and explain them thoroughly.

Guidelines:

  1. Keep the names simple for variables which are used only briefly and locally;For a controlling a for loop, it is often possible to find a short but meaningful word that better describes the use of the variable.
  2. Avoid deliberate misspelling and meaningless suffixes to obtain different names.*
  3. Be careful int the use of the letter”l”(small ell), “O”(capital oh), and “0”(zero). both “I” and “O” should never be used alone as names.

Documentation and Format

Precept:
Keep your documentation concise but descriptive.

Guidelines:

  1. Place a prologue at the beginning of each function including:
    (a) Identification (programmer’s name, date, version number).
    (b) Statement of the purpose of the function and algorithm used.
    (c) The changes the function makes and what data it use.
    (d) Reference to further documentation external to the program.
  2. When each variable, constant, or class is declared, explain that what it is and how it is used. BETTER still, make this information evident from the name.
  3. For those significant section(Paragraph or Function ) of the program, it is essential to have a comment stating briefly its purpose or action.
  4. Avoid comments that parrot what the code does, such as count ++; //Increase counter by 1.
    or that are meaningless jargon, such as // Horse string length into correctitude.
  5. Explain any statement that employs a trick or whose meaning is unclear. BETTER still, avoid such statements.
  6. The code itself should explain how the program works. The documentation should explain why it works and what it does.
  7. Whenever a program is modified, be sure that the documentation is correspondingly modified.

Precept
The reading time for programs is much more than the writing time. Make reading easy to do.

Refinement and Modularity

Precept:
1. Don’t lose sight of the forest for its trees.
2. Use classes to model the fundamental concepts of the program.
3. Each lass or function should hide something
** Middle-level managers in a large company do not pass on everything they receive form their departments to their superior; they summarize, collate, and weed out the information, handle many requests themselves, and send on only what is needed at the upper levels.

Guidelines about parameters

  1. Input Parameters
  2. Output Parameters
  3. Inout parameters
  4. Local Variables
  5. Global Variablesf
0 0