How to write code to make it high efficiency

Source: Internet
Author: User
Tags comments

First, typesetting:
1. Add the appropriate space between the keyword and the operator.
2. Add a blank line between the block and the blocks
3. Longer statements, expressions, etc., are divided into multiple lines of writing.
4. The new line to be adjusted to fit the indentation, so that the layout is neat, the statement is readable.
5. Long expressions to divide a new line at a low-priority operator, the operator is placed at the top of the new line.
6. If there are long expressions or statements in the circular, judgment and other statements, then the adaptation is to be divided.
7. If the parameters in the function or process are longer, appropriate partitioning is necessary.
8. It is not allowed to write multiple phrase sentences in one line, that is, a single statement is written on a line.
9. The beginning of a function or procedure, the definition of a structure, and the code in a loop, judgment, and so on, are indented in style. The
10.c/c++ language is a block of blocks defined with curly braces ' {' and '} ', when you write a program block ' {' and '
'} ' should have exclusive lines and be in the same column, and align to the left of the statement that references them. The beginning of the function body
, the definition of the class, the definition of the structure, the definition of the enumeration, and the procedures in the If, for, do, while,
switch, case statements are indented as above.

Second, the note
1. Note to be simple and clear.
2. Write the code side comment, modify the code at the same time to modify the corresponding comments to ensure that the comments and code consistency.
3. Where necessary, note the amount of comments to be moderate. The contents of the notes should be clear, concise and accurate, and prevent
Annotation ambiguity. Keep the comment adjacent to the code it describes, which is the nearest principle of the annotation.
4. Comments on the code should be placed in the adjacent position above it, not placed below.
5. Comments on the data structure should be placed in the adjacent position above it, not placed below; For each domain in the structure
Note should be placed to the right of this field, and comments from different fields in the same structure will be aligned.
6. Comments on variables and constants should be placed on the top or right side of them.
7. Global variables should have a more detailed comment, including its function, range of values, which functions or procedures to access it
and instructions on how to access them.
8. The header of each source file must have the necessary annotation information, including: file name; version number; author; build
A description of a module function, such as the function, the main algorithm, the relationship between the internal parts, the file and its
File relationships, etc.), the main function or process list, and the history of this document modification records.
9. Before each function or procedure, you need to have the necessary annotation information, including: function or procedure name; function description
Description of input, output, and return values, invocation relationships, description of called relationships, and so forth.

Third, the name
1. Shorter words can be shortened by removing "vowels";
2. Longer words take precedence over the first few characters of a word, and use parentheses to clarify the order of the expressions to avoid
Avoid using the default priority.
3. Use of the Hungarian notation

Four, readability
1. Avoid the use of difficult-to-understand figures, with meaningful logos instead.
2. Do not use difficult-to-understand high-skill sentences.
3. The more closely related code in the source program should be as contiguous as possible.

Five, variable
1. Remove unnecessary public variables.
2. Constructs only one module or function that can be modified, created, and the rest of the public
variables that are accessible only to modules or functions, to prevent the phenomenon that multiple different modules or functions can be modified and create the same public variable.
3. Carefully define and clarify the meaning, function, value range and the relationship between public variables.
4. Identify the relationships between public variables and functions or procedures that manipulate this public variable, such as access, modification, and creation.
5. When passing data to a public variable, be very careful to prevent the occurrence of an unreasonable value or an out-of-bounds phenomenon.
6. Prevent local variables from having the same name as public variables.
7. Carefully design the layout and arrangement of elements in the structure, so that the structure is easy to understand, save space, and reduce the use of less
caused by misuse.
8. The design of the structure should consider forward compatibility and future version upgrades as much as possible, and leave room for some future applications (such as reserving some space, etc.).
9. Pay attention to the principles and details of the specific language and compiler's handling of different data types.
10. The use of uninitialized variables is strictly prohibited. Initializes variables while declaring them.
11. When programming, be aware of the casts of data types.

Vi. functions, processes
1. The size of the function is limited to 200 lines.
2. A function is best done with only one function.
3. Write functions for simple functions.
4. Function functions should be predictable, that is, the same output should be produced as long as the input data is the same.
5. Try not to write functions that depend on the internal implementation of other functions.
6. Avoid designing multi-parameter functions and removing unused parameters from the interface.
7. Explain in detail the role of each parameter, the range of values and the relationship between the parameters.
8. Check the validity of all parameter inputs for the function.
9. Check the validity of all non-parametric inputs of the function, such as data files, public variables, etc.
10. Function names should accurately describe function functions.
11. Avoid using meaningless or ambiguous verbs to name the function
12. The return value of the function should be clear and unambiguous so that the user can not easily ignore the error condition.
13/Clear Function function, accurate (rather than approximate) implementation of function design.
14. Reduce the function itself or recursive calls between functions.
15. When you write a reentrant function, if you use global variables, you should turn off interrupts, semaphores (i.e. P, v operations)
and other means to protect it.

Seven, the testability
1. Before writing the code, you should design the procedures and methods of debugging and testing, and design a variety of tuning
and the corresponding test code, such as print function.
2. Before the integration test/system is tuned, build the test environment, test project and test case, and
Analyze and optimize test cases carefully to improve test efficiency.

Viii. program Efficiency
1. Always pay attention to the efficiency of the code when programming.
2. Improve the efficiency of code under the premise of ensuring the correctness, stability, readability and testability of the software system.
3. Can not blindly pursue the code efficiency, and the correctness of the software, stability, readability and testability caused by the shadow
Ring.
4. When programming, be mindful of the efficiency of your code, and be considerate when optimizing your code.
5. To carefully construct or directly use the assembly to write functions that are frequently called or have very high performance requirements.
6. Improve the spatial efficiency by dividing and organizing the system data structure and optimizing the program algorithm.
7. In multiple loops, the busiest loop should be placed on the inner layer.
8. Minimize the loop nesting level.
9. To avoid the loop body containing the judgment statement, the Loop statement should be placed in the code block of the judgment statement.
10. Try to use multiplication or other methods instead of division, especially in floating-point operations.

Ix. Quality Assurance
1. Build software quality in the software design process.
Code Quality Assurance Priority Principle
(1) Correctness, refers to the program to achieve the design requirements of the function.
(2) Stability, security, refers to the program is stable, reliable, safe.
(3) testability, refers to the program to have a good testability.
(4) specification/readability, refers to the program writing style, naming rules, etc. to conform to the norms.
(5) Global efficiency refers to the overall efficiency of a software system.
(6) Local efficiency refers to the efficiency of a module/sub-module/function itself.
(7) Personal expression/personal convenience, refers to personal programming habits.
2. Reference only the storage space that belongs to itself.
3. Prevent references to memory space that has already been freed.
4. The memory allocated in the procedure/function is released before the procedure/function exits.
5. The file handle (used for opening the file) requested in the procedure/function to be closed before the procedure/function exits.
6. Prevent memory operations from overstepping.
7. Always pay attention to whether an expression overflows or overflows.
8. Seriously deal with the various error situations that the program can encounter.
9. At the beginning of the system operation, the variables and the operating environment are initialized to prevent the uninitialized variables from being referenced.
10. When the system is running, the data that is loaded into the system is checked for consistency.
11. It is strictly forbidden to change the settings and configuration of other modules or systems arbitrarily.
12. You cannot arbitrarily change the interface to other modules.
13. Fully understand the interface of the system and then use the functionality provided by the system.
14. Always be aware of easily confusing operators. When the program is finished, you should check the operators from beginning to end.
15. Use the recommended standard statement instead of using statements that have a large relationship to the hardware or the operating system.
16. Recommendation: When using a software development kit or control provided by a third party, be aware of the following:
(1) fully understand the application interface, usage environment, and usage considerations.
(2) cannot be overly convinced of its correctness.
(3) Do not use unfamiliar third-party toolkits and controls unless necessary.

Ten, Code compilation
1. Write code should be careful to save at any time, and regular backup, to prevent due to power outages, hard disk damage and other reasons caused
Code is missing.
2. Within the same project group, it is best to use the same editor and use the same setup options.
3. Reasonable design of the software system directory, convenient for developers to use.
4. Turn on all the compiler's alarm switches to compile the program.
5. In the same project group or product group, you want to compile the switch options uniformly.
6. Use the tool software (such as visual SourceSafe) to maintain the code version.

Xi. Code testing, maintenance
1. Unit testing requires at least a statement overlay.
2. Unit tests begin by tracking each statement and observing changes in data flow and variables.
3. The code to be cleaned, collated or optimized should be reviewed and tested.
4. Code version upgrades are subject to rigorous testing.

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.