Programming specifications-programmers should write code like this

Source: Internet
Author: User
Tags constant definition goto header

Basic requirements

1.1 Program Structure analysis, easy to understand, a single function of the number of programs can not exceed 100 lines.

1.2 What to do, to be simple, straightforward when the code is streamlined to avoid garbage programs.

1.3 Use standard library functions and public functions as much as possible.

1.4 Do not arbitrarily define global variables, try to use local variables.

1.5 use parentheses to avoid ambiguity.

2. Readability Requirements

2.1 Readability first, efficiency second.

2.2 Keep the annotation exactly the same as the code.

2.3 Each source program file, there are file header description, specifications see specification.

2.4 Each function, there is a function header description, specifications see specification.

2.5 When a primary variable (struct, union, class, or object) is defined or referenced, the annotation reflects its meaning.

The 2.7 constant definition (DEFINE) is described accordingly.

2.8 Each stage of the processing process has a description of the relevant annotation.

2.9 is commented before the typical algorithm.

2.10 Use indentation to display the logical structure of the program, the indentation is consistent and in the TAB key, the tab is defined as 6 bytes.

2.11 Cycle, branch level not more than five layers.

2.12 Comments can be on the same line as the statement, or on the uplink.

2.13 Blank lines and whitespace characters are also a special annotation.

2.14 at a glance the statement is not annotated.

2.15 Annotations can be scoped to: definitions, references, conditional branches, and a piece of code.

2.16 Note the number of lines (excluding the program headers and function header descriptions) should be 1/5 to 1/3 of the total number of rows.

3. Structural Requirements

3.1 Prohibit the occurrence of two equivalent slip lines.

3.2 Prohibit goto statements.

3.3 Use an IF statement to emphasize that only one group of two sets of statements is executed. No else GOTO and else return are prohibited.

3.4 Use case to implement Multiplex branch.

3.5 Avoid multiple exits from circulation.

The 3.6 function has only one outlet.

3.7 Do not use conditional assignment statements.

3.8 Avoid unnecessary branches.

3.9 Do not easily replace the logical expression with the conditional branch.

4. Correctness and fault tolerance requirements

4.1 Program first is correct, followed by graceful

4.2 Can not prove that your program is not wrong, so after writing a program, you should go back to check.

4.3 When you change an error, you may have a new error, so consider the impact on other programs before you modify it.

4.4 All variables must be initialized before they are called.

4.5 For all user input, must be checked for legality.

4.6 Do not compare the equality of floating-point numbers,

such as: 10.0 * 0.1 = 1.0, unreliable

4.7 When the program is related to the environment or the state, it must take the initiative to deal with the unexpected event, such as whether the file can be locked logically, the printer is online, etc.

4.8 Unit tests are also part of programming, and the program that submits the test must pass the unit test.

Related Article

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: