C ++ coding specifications

Source: Internet
Author: User

//////////////////////////////////////// ///////////////////////////
// File name: mange_fun.cxx
// Version:
// Purpose and main function: Set System Parameters
// Creation date:
// Modification date:
// Author :***
// Modifier:
//////////////////////////////////////// ////////////////////////

 

// ================================================ ======================================
// Letter count: insurehasoutputinfo
// Function Description: Ensure proper output information is available.
// Input parameter: nproductid: corresponding product ID
// Output parameter: void
// Creation date: 00-2-21
// Modification date: 00-2-21
// OPERATOR :***
// Additional description:
// ================================================ ======================================

 

Other Specifications
1. Each source file should contain no more than 20 function bodies, and each function body should not contain more than 120 lines of original code.
2. There are no more than five input parameters and no more than five output parameters.
3. repeated calls to more than three statements should be encapsulated as function calls.
4. Avoid nested loops above three layers as much as possible; reduce if statements of the program as much as possible; Use less obscure statements such as I * = B, A = C + D * B ++.
5. Make full use of the C ++ idea and use the C ++ language for programming in a true sense; Expand member functions for classes.
6. A function only provides one function.
7. Compile functions for simple functions.
8. functions should be predictable, that is, the same output should be generated if the input data is the same.

Note: Functions with internal "Memory" may be unpredictable, because the output may depend on the status of the internal memory (such as a tag. This function is not easy

Understanding is not conducive to testing and maintenance. In C/C ++, the static local variable of a function is the internal memory of the function, which may make the function unpredictable. However, when a function returns

When the return value is a pointer type, the address of the static local variable must be used as the return value. If the return value is an auto class, the return value is an error needle.

Example: the return value (function) of the following function is unpredictable.
Unsigned int integer_sum (unsigned int Base)
{
Unsigned int index;
Static unsigned int sum = 0; // note that it is of the static type.
// If it is changed to the auto type, the function becomes predictable.
For (Index = 1; index <= base; index ++)
{
Sum + = index;
}
Return sum;
}
9. Avoid designing multi-parameter functions and Remove unused parameters from the interface.
Note: The objective is to reduce the complexity of Inter-function interfaces.
10. Non-scheduling functions should reduce or prevent control parameters, and use only data parameters as far as possible.
The purpose is to prevent control coupling between functions.
Example: The following function is not properly constructed.
Int add_sub (int A, int B, unsigned char add_sub_flg)
{
If (add_sub_flg = integer_add)
{
Return (A + B );
}
Else
{
Return (a B );
}
}

It is better to define the following two functions.
Int add (int A, int B)
{
Return (A + B );
}
Int sub (int A, int B)
{
Return (a B );
}
Check the validity of all parameter input of the function.
11. Check the validity of all non-parameter inputs of the function, such as data files and public variables.
Note: There are two main types of function input: parameter input and global variable and data file input, that is, non-parameter input. Before using input, the function should perform the necessary

Check.
12: The function name should accurately describe the function.
13: use the verb-object phrase to name the function that executes an operation. For the OOP method, only the verb can be used (the noun is the object itself ).
Example: Use the following method to name a function.
Void print_record (unsigned int rec_ind );
Int input_record (void );
Unsigned char get_current_color (void );
The Return Value of the function must be clear and clear, so that users cannot easily ignore errors.
Note: the meaning of each error return value of a function must be clear, clear, and accurate to prevent misuse, understanding, or ignoring error return codes.

14: Unless necessary, it is best not to compile the system's default conversion method or forced conversion method as the return value for variables of different types than the function return value.

15. When you call a function to enter parameters, minimize unnecessary default data type conversion or forced data type conversion.
Note: Data type conversion is more or less risky.

16. design the function of high fan entry and Reasonable fan out (less than 7.
Note: fan-out refers to the number of other functions directly called (controlled) by a function, and fan-In refers to the number of upper-level functions that call it.

17. Improve the function structure in the module, reduce the Coupling Degree between functions, and improve the independence of functions and code readability, efficiency, and maintainability. When optimizing the function structure, observe the following

Then:
(1) The implementation of module functions cannot be affected.
(2) carefully examine and improve module or function error handling and module performance requirements.
(3) Improve the software structure by decomposing or combining functions.
(4) Examine the scale of the function. If it is too large, it should be decomposed.
(5) reduce the complexity of Inter-function interfaces.
(6) function calls at different levels should have reasonable fan-in and fan-out.
(7) functions should be predictable.
(8) Improve Function cohesion. (Single function has the highest cohesion)
Note: The function structure after preliminary division should be improved and optimized to make it more reasonable.

18. Use assertions to discover software problems and improve code testability.
Note: assertion checks certain hypothetical conditions (it can be understood that if the condition is true, there is no action; otherwise, it should be reported). It can quickly detect and locate software problems, and at the same time introduce system errors.

Automatic row alarm. Assertions can be used to locate problems that are hidden deep in the system and difficult to find by other means, thus shortening the software issue locating time and improving the testability of the system. Actual response

You can flexibly design assertions according to specific situations.
Example: The following is an asserted in C language that is designed with a macro. (Null is 0l)
# Ifdef _ exam_assert_test _ // If asserted test is used

Void exam_assert (char * file_name, unsigned int line_no)
{
Printf ("/n [exam] assert failed: % s, line % u/N ",
File_name, line_no );
Abort ();
}

# Define exam_assert (condition)
If (condition) // if the condition is true, no action is performed.
NULL;
Else // otherwise report
Exam_assert (_ file __, _ line __)

# Else // if no asserted test is used

# Define exam_assert (condition) null

# Endif/* end of assert */

Use assertions to check the assumptions of the Program Development Environment (OS/Compiler/hardware.
Note: The software and hardware environment and configuration requirements required for running the program cannot be checked by assertions, but must be handled by a specific piece of code. Assertions can only be used for assumptions in the Program Development Environment and

Check whether the software and hardware of a specific version of the configuration have certain functions. If a NIC is configured in the system running environment, it should be checked by the program's formal Code; and whether the NIC is

With an imaginary function, the assertion can be used to check.
Enable assertion checks on the functions and features provided by the compiler because the final software product (that is, the running code or machine code) has no direct relationship with the compiler, that is, the software running process

(Note that it is not in the compilation process), and should not put forward any requirements on the functions of the compiler.
Example: Use assertions to check whether the memory space occupied by the int type data of the compiler is 2, as shown below.
Exam_assert (sizeof (INT) = 2 );

 

Program Efficiency
1: Pay attention to code efficiency during programming.
Note: code efficiency includes global efficiency, local efficiency, time efficiency, and space efficiency. Global efficiency is system efficiency from the perspective of the entire system; Local efficiency is from the perspective of modules or functions.

Efficiency in terms of angle; time efficiency is the length of time required by the program to process input tasks; space efficiency is the memory space required by the program, such as the size of machine code space, data space, stack space

Size.
2: improve code efficiency while ensuring the correctness, stability, readability and testability of the software system.
Note: you cannot blindly pursue code efficiency, but it affects the correctness, stability, readability, and testability of the software.
3: Local efficiency should serve global efficiency, and it cannot affect global efficiency because of local efficiency improvement.
4: Improve space efficiency by dividing and organizing system data structures and optimizing program algorithms.
Note: This is the fundamental solution to the software space efficiency.
Example: The following shows that the structure of the student's academic score is unreasonable.
Typedef unsigned char byte;
Typedef unsigned short word;

Typedef struct student_score_stru

Byte name [8];
Byte age;
Byte sex;
Byte Class;
Byte subject;
Float score;
} Student_score;

Because each student has scores for multiple subjects, the above structure will occupy a large space. The following improvements should be made (divided into two structures). The total storage space will decrease and operations will become more convenient.
Typedef struct student_stru
{
Byte name [8];
Byte age;
Byte sex;
Byte Class;
} Student;

Typedef struct student_score_stru
{
Word student_index;
Byte subject;
Float score;
} Student_score;

Analyze and optimize related algorithms.
Carefully examine and analyze the input processing methods (such as transactions and messages) of the system and modules, and make improvements.
Analyze and optimize the division and organization of functions in the module, improve the organizational structure of functions in the module, and improve program efficiency.
Do not blindly pursue compact code. Note: Compact Code does not represent efficient machine code.

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: 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.