"High quality Programming Guide C + + language" Lin Rui, Han Yongquan Summary 1

Source: Internet
Author: User

Chapter 1th Structure of the document

1. The header file consists of three parts:

(1) Copyright and version statement at the beginning of the header file.

(2) Preprocessing block.

(3) Functions and class structure declarations, and so on.

Rule 1. In order to prevent the header file from being repeatedly referenced, the preprocessing block should be produced using the IFNDEF/DEFINE/ENDIF structure.

Rule 2. Use the #include <filename.h> format to reference the header file of the standard library (the compiler will start searching from the standard library directory).

Rule 3. #include the "filename.h" format to reference the header file of a nonstandard library (the compiler will start the search from the user's working directory).

recommendation 1. Only "declarations" are stored in the header file without the "definition".

recommendation 2. Do not advocate the use of global variables, try not to appear in the header file like extern int value such declarations.

For example, code:

#ifndef graphics_h//prevent Graphics.h from being repeatedly referenced

#define Graphics_h

#include <math.h>//header file referencing the standard library

...

#include "myheader.h"//header file referencing non-standard libraries

...

void Function1 (...); global function declaration

...

Class Box//struct declaration

{

...

};

#endif

2. The definition file has three parts:

(1) Define the copyright and version statement at the beginning of the file.

(2) A reference to some header files.

(3) The implementation body of the program (including data and code).

#include "graphics.h"//reference header file

...

The implementation body of the global function

void Function1 (...)

{

...

}

The implementation body of the class member function

void Box::D raw (...)

{

...

}

The 2nd chapter the layout of the program

1. Blank line: a blank line plays the role of separating the program paragraphs. An empty line (not too much or less) will make the layout of the program clearer. Empty lines do not waste memory, although printing a program that contains blank lines consumes a little more paper, but it is worth it. So don't be reluctant to use a blank line.

Rule 1. After each class declaration, a blank line is added after the end of each function definition.

Rule 2. In a function body, there is no blank line between the closely related statements on the IBO, and the other places should be separated by a blank line.

2. Line of code:

Rule 1. One line of code is one thing, such as defining only one variable, or just writing a single statement. Such code is easy to read and easy to write comments.

Rule 2: If, for, while, do, and so on a single line, the execution statement must not follow immediately thereafter. Add {} No matter how many statements are executed. This will prevent writing errors.

Recommendation: initialize the variable as much as possible while defining the variable (nearest principle) if the reference to the variable is far apart from its definition, the initialization of the variable is easily forgotten. If you reference a variable that is not initialized, you may cause a program error. This recommendation can reduce the risk.

3. Spaces within the line of code:

Rule 1. Leave a blank after the keyword. Like const, virtual, inline, case and so on at least one space after the keyword, otherwise cannot discriminate keywords. After keywords like if, for, and while, leave a space followed by the left Parenthesis ' (') to highlight the keyword.

Rule 2. Do not leave spaces after the function name, followed by the opening parenthesis ' (', to distinguish it from the keyword.

Rule 3. ' (' Back immediately, ') ', ', ', '; ' Move forward, followed by a blank space.

rule 4. ', ' after which you want to leave a blank lattice, such as function (x, y, z). if '; ' is not a line ending symbol, followed by a blank lattice, such as for (initialization; condition; update).

Rule 5. Assignment operators, comparison operators, arithmetic operators, logical operators, bit field operators, such as "=", "+ =" ">=", "<=", "+", "*", "%", "&&", "| |", "<<" , "^" and other two-dollar operators should be preceded by a space.

rule 6. Unary operators such as "!", "~", "+ +", "--", "&" (address operator) are not preceded by spaces.

Rule 7: operators such as "[]", ".", "and" are not preceded by spaces.

Recommendation: for statements with long expressions and if statements, some spaces, such as for (i=0; i<10; i++) and if ((a<=b) && (C<=d)), can be suitably removed for the sake of compactness.

4. Alignment:

Rule 1. The delimiter ' {' and '} ' of the program should have a single row and be in the same column, with the statement that references them left-justified.

Rule 2. Code blocks within {} are left-aligned at the right-hand side of ' {'.

5. Long line splitting:

Rule 1. The maximum length of the code line should be within 70 to 80 characters. The line of code should not be too long, otherwise the eyes can not see, it is not easy to print.

Rule 2. Long expressions are split into new rows at low-priority operators, and the operators are placed at the top of the new line (so that the operator is highlighted). The new rows to be split are indented appropriately, making the layout tidy and the statements readable.

6. The position of the modifier:

rule: you should place modifiers * and & and close to variable names.

7. Notes:

The comment for the C language is "/*...*/". In the C + + language, the comments of the block are often "/*...*/", and the line comments are generally "/...". Annotations are typically used to:

(1) version, copyright notice;

(2) function interface description;

(3) Important lines of code or paragraph hints.

Although annotations can help you understand your code, be careful not to use annotations too much.

Rule 1. A comment is a "hint" to the code, not a document. The notes in the program are not distracting and the comments are too many to dazzle. There are fewer tricks to note.

Rule 2. If the code is clear, you do not have to comment. Otherwise superfluous, boring.   e.g. i++; I plus 1, extra notes

Rule 3: Write code side comments, modify the code at the same time to modify the corresponding comments to ensure that the comments and code consistency. Comments that are no longer useful are to be deleted.

rule 4. Comments should be accurate and understandable to prevent the annotation from having two semantics. Wrong comments are not only useless but harmful.

Rule 5. Try to avoid abbreviations in comments, especially those that are not commonly used.

rule 6. The location of the note should be adjacent to the code described, which can be placed above or to the right of the code and not placed below.

Rule 7. When the code is longer, especially with multiple nesting, it should be filled at the end of some paragraphs for readability.

8. Layout of the class:

Classes can encapsulate data and functions, where functions represent the behavior (or service) of a class. The protected class provides the keyword public, the private, and private, respectively, to declare which data and functions are publicly, protected, or proprietary. This can achieve the purpose of information hiding, that is, to let the class only expose what must be known to the outside world, and hide everything else.

There are two main ways to layout a class:

(1) write the private type of data in front, and write the function of the public type behind.

Programmers who use this layout argue that the design of the class is "data-centric" and focuses on the internal structure of the class.

(2) write the function of the public type in front, and write the data of the private type behind. Programmers in this format argue that the design of a class is "behavior-centric", with a focus on what interfaces (or services) a class should provide.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"High-quality Programming Guide C + + language" Lin Rui, Han Yongquan summary 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.