1 Title 6 2 Note 11 3 Identifier Name 18 4 Readability 20 5 Variables, Structure 22 6 Function, Process 28 7 Measuring 36 8 Program Efficiency 40 9 Quality Assurance 44 10 Code Editing, Compiling, Review 50 11 Code Test Maintenance 52 12 macro 53
1 typography
¹1-1: The program block should be written in indentation, and the number of retracted spaces is 4.
Explanation: There can be inconsistent with the code automatically generated by the development tool.
¹1-2: A relatively independent block between the variables must be empty.
Example: The following example does not meet the specification.
IF (! Valid_ni (ni))
{
... // Program Code
}
REPSSN_IND = SSN_DATA [INDEX] .repssn_index;
REPSSN_NI = SSN_DATA [INDEX] .ni;
Write as follows
IF (! Valid_ni (ni))
{
... // Program Code
}
REPSSN_IND = SSN_DATA [INDEX] .repssn_index;
REPSSN_NI = SSN_DATA [INDEX] .ni;
¹1-3: Troubled statements (> 80 characters) should be divided into multi-line writing, long expressions should be divided on the low priority operator, operators are in the new row, and the new lines must be done Appropriate indentation makes the typography, readily readable.
Example:
Perm_count_msg.head.len = no7_to_stat_pers_count_len
Stat_Size_Per_FRAM * SIZEOF (_UL);
ACT_TASK_TABLE [frame_id * stat_task_check_number index] .occupied
= stat_poi [index] .occupied;
ACT_TASK_TABLE [TASKNO] .duration_true_or_false
= SYS_GET_SCCP_STATISTIC_STATE (STAT_ITEM);
REPORT_OR_NOT_FLAG = ((TaskNo && (n7stat_stat_item_valid (stat_item)) && (act_task_table [taskno] .Result_data! = 0); ¹1-4: If there is a longer expression or statement in the statement, it is necessary to adjust the division, and the long expression is to be divided on the low priority operator. The operator is placed in the new row . Example: IF ((TaskNo && (n7stat_stat_item_valid (stat_item))) { ... // Program Code } For (i = 0, j = 0; (i && (j { ... // Program Code } FOR (i = 0, j = 0; (i i , J ) { ... // Program Code } ¹1-5: If the parameters in the function or the process are longer, appropriate division is performed. Example: N7STAT_STR_Compare (Byte *) & stat_Object, (Byte *) & (ACT_TASK_TABLE [TASKNO] .stat_Object), SIZEOF (_STAT_Object)); N7STAT_FLASH_ACT_DURATION (stat_item, frame_id * stat_task_check_number Index, Stat_Object; ¹1-6: Do not allow multiple phrases to write in a row, just write only one statement. Example: The following example does not meet the specification. Rect.Length = 0; Rect.width = 0; Write as follows Rect.length = 0; Rect.width = 0; ¹1-7: If, for, do, while, case, switch, default, etc., and if the execution statement of the IF, for, do, while, etc., no matter how much is incorporated into {}. Example: The following example does not meet the specification. IF (Pusercr == Null) Return; Should be written as follows: IF (Pusercr == NULL) { Return; } ¹1-8: Align only the space bar, do not use the Tab key. Description: When using a different editor reading program, the program layout is not neat due to the number of spaces set by the Tab key, do not use the BC as the editor, because the BC will automatically turn 8 spaces to a Tab button. Therefore, most of the version used to use BC will shrink. ¹1-9: The start of the function or process, the definition of the structure, the code in the statement, and judgment, etc. The indent style is used, and the situation in the case statement processing statement should also follow the statement indentation requirements. ¹1-10: The delimiter of the block (such as the C / C language braces '{' and '}') should eachly a row and in the same column, and align with the statement that references them. In the beginning of the function, the definition, the definition of the structure, the definition of the enumeration, and the programs in IF, For, Do, While, Switch, and CASE statements are used. Example: The following example does not meet the specification. For (...) { ... // Program Code } IF (...) { ... // Program Code } Void Example_Fun (Void) { ... // Program Code } Writing as follows. for (...) { ... // Program Code } IF (...) { ... // Program Code } Void Example_Fun (Void) { ... // Program Code } ¹1-11: When two or more keywords, variables, constants are peer-to-peer operation, the operators between them have to be spaced before or before and after, if they are proactive, if they are closely related to the relationship The characters (such as ->) will not be spaced. Note: The purpose of writing code in this loose manner is to make the code clearer. Since the clarity generated by the leaves is relative, there is no need to leave space in a very clear statement. If the statement is sufficient, the inside of the parentheses (ie, the left and right brackets in front) do not need to be spaced, Needless to add space between multiple hinds, because in the C / C language, parentheses is already the clearest marker. In the long statement, if there is a lot of spaces, it should be keeled as a whole, and the part is not spaced. Do not leave more than two spaces for the operation. Example: (1) comma, the semicolon only in the back plus space. INT A, B, C; (2) Comparison operator, assignment operator "=", " =", arithmetic operator " ", "%", logical operator "&&", "&", bit domain operator "<<", The front and rear spaces of the two-purpose operators such as "^". IF (current_time> = max_time_value) A = B C; A * = 2; A = B ^ 2; (3) "!", "~", " ", "-", "&" or other single operator before and after the space. * P = 'a'; // content operation "*" and content Flag =! isempty; // Non-action "!" and content P = & mem; // address operation "&" between content I ; // " ", "-" between content (4) "->", "." Never add space. P-> ID = pid; // "->" pointer does not add space before and after (5) IF, for, while, switch, etc. should be spaced apart from the posterior parentheses, making the keywords such as IF more prominent, obvious. IF (a> = b && c> d) 1/21-1: One line of programs should be less than 80 characters, do not write too long. 2 note ¹2-1: Under normal circumstances, the amount of active annotation of the source must be 20%. Description: The principle of comments is to help understand the reading of the program, adding in the added place, not too much not much, not too little, the comment language must be accurate, easy to understand, concise. ¹2-2: Illustrative documents (such as head file .h file, .inc file, .def file, compile instruction file .cfg, etc.) should be annotated, and the comment must be listed: copyright instructions, version number, generation date, Author, content, function, relationship with other files, modification log, etc., there should be a brief description of the function function in the annotation of the header file. Example: The header of the header of the file below is comparative standard, of course, is not limited to this format, but the above information is recommended to be included. / ************************************************** CopyRight (C), 1988-1999, Huawei Tech. Co., Ltd. File name: // file name Author: Version: Date: // Author, version and completion date Description: // For detail the main function of this program file, with other modules / / The interface, output value, value range, meaning and parameters of the function // System, order, independence or dependency, etc. Others: // Other content Function List: // Main function list, each record should include a function name and functional description 1. .... History: / / Modify the history list, each modified record should include the change date, modify / / modified 1. Date: AUTHOR: Modification: 2. ... ********************************************************* / ¹2-3: The source file head should be annotated, list: copyright instructions, version number, generation date, author, module destination / function, main function, and its functions, modification log, etc. Example: The header comment comparison criterion of the following source file, of course, is not limited to this format, but the above information is recommended to be included. / ************************************************** *********** CopyRight (C), 1988-1999, Huawei Tech. Co., Ltd. Filename: Test.cpp Author: Version: Date: Description: // module description Version: // Version information Function List: // Main function and its function 1. ------- History: // Historical modification record David 96/10/12 1.0 Build this moudle *********************************************************** ********* / Description: Description A relationship between the content, functionality, and internal parts of this document and other files are related to other files. History is a list of modified history, and each modified record should include a clear date, modifier, and modification. ¹2-4: The function head should be annotated, listed: the purpose of the function, the input parameters, output parameters, return values, call relationships (functions, tables), etc. Example: Comment comparison criterion below this function, of course, is not limited to this format, but the above information is recommended to be included. / ************************************************** Function: // Function Name Description: // Description of function, performance, etc. Calls: // List of functions called by this function Called by: // Call the function list of this function Table access: // Access table (this item only involves the database operation) Table updated: // Modified table (this item only involves the database operation) INPUT: / / Input parameter description, including each parameter // Use, value, and parameters. Output: // For the description of the output parameters. Return: // Function return value Other description ********************************************************* / ¹2-5: Edward Note, modify the code simultaneously modify the corresponding annotation to ensure the consistency of comments and code. Annotation that is no longer deleted. ¹2-6: The contents of the comment should be clear, clear, accurate, and prevent the annuality of the annotation. Explanation: The wrong note is not only harmful. Rules 2-7: Avoid using abbreviations in the comments, especially very useful use. Note: The necessary explanation should be made to abbreviation when using an abbreviation or before or before. ¹2-8: Note It should be similar to the code described. Separate with a blank line. Example: The following example does not meet the specification. example 1: / * GET Replicate Sub System INDEX and NET INDICATOR * / REPSSN_IND = SSN_DATA [INDEX] .repssn_index; REPSSN_NI = SSN_DATA [INDEX] .ni; Example 2: REPSSN_IND = SSN_DATA [INDEX] .repssn_index; REPSSN_NI = SSN_DATA [INDEX] .ni; / * GET Replicate Sub System INDEX and NET INDICATOR * / Write as follows / * GET Replicate Sub System INDEX and NET INDICATOR * / REPSSN_IND = SSN_DATA [INDEX] .repssn_index; REPSSN_NI = SSN_DATA [INDEX] .ni; ¹2-9: For all physical meanings, constants, if its name is not fully annotated, the statement must be annotated, indicating its physical meaning. Note, constant, macros should be placed adjacent to or right. Example: / * Active Statistic Task Number * / #define max_act_task_number 1000 #define max_act_task_number 1000 / * Active Statistic Task Number * / ¹2-10: Data structure declaration (including arrays, structures, classes, enumerations, etc.), if their name is not fully annotated, you must comment. The annotation of the data structure should be placed adjacent to the adjacent position, and cannot be placed below; the notes released in each domain in the structure in this domain. Example: Enumeration / Data / Joint Structure can be described as follows. / * SCCP Interface with sccp user primitive message name * / Enum sccp_user_primitive { N_Unitdata_ind, / * sccp notify sccp user unit data come * / N_notice_ind, / * sccp notify user the No.7 network can not * / / * Transmission this message * / N_Unitdata_req, / * sccp user's unit data transfer request * / } ¹2-11: Global variables There must be more detailed notes, including their function, value range, which functions or procedures to access it, and pay attention to the time of access. Example: / * The ErrorCode When sccp Translate * / / * Global Title Failure, As Follows * / / / Variable Role, Meaning / * 0 - Success 1 - GT Table Error * / / * 2 - GT Error Others - No use * / / / variable range / * ONLY FUNCTION Sccptranslate () in * / / * this moduel can modify it, and other * // * Module Can Visit It THROUGH CALL * / / * The function getGTTranserrorCode () * / / / usage method BYTE G_GTTRANERRORCODE; ¹2-12: Comment is the same for the described content. Description: You can make the program are neat, and the reading and understanding of the comments can be easily. Example: As the following example, the typography is not neat, reading is slightly inconvenient. Void Example_Fun (Void) { / * Code One Comments * / Codeblock One / * Code Two Comments * / Codeblock TWO } It should be changed to the following layout. Void Example_Fun (Void) { / * Code One Comments * / Codeblock One / * Code Two Comments * / Codeblock TWO } ¹2-13: Separate the comment with the code above the code. Example: as the following example, the code is too compact. / * Code One Comments * / Program Code ONE / * Code Two Comments * / Program Code TWO Write as follows / * Code One Comments * / Program Code ONE / * Code Two Comments * / Program Code TWO ¹2-14: The definition and branch statement (conditional branch, cycle statement, etc.) of the variable must be written. Note: These statements are often the key to implementing a particular function. For maintenance personnel, good comments help better understand the procedures, sometimes better than watching design documents. ¹2-15: For the CASE statement under the Switch statement, if you need to process one case after a special situation, you must go to the next case process, you must add a clear annotation before the CASE statement is processed. Description: This is more clearly aware of the programming of the program, effectively prevents an omission of the BREK statement. Example (note the bold portion of the slope): Case CMD_UP: PROCESSUP (); Break; Case cmd_down: ProcessDown (); Break; Case cmd_fwd: Processfwd (); IF (...) { ... Break; } Else { Processcfw_b (); // NOW JUMP INTO CASE CMD_A } Case cmd_a: PROCESSA (); Break; Case cmd_b: PROCESSB (); Break; Case cmd_c: PROCESSC (); Break; Case cmd_d: Processd (); Break; ... 1/22-1: Avoid insertion comments in a row code or expression. Explanation: Unless necessary, the comment should not be inserted in the middle of the code or expression, otherwise it is easy to make the code understandability. 1/22-2: Code is self-comment by the correct naming of functions or processes, variables, structures, etc. and reasonably organizes the code. Description: Clear and accurate functions, variables, etc., can increase code readability and reduce unnecessary comments. 1/22-3: In the function of the code, annotation at the intent level, provide useful, additional information. Note: The purpose of the comment is to explain the purpose, functionality and method of the code, providing information other than code to help the reader understand the code to prevent unnecessary repetition information. Example: The following comments are not significant. / * if resceive_flag is true * / IF (Receive_Flag) The following annotations give additional useful information. / * IF MTP Receive A Message from links * / IF (Receive_Flag) 1/22-4: At the end of the program block, the right fill is marked to indicate the end of a block. Description: When the code segment is longer, especially when multiple nested, this can make the code clearer and more convenient to read. Example: See the following example. IF (...) { // Program Code While (INDEX { // Program Code } / * end of while (index } / * End of if (...) * / / / indicates which if statement ends 1/22-5: Notes in the format of the annotation, it is recommended to use "/ * ... * /". 1/22-6: Comments should consider the factors that are easy to read and appear. If the language used is in, the English is mentioned, it is recommended to use more Chinese unless it is very fluent and accurate English. Description: The comment language is not uniform, affecting the program easy to readability and appearance, for the considering of maintenance personnel, it is recommended to use Chinese. 3 identifier named ¹3-1: The naming of the identifier is clear, clear, clear meaning, and use a complete word or abbreviations that you can understand, avoid misunderstanding. Explanation: The short word can form abbreviation by removing "vowels"; longer words can take a few letters of the first few letters of the word; some words have a recognized abbreviation. Example: The abbreviation of the following words can be basically approved by everyone. Temp can be abbreviated as TMP; FLAG can be abbreviated as flg; STATISTIC can be abbreviated as Stat; INCREMENT can be abbreviated as inc; Message can be abbreviated as MSG; ¹3-2: If special conventions or abbreviations are used, they must be annotated. Note: The necessary annotations for the abbreviations or conventions used in the file should be made in the beginning of the source file. ¹3-3: The own naming style is consistent from the beginning, and cannot be changed back. Description: Individual naming style can be used in the premise of complying with the naming rules of the project group or product group. (There is no regular place in the naming rules to have personal naming style). ¹3-4: Naming for variables, disabled a single character (such as i, j, k ...), it is recommended to indicate its variable type, data type, etc. in addition to specific meaning, but i, j, k Partial cycle variables are allowed. Description: Variables, especially local variables, if used in single characters, it is easy to knock the wrong (such as i-written J), and the compile time is not checked, it is possible for this small error to spend a lot of error. Example: The definition method of the local variable name shown below can be drawn. INT LIV_WIDTH Its variable name is explained as follows: l Partial variable (Local) (Other: G Global Variables (Global) ...) I Data Type (Interger) V variable (other: C constant (const) ...) WiDTH variable meaning This prevents local variables from being renamed with global variables. ¹3-5: Naming specification must be consistent with the system style used, and unify the same project, such as using UNIX's full lowercase to lower the line style or case-write mix, do not use cases and underscore mixing Method, it is used as a special identifier such as M_ and G_ of identifying member variables or global variables, which is permitted by adding case in size. Example: add_user is not allowed, add_user, adduser, m_adduser allows. 1/23-1: Unless necessary, do not define the identifier with numbers or more strange characters. Example: Name the following, make people doubt. #define _example_0_test_ #define _example_1_test_ Void set_sls00 (Byte SLS); Should be changed to meaningful words named #define _example_unit_test_ #define _example_assert_test_ Void Set_UDT_MSG_SLS (Byte SLS); 1/23-2: In the same software product, the interface part identifier (variable, structure, function, and constant) should be planned to prevent compilation and links. Note: The identifier of the interface should have more stringent restrictions to prevent conflicts. If the variables of the interface portion can be specified with the "module" identity, etc. 1/23-3: Naming with the correct antisense word group or a function of the mutually exclusive variable or the opposite action. Note: The following is some antisense phrases commonly used in the software. Add / Remove BEGIN / END CREATE / DESTROY INSERT / DELETE FIRST / LAST GET / RELEASE Increment / Decrement Put / Get Add / Delete Lock / Unlock Open / CLOSE MIN / MAX OLD / New Start / STOP Next / Previous Source / Target Show / Hide Send / Receive Source / Destination CUT / PASTE UP / DOWN Example: INT min_sum; Int max_sum; INT Add_user (byte * user_name); INT delete_user (byte * user_name); 1/23-4: In addition to special applications such as compilation switch / header files, avoid using the definitions of the following scrubbing and ending below _example_test_. 4 readability ¹4-1: Pay attention to the priority of the operator, and use parentheses to express the order of operation of the expression, avoid using the default priority. Description: Prevent misunderstandings when reading procedures, preventing procedures from failing to cause the default priority to design ideas. Example: Expression in the following statement Word = (high << 8) | Low (1) IF ((A | B) && (A & C)) (2) IF ((A | B) <(C & D)) (3) If writing is HIGH << 8 | LOW A | B && A & C A | B due to HIGH << 8 | Low = (high << 8) | Low, A | B && A & C = (A | B) && (A & C), (1) (2) Will not be wrong, but the statement is not easy to understand; A | B ¹4-2: Avoid using the numbers that are not easy to understand, replace it with meaningful identifiers. Involved in physical states or constants containing physical significance should not directly use numbers, and must be replaced with meaningful enumeration or macro. Example: The following program readability is poor. IF (Trunk [index] .trunk_state == 0) { Trunk [index] .trunk_state = 1; ... // Program Code } It should be changed as follows. #define trunk_idle 0 #define trunk_busy 1 IF (Trunk [index] .trunk_state == trunk_idle) { Trunk [index] .trunk_state = trunk_busy; ... // Program Code } 1/24-1: The code that is relatively close in the source program should be as close as possible. Description: Easy to read and find out. Example: The following code layout is not reasonable. Rect.Length = 10; CHAR_POI = STR; RECT.WIDTH = 5; If you are writing as follows, you may be clearer. Rect.Length = 10; Rect.width = 5; // The length of the rectangle is closely related, put together. CHAR_POI = STR; 1/24-2: Don't use the stunning skills that is difficult to understand unless it is necessary. Explanation: High-tech statements are not equal to high efficiency procedures, and the efficiency key of actually programs is algorithm. Example: As follows, consider that there may be problems in no circumference, it is more difficult to understand. * STAT_POI = 1; * stat_poi = 1; It should be changed to the following. * stat_poi = 1; STAT_POI ; // This two statement is equivalent to "* stat_poi = 1;" stat_poi; * STAT_POI = 1; // This two statement is equivalent to "* STAT_POI = 1;" 5 variable, structure ¹5-1: Remove the unnecessary common variable. Note: The public variable is one of the reasons for increasing the inter-module, so there is no necessary common variables to reduce the coupling between the modules. ¹ 5-2: Carefully define and clarify the meaning, role, value range of public variables and the relationship between public variables. Note: While noticing the variable declaration, the meaning of its meaning, role, and value range should be given, and the relationship with other variables should be explained if necessary. ¹ 5-3: Clarify the relationship between the common variables and the function or process of operating this public variable, such as access, modification, and creation. Note: After clarifying the relationship between processes, it will facilitate further optimization of procedures, unit testing, system coordination, and code maintenance. Description of this relationship can be described in a comment or document. Example: In the source file, you can explain the following annotation form. Relation system_init input_recprint_rec stat_score Student Create Modify Access Access Score Create Modify Access Access, Modify Note: Relation is an operational relationship; system_init, input_rec, print_rec, stat_score is four different functions; student, score is two global variables; create representation creation, modify represents modification, and Access is access. Where the function input_rec, both can modify the variable score, so this variable will cause large coupling between functions and may increase the difficulty of code testing and maintenance. ¹5-4: When data is passed to the public variable, it is very careful, preventing the phenomenon of deficiency and unreasonable values or offshore. Note: When assigning a common variable, it should be checked to improve the reliability and stability of the code. ¹ 5-5: Prevent local variables from being the same name of the public variable. Description: If a better naming rule is used, this problem can be eliminated. ¹ 5-6: It is strictly forbidden to use the variable that is not initialized as the right value. Description: Especially in C / C , unfolded pointers, often cause system crash. 1/25-1: Constructs only one module or function can be modified, created, and the rest of the module or function is only accessible, preventing multiple different modules or functions from modifying, creating the same common variable. Description: Reduce the coupling of common variables. 1/25-2: Using strict forms of data type defined, portable data types, try not to use variables with specific hardware or software environmental relationships. Description: Use standard data types to facilitate the transplantation of the program. Example: As the following example (in the BC3.1 environment under DOS), problems may occur during transplantation. void main () { Register int index; // register variable _AX = 0x4000; // _ax is the register "pseudo variable" provided by BC3.1 ... // Program Code } 1/25-3: The function of the structure is a single, which is an abstraction of a transaction. Note: It should strive to make the structure represents an abstraction of a realistic transaction when designing the structure, not simultaneous representation. Each element in the structure should represent different sides of the same transaction, and the elements of different transactions that have no relationships or weak relationships should not be placed in the same structure. Example: The following structure is not clear and reasonable. Typedef struct student_stru { Unsigned char name [8]; / * student's name * / UNSIGNED CHAR AGE; / * STUDENT'S AGE * / Unsigned char sex; / * student's sex, as follows * / / * 0 - female; 1 - MALE * / Unsigned char Teacher_name [8]; / * the student teacher's name * / unisgned charr Teacher_sex; / * His Teacher Sex * / }. If it is changed, it may be more reasonable. Typedef struct teacher_stru { Unsigned char name [8]; / * teacher name * / Unisgned Char Sex; / * Teacher SEX, AS FOLLOWS * / / * 0 - female; 1 - MALE * / } Teacher; Typedef struct student_stru { Unsigned char name [8]; / * student's name * / UNSIGNED CHAR AGE; / * STUDENT'S AGE * / Unsigned char sex; / * student's sex, as follows * / / * 0 - female; 1 - MALE * / Unsigned int teacher_ind; / * his teacher index * / }. 1/25-4: Do not design, very flexible data structure. Explanation: The facade has, the flexible data structure is easy to cause misunderstanding and difficulty. 1/25-5: The relationship between different structures should not be too complicated. Explanation: If the relationship between two structures is more complicated, close, then a structure should be combined. Example: The construction is unreasonable as the following two structures. Typedef struct person_one_stru { UNSIGNED Char Name [8]; UNSIGNED CHAR ADDR [40]; Unsigned char sex; UNSIGNED CHAR City [15]; } In Person_one; Typedef struct person_two_stru { UNSIGNED Char Name [8]; Unsigned char agn; UNSIGNED Char Tel; } Person_Two; Since both structures describe the same thing, it is better to synthesize a structure. Typedef struct person_stru { UNSIGNED Char Name [8]; Unsigned char agn; Unsigned char sex; UNSIGNED CHAR ADDR [40]; UNSIGNED CHAR City [15]; UNSIGNED Char Tel; } In Person; 1/25-6: The number of elements in the structure should be If the number of elements in the structure can be considered to make the element into different sub-structures in accordance with some principles to reduce the number of elements in the original structure. Description: Increased consolidate, operability, and maintenanceability. Example: If it is considered to be too much _Person structural element, then it can be divided as follows. Typedef struct person_base_info_stru { UNSIGNED Char Name [8]; Unsigned char agn; Unsigned char sex; Person_base_info; Typedef struct person_address_stru { UNSIGNED CHAR ADDR [40]; UNSIGNED CHAR City [15]; UNSIGNED Char Tel; } Person_Address; Typedef struct person_stru { Person_base_info person_base; Person_address Person_Addr; Person; 1/25-7: Carefully design the layout and arrangement of elements in the structure, so that the structure is easy to understand, saving the space, and reduces the phenomenon of misuse. Note: The elements in the structure are arranged in a reasonable arrangement, saving space and increases understandability. Example: The bit field arrangement in the following structure will account for large space, readability is slightly poor. Typedef struct example_stru { Unsigned int valid: 1; Person Person; Unsigned int set_flg: 1; EXAMPLE; If it is changed to the following form, not only can save 1 byte space, but readability is also better. Typedef struct example_stru { Unsigned int valid: 1; Unsigned int set_flg: 1; Person Person; EXAMPLE; 1/25-8: Structure Design To consider the upgrade of forward compatibility and later, and reserve room for some future possible applications (such as reservation, etc.). Note: Software is compatible with one of the important signs of software products success. If you want the product to have better forward compatibility, you should reserve certain rooms for the later version of the product design, and you must consider the various features of the previous version when upgrading the product. 1/25-9: Pay attention to the specific language and compiler to handle the principles of different data types and related details. Description: If in the C language, the Static local variable will be generated in the memory "data area", not the Static local variable will be generated in the "stack". These details are very important to the guarantee of the quality of the program. 1/25-10: When programming, pay attention to the forced conversion of the data type. Note: When the data type is forced to convert, the significance of its data, the value after the conversion is possible, and these details are likely to leave a hidden danger. 1/25-11: There is also a full understanding of the default data type conversion of the compilation system. Example: The value is assigned, most compilers do not produce alarm, but the meaning of the value is slightly changed. CHAR CHR; UNSIGNED SHORT INT EXAM CHR = -1; Exam = chr; // The compiler does not produce alarm, at which time Exam is 0xffff. 1/25-12: Minimize the default conversion and mandatory conversion without the necessary data types. 1/25-13: Reasonably design data and use custom data types to avoid unnecessary type conversions between data. 1/25-14: Appropriate naming for custom data types makes it self-descriptive to increase code readability. Note that its naming mode is unified in the same product. Note: Use custom types to make up for the shortcomings of the programming language, the shortcomings that are less, insufficient information volume, and make the program clear and concise. Example: Custom data type can be declared with reference to the following manner. The following statement makes it simply using the data type. TYPEDEF UNSIGNED CHAR BYTE Typedef unsigned short word; Typedef unsigned int dword; The following statement allows the data type to have a richer meaning. Typedef float distance; Typedef float score; 1/25-15: When declaring data structures for distributed environments or different CPU communication environments, you must consider issues such as the byte order of the machine, and the bit domains and byte alignment used. Description: For example, Intel CPU and 68360 CPU, when processing bitmines and integers, "order" in memory is exactly the opposite. Example: If there is a short integer and structure. UNSIGNED short int exam; typedef struct exam_bit_stru {/ * Intel 68360 * / Unsigned Int A1: 1; / * bit 0 7 * / Unsigned int A2: 1; / * bit 1 6 * / Unsigned Int A3: 1; / * bit 2 5 * / } Exam_bit; The following is the way the Intel CPU generates short integers and bitmines. Memory: 0 1 2 ... (from low to high, byte) Exam Exam low byte Exam High byte Memory: 0 Bit 1 bit 2 bit ... (byte "bit") EXAM_BIT A1 A2 A3 The following is the way the 68360 CPU generates short integers and bitmines. Memory: 0 1 2 ... (from low to high, byte) Exam Exam High byte Exam Low byte Memory: 7 Bit 6 bit 5 bit ... (byte "bit") EXAM_BIT A1 A2 A3 Explanation: In alignment, the CPU's operating efficiency is much faster. Example: As shown below, when a LONG type (such as long1 in the figure) is aligned with the memory word boundary in memory, the CPU Access this number only needs to access a memory, and when a long type (as shown LONG2) When the location in memory spans the word boundary, the CPU Access this number requires multiple access memory, such as I960cx access to such a number to read the memory three times (a Byte, a short, a Byte, by CPU) Micro-code execution, transparent software transparency), all of the CPU operation is much faster in all alignment mode. 1 8 16 24 32 ------------------------- | long1 | long1 | long1 | long1 | ------------------------- | | | | | Long2 | -------------------------- | long2 | long2 | long2 | | | -------------------------- | .... 6 function, process ¹ ¹ -1:,, 处理, 处理,,,..,... ¹6-2: Clear function function, accurate (rather than approximate) implement function design. ¹6-3: When writing reusable functions, pay attention to the use of local variables (such as the re-enter function of the C / C language, you should use the AUTO default local variable or register variable). Explanation: When writing a reusable function of the C / C language, the Static partial variable should not be used, otherwise it must be specially processed to make functions of reproducibility. ¹6-4: When writing a reinforcing function, if global variables are used, it should be protected by means of the interrupt, the amount of semaphore (ie, the P, V operation). Note: If the global variable used is not protected, this function does not have reusability, that is, when multiple processes call this function, it is possible to make the global variable becomes unknown. Example: Assuming Exam is an INT type global variable, the function SQure_exam returns an Exam square value. Then the following functions do not have reusability. Unsigned int example (int Para) { Unsigned int Temp; Exam = para; // (**) TEMP = Square_exam (); Return Temp; } If this function is called by multiple processes, the result may be unknown because when the (**) statement is just executed, the process using this function may be activated, then the newly activated process is executed When the function, Exam is given to another PARA value, so when the control is returned to "Temp = Square_exam ()", the calculated TEMP is likely to not be the result of the expected. This function should be improved as follows. Unsigned int example (int Para) { Unsigned int Temp; [Application Sample Operation] // If the application is not "semapled", the additional process is Exam = para; // assigns an Exam and calculates its square process (ie, use this Temp = Square_exam (); // Signal), this process must wait for its release signal to [Release Semicone Operation] // Continued. If you apply for a signal, you can continue, but // It must wait for the process to release the semaphore, // use this signal. Return Temp; } ¹6-5: In the same project group, the legitimacy inspection of the interface function parameters should be responsible for the caller of the function or by the interface function itself, the default is the responsibility of the function caller. Description: The legality check of the combination of the parameters of the module indirect port function, there are often two extreme phenomena, ie: either the caller and the invigible pair of parameters do not legally check, and the result is missing the legality check. One necessary processing procedure, resulting in problem hidden dangers; either the caller and the caller check the parameters, which will not cause problems, but generate redundant code, which reduces efficiency. 1/26-1: Prevents the parameters of the function from as a working variable. Description: Use the parameters of the function as the working variable, which may change the parameter content, which is very dangerous. For parameters that must be changed, it is best to use a local variable generation, and finally assign the content of the local variable to this parameter. Example: The implementation of the function is not very good. Void Sum_Data (unsigned int number, int * data, int * sum) { Unsigned int count; * SUM = 0; For (count = 0; count { * SUM = DATA [count]; // SUM is a working variable, not very good. } } If you change to the following, it is better. Void Sum_Data (unsigned int number, int * data, int * sum) { Unsigned int count; int sum_temp; SUM_TEMP = 0; For (count = 0; count { SUM_TEMP = Data [count]; } * SUM = SUM_TEMP; } 1/26-2: The size of the function is within 200 lines. Description: Does not include annotations and spaces. 1/26-3: A function completes only one function. 1/26-4: Writing a function for a simple function. Explanation: Although the functions that can be done with only one or two lines, it seems to be unnecessary, but the function can make the function, add program readability, and convenient maintenance, test. Example: The functionality of the statement is not obvious. Value = (a> b)? A: B; It is clear that it is clear. Int Max (Int A, Int b) { RETURN ((a> b)? a: b); } Value = max (a, b); Or is changed as follows. #define max (A, B) (((a)> (b))? (a): (b)) Value = max (a, b); 1/26-5: Do not design a function of multi-purpose. Note: Multi-function is a function of one, it is likely to make functions, test, and maintenance of functions. 1/26-6: The function of the function should be predicted, that is, as long as the input data is the same, the same output should be generated. Note: The function of a function with internal "memory" may be unpredictable because its output may depend on the state of the internal memory (such as a mark). Such functions are neither easy to understand and are not conducive to testing and maintenance. In the C / C language, the STIC local variable of the function is the internal memory of the function, it is possible to make the function of the function unpredictable, however, when a function is returned, it must be the address of the local variable of Static as a Return the value, if the AUTO class, returns to the wrong needle. Example: The following functions, the return value (ie, the function) is unpredictable. Unsigned int integer_sum (unsigned int base) { Unsigned int index; Static unsigned int sum = 0; // Note, is a Static type. // If it is changed to the AUTO type, the function will become predictable. For (INDEX = 1; index <= base; index ) { SUM = Index; } Return SUM; } 1/26-7: Try not to write functions that depend on the internal implementation of other functions. Description: This article is the basic requirement of function independence. Since most of the high-level languages are structured, the syntax requirements of the specific language and the compiler function can be prevented from occurring. But in assembly language, due to its flexibility, it is likely to make this situation. Example: As follows, the assembler example of TASM under DOS. Process Print_MSG's implementation depends on the specific implementation of INPUT_MSG, which is unstructured, difficult to maintain, modify. ... // code Proc Print_MSG // Process (Function) Print_MSG ... // code JMP Label ... // code ENDP PROC INPUT_MSG // Process (Function) Input_MSG ... // code Label: ... // code ENDP 1/26-8: Avoid designing multi-parameter functions, unused parameters remove from the interface. Description: Objective To reduce the complexity of functional interface. 1/26-9: Non-scheduling functions should reduce or prevent control parameters, try only to use data parameters. Note: The purpose of this recommendation is to prevent the control coupling between the function. The scheduling function refers to the corresponding functional entity (ie function or process) based on the input message type or control command, and it does not complete the specific function. Control parameters refer to the parameters of changing the functional behavior, that is, the function should determine how the specific work is determined according to this parameter. The control parameters of the non-schedule function increases the control coupling between the functions, which is likely to increase the degree of coupling between the functions and make the function of the function unique. Example: The following function is not reasonable. 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 be divided into the following two functions. Int Add (Int A, INT B) { Return (A B); } int SUB (int A, int b) { Return (a b); } 1/26-10: Check the validity of all parameter inputs for functions. 1/26-11: Check the validity of all non-parameter inputs, such as data files, common variables, etc. Description: There are two main inputs: one is parameter input; the other is the global variable, the input of data files, that is, non-parameter input. The function should be checked before using the input. 1/26-12: The function name should accurately describe the functionality of the function. 1/26-13: Name the function of performing a function using the Bright Articles. If it is an OOP method, you can only verb (noun is the object itself). Example: Refer to the following naming function. Void Print_Record (unsigned int REC_IND); INT INPUT_RECORD (VOID); Unsigned char GET_CURRENT_COLOR (Void); Recommendation 6-14: Avoid named useless or unqualified verbs. Note: Avoid named the meaning of unclear verbs such as Process, Handle, etc., because these verbs do not explain what to do. Recommendation 6-15: The return value of the function is clear, clear, so that users don't easily ignore the wrong situation. Note: The meaning of each error return value is clear, clear, accurate, prevents the user from misuse, understanding errors or ignoring the error return code. 1/26-16: Unless necessary, it is best not to return a variable that is different from the value of the function to the value of the value, and returns to the system default conversion method or the mandatory conversion mode is returned. 1/26-17: Let the function appear easy to understand in the call point, easy to understand. 1/26-18: When the call function fills in the parameters, it should minimize the unnecessary default data type conversion or forced data type conversion. Description: It is dangerous because of data type conversion or less dangerous. 1/26-19: Avoid unnecessary statements in the function to prevent spam code in the program. Note: The garbage code in the program not only takes advantage of additional space, but also often affects the functionality and performance of the program, but it is likely to cause unnecessary trouble to test, maintenance, etc. 1/26-20: Prevents putting unconnected statements in a function. Description: A random cohes occurring in the process of preventing functions or processes. Random cohesive refers to the same function or process that will not be associated or associated with weak statements. The maintenance of the random megadownload megadownload megadownload megadownload megadownload megadownload megadownload megadownload megadownload megadownload megadownload megadownload megadownload Using a random internal polymer function, it is often easy to appear in an application, and another application occasion does not allow this improvement, thereby caught in a dilemma. When programming, often encounter the same code in different functions, and many developers are willing to propose these code and constitute a new function. If these code is associated and is completed, this configuration is reasonable, otherwise this configuration will produce a randomized function. Example: The following function is a random coner. void init_var (void) { Rect.length = 0; Rect.width = 0; / * The length and width of the initialization rectangle * / POINT.X = 10; Point.y = 10; / * Initialize the "point" coordinate * / } The length of the rectangle, the width and the coordinates of the point are basically no relationship, so the above function is randomly. Two functions should be divided as follows: Void Init_Rect (Void) { Rect.length = 0; Rect.width = 0; / * The length and width of the initialization rectangle * / } Void Init_Point (Void) { POINT.X = 10; Point.y = 10; / * Initialize the "point" coordinate * / } 1/26-21: If you repeat the same thing, then there may be problems on the division of functions. Description: If there is a substantial association between the statements of this code and is completed, it can be considering a new function to consider this code. 1/26-22: Function is not clear, especially when only one superior function calls it, consider merged it into the superior function without having to exist alone. Explanation: Excessive in modules is too much, generally makes interfaces between functions complicated. So too small functions, especially those with low or clear functions, is not worth existence. 1st 6/26-23: Design high fan in, reasonable fan out (less than 7) function. Description: Fan out means that a function calls (control) the number of other functions, and the fan is referring to how many upper-level functions call it. The fan is excessive, indicating that the function is over-complex, requires too much lower-level function to control and coordinate; and the fan is too small, such as always 1, indicating that the function's call level may be too much, this is unfavorable to read and function structure analysis, And the program is running to system resources such as stack space, etc. Function is more reasonable (except for scheduling functions) is usually 3-5. The fan is too large, which is generally due to the lack of the intermediate level, and the intermediate level function can be appropriately increased. The fan is too small, and the lower level function can be further decomposed in multiple functions or merge into the superior function. Of course, when decomposing or consolidating functions, you cannot change the functions to be implemented, and you cannot violate the independence between the functions. The larger fan, the more the superiors that use this function, the more efficient use of such functions, but cannot violate the independence between the function and simply pursue high fans. The function and the underlying function in the common module should have a high fan. A better software structure is usually a high fan-out of the top-level function, and the mid-layer function is small, and the underlying function is fanned into the common module. 1/26-24: Reduce recursive call between functions or functions. Description: Recursive calls, especially between functions (such as A-> B-> C-> a), affecting the understandability of the program; recursive calls generally occupy more system resources (such as stack space); recursive call There is a certain impact on the test of the program. Therefore, unless the implementation of certain algorithms or functions, there is no necessary recursive call. 1/26-25: Carefully analyze the functionality and performance requirements of the module, and further segment, and if necessary to draw the relevant data flow map, the module is used to divide and organize. Note: The division of functions and organizations are the critical steps in the implementation of the module, how to divide reasonable function structure, which is related to the final efficiency and maintenanceability, measurability, etc. of the module. The function structure is often one of the common methods based on the functional map of the module or / and the data flow map. 1/26-26: Improve the structure of the function in the module, reduce the coupling between the function, and increase the independence of the function and the code readability, efficiency, and maintainability. When you optimize the function structure, you must follow the following principles: (1) Do not affect the implementation of the module function. (2) Carefully examine the module or function error handling and the performance requirements of the module and perfect. (3) Improve the software structure by breaking down or consolidation functions. (4) The scale of the inspection function is too large to decompose. (5) Reduce the complexity of the interface between the function. (6) Different levels of function calls must have a relatively reasonable fan, fan out. (7) The function function should be predictable. (8) Improve the function of the function. (The function of the single function is highest) Explanation: Improvement, optimization of the function structure after preliminary division, make it more reasonable. 1/26-27: Program in the environment of multi-tasking operating system, pay attention to the reproducibility of functions. Description: Reproduction is that the function can be called by multiple task processes. In multi-task operating systems, it is very important to have reusability because this is a need for multiple processes to share this function. In addition, whether the compiler provides a reusable function library, which is related to the operating system it serves, and only the compiler is likely to provide a retrofitable library when only the operating system is multitasking. Such as DOS BC and MSC, etc. do not have a retrnight library because DOS is a single user single task operating system. 1/26-28: Avoid using BOOL parameters. Description: There are two, one is that the BOOL parameter value is meaningless. The meaning of Ture / false is very vague. It is difficult to know what this parameter is conveyed when calling; its second is that the BOOL parameter value is not conducive to expansion. There is NULL is also a meaningless word. 1/26-29: For functions that provide the return value, it is best to use it when referenced. 1/26-30: When a process (function) is more reference to a longer variable (typically a structure), it can be used as a macro replacement. Description: This can increase programming efficiency and readability of the program. Example: More reference theateivebuffer [firstsocket] in a process .BydataPtr, The following macro definition can be replaced by: # Define psockdata thereteivebuffer [firstscoket] .BYDATAPTR 7-measurable ¹7-1: In the same project group or product group, there must be a unified setup switch and the corresponding print function prepared for integrated testing and system coordination, and have a detailed description. Note: This rule is for project groups or product groups. ¹ 7-2: In the same project group or product group, the format of the data string in which the print out must be in a unified form. At least there must be at least in the information string (or source file name) and the line number. Explanation: Unified Infinction Information Format is easy to integrate testing. ¹ 7-3: Programming To select the appropriate test point for unit testing, and carefully construct test code, test case, and give a clear annotation. The test code portion should be used as a submodule (in the module) to facilitate the installation and disassembly of the code in the module (by adjusting the switch). Description: Prepare for unit testing. ¹ 7-4: Before making an integrated test / system joint adjustment, construct test environment, test projects and test cases, while carefully analyzing and optimizing test use cases to improve test efficiency. Explanation: Good test cases should be as possible to simulate the boundaries of the program, various complex environments and some extreme conditions. ¹ 7-5: Using assertions to discover software issues, improve code-specificity. Description: The assertion is to check for some hypothetical conditions (it can be understood that if the condition is established, there is no action, otherwise it should be reported), which can quickly discover and locate the software problem, and automatic alarms for system errors. As a result, it can be locally hidden in the system, and the problems that are extremely difficult to discover in the system will be positioned, thereby shortening software problem positioning time, increasing the system's measurability. When actual application, the assertions can be flexibly designed according to the specific situation. Example: Below is an assertion in the C language and is designed with macro. (Where NULL is 0L) #ifdef _exam_assert_test_ // If you use the assertion test Void Exam_assert (Char * file_name, unsigned int line_no) { Printf ("/ n [eXam] assert filed:% s, line% u / N", FILE_NAME, LINE_NO; Abort (); } #define exam_assert (condition) If the condition is established, there is no action. NULL; ELSE / / otherwise report EXAM_ASSERT (__file__, __line__) #ELSE // If you do not use the assertion test #define exam_assert (condition) NULL #ENDIF / * END OF ASSERT * / ¹ 7-6: Use asserts to check the procedures that should occur during normal operation, but there is a possibility of illegal situations. ¹ 7-7: You cannot use the assert to check the final product will definitely appear and must be processed. Description: As an assertion is used to handle the wrong case that may occur and must be processed, not an assertion. If a module receives a message on other modules or links, the rationality of the message is checked, which is normal error check, and cannot be implemented in an assertion. ¹ 7-8: A clear comment is added to a more complicated assertion. Note: To complex assertion, add an annotation to clarify the assertion and reduce unnecessary misuse. ¹ 7-9: Use the assertion to confirm the parameters of the function. Example: Assuming that there is a pointer in a function parameter, you can check it before using the pointer, as follows. Int exam_fun (unsigned char * STR) { Exam_assert (STR! = Null); // Use the assertion to check "hypothesis pointer is not empty" condition ... // Other Program Code } ¹ 7-10: Use assert that guarantees that there is no defined feature or function is not used. Example: Assuming that a communication module is designed to provide "no connection" and "connection" services. However, only the "no connection" business is implemented in the current version, and in this release, the user (upper module) should not generate a request for "connection" business, then the assertion check user is used when testing " Connect "business. as follows. #define exam_connectionLESS 0 // No connection service #define exam_connection 1 // Connecting Service INT MSG_PROCESS (exam_message * msg) { Unsigned char service; / * message service class * / Exam_assert (msg! = Null); Service = GET_MSG_SERVICE_CLASS (MSG); Exam_assert (Service! = Exam_Connection); // Assume that no connection service is not used ... // Other Program Code } ¹7-11: Check the hypothesis of the program development environment (OS / Compiler / Hardware) with assertions. Note: The software and hardware environment and configuration requirements required for program runtime cannot be checked by assertion, but must be processed by a special code. Using asserts Only the assumptions in the program development environment and whether the configured version of the hardware has a hypothesis of certain functions. If a network card is configured in the system operating environment, it should be checked by the official code in the program; if this network card has a function of a function, it can be checked by the assertion. The functions and features provided by the compiler are assumed to be asserted because the software final product (ie, run code or machine code) and the compiler have no direct relationship, that is, during software operation (notice is not compilation process) will not No need for the compiler's functionality. Example: Whether the memory space occupied by the INT data of the compiler is 2, as follows. Exam_ASSERT (SIZEOF (INT) == 2); ¹ 7-12: The assertion should be removed from the official software product (ie, turn off the relevant adjustment switch). Explanation: Accelerate the software running speed. ¹ 7-13: Setting with the cancellation of testing methods in the software system, cannot affect the function of software implementation. Description: The software with test code and the software that turn off the test code should be consistent on functional behavior. ¹7-14: Switch the software's Debug and official version with the debug switch, not the different source files of the official version and the Debug version to reduce the difficulty of maintenance. ¹ 7-15: The software's debug version and the release version should be unified, not allowing the homer, and should pay attention to ensure the consistency of both versions in implementation. 1/27-1: Before writing code, the method and means of program debugging and testing should be pre-design, and design a variety of adjustment switches and corresponding test code such as print functions. Description: The debugging and testing of the program is a very important phase in the software survival cycle, how to make more comprehensive and high-rate tests and find out the errors in the software as much as possible. Therefore, before writing the source code, in addition to a relatively complete test plan, a series of code test methods should be designed to facilitate unit testing, integrated testing and system coordination. 1/27-2: The debug switch should be divided into different levels and types. Note: The setting and classification of the debugging switch should be considered from the following aspects: adjustment of a part of the module or system; adjustment of a function of a module or system; for some other purpose, such as performance, capacity, etc. test. This makes it easy for software functions, and facilitates module unit testing, system coordination, etc. 1/27-3: Write the anti-wrong procedure, and then use assertions to declare an error after processing errors. Example: If a module receives a message on the communication link, the legality of the message is checked. If the message category is not specified in the communication protocol, an error should be performed, and the assertion report is available, such as the following example. #ifdef _exam_assert_test_ // If you use the assertion test / * NOTICE: THIS FUNCTION DoES Not Call 'Abort' to EXIT Program * / Void assert_report (char * file_name, unsigned int line_no) { Printf ("/ n [exam] error report:% s, line% u / N", FILE_NAME, LINE_NO; } #define assert_report (condition) If the condition is established, there is no action. NULL; ELSE / / otherwise report askERT_REPORT (__file__, __line__) #ELSE // If you do not use the assertion test #define assert_report (condition) NULL #ENDIF / * END OF ASSERT * / INT msg_handle (unsigned char msg_name, unsigned char * msg) { Switch (msg_name) { Case msg_one: ... // Message MSG_One Processing Return Msg_Handle_suCcess; ... // Other legitimate message processing DEFAULT: ... // Message error handling AskERT_REPORT (FALSE); // "Legal" message is not established, report Return Msg_Handle_ERROR; } } 8 program efficiency ¹8-1: Takes usually the efficiency of the code when programming. Description: The code efficiency is divided into global efficiency, local efficiency, time efficiency and spatial efficiency. The global efficiency is systematic efficiency of standing throughout the system; local efficiency is the efficiency of standing on module or function; time efficiency is the length of time required for program processing input tasks; spatial efficiency is the memory space required for the program, such as Machine code space size, data space size, stack space size, etc. ¹8-2: Improve code efficiency under the premise of ensuring the correctness, stability, readability and measurability of the software system. Explanation: You cannot pursue code efficiency, and affect the correctness, stability, readability and measurability of the software. ¹8-3: Local efficiency should be global efficiency services, which cannot affect global efficiency due to local efficiency. ¹8-4: By the improvement of the division of the system data structure and the organization, and the optimization of the program algorithm is improved. Note: This method is to solve the fundamental approach to Software space efficiency. Example: The structure of the student learning score is unreasonable as follows. TYPEDEF UNSIGNED CHAR BYTE Typedef unsigned short word; Typedef strunt student_score_stru Byte Name [8]; Byte agec; BYTE SEX; BYTE CLASS; BYTE SUBJECT; Float score; STUDENT_SCORE; Because each student has a multi-study score, so that the above structure will take up larger space. The following improvements (divided into two structures), the total storage space will become small, and the operation is also more convenient. Typedef struct student_stru { Byte Name [8]; Byte agec; BYTE SEX; BYTE CLASS; }. Typedef strunt student_score_stru { Word student_index; BYTE SUBJECT; Float score; STUDENT_SCORE; ¹8-5: The working volume in the circulation is minimized. Note: Whether the statement within the circulation can be carefully considered outside the cyclic body, so that the workload in the circulation is minimal, thereby improving the time efficiency of the program. Example: The following code is not high. For (Ind = 0; Ind { SUM = Ind; Back_sum = SUM; / * Backup sum * / } The statement "back_sum = sum;" can be placed after the FOR statement, as follows. For (Ind = 0; Ind { SUM = Ind; } Back_sum = SUM; / * Backup sum * / 1/28-1: Carefully analyze the algorithm and optimize. 1/28-2: Carefully examine, analyze system and module processing inputs (such as transactions, messages, etc.), and improve. 1/28-3: Analyze, optimize, improve the organizational structure of functions in modules, and improve program efficiency. Explanation: The efficiency of the software system is mainly related to algorithms, handling task, system functions, and function structures. Only unable to solve fundamental problems only on code. 1/28-4: When programming, always pay attention to code efficiency; when optimizing the code, consider the week. 1/28-5: It should not spend too much time to improve the function code efficiency of calls not very frequent. Explanation: Optimization of code can improve efficiency, but if considering it is very likely to cause serious consequences. 1/28-6: To carefully construct or direct use of the compilation to call frequently or high performance functions. Note: Compilation embedded methods can be used only when the compilation system is generated and the hardware system is more familiar. Embedded assembly can improve time and space efficiency, but there is also a risk. 1/28-7: In the premise of ensuring the quality of the program, the space efficiency is improved by compressing the quantity, remove unnecessary code and reducing unnecessary local and global variables. Note: This method can play a role in improving space efficiency, but often does not solve fundamental problems. 1/28-8: In multiple cycles, the most busy cycle should be placed in the innermost layer. Description: Reduce the number of times the CPU cut into the circulating layer. Example: The following code is not high. For (ROW = 0; ROW <100; Row ) { For (COL = 0; Col <5; Col ) { SUM = a [row] [col]; } } It can be changed to the following to improve efficiency. For (COL = 0; Col <5; Col ) { For (ROW = 0; ROW <100; Row ) { SUM = a [row] [col]; } } 1/28-9: Minimize circulation nested hierarchy. 1/28-10: Avoiding the cycle in the cycle, the cycle can be placed in the code block of the judgment statement. Description: The purpose is to reduce the number of judgments. The determination statement in the circulation body can be moved to the cycle body, and the specific case of the program is generally, the judgment statement independent of the cyclic variable can be moved outside the cyclic body, and the associated may not. Example: The following code efficiency is slightly lower. For (IND = 0; Ind { IF (DATA_TYPE == Rect_area) { Area_sum = Rect_area [Ind]; } Else { RECT_LENGTH_SUM = Rect [Ind] .length Rect_width_sum = Rect [Ind] .width; } } Because the judgment statement is not related to the loop variable, it can be improved as follows to reduce the number of judgments. IF (DATA_TYPE == Rect_area) { For (IND = 0; Ind { Area_sum = Rect_area [Ind]; } } Else { For (IND = 0; Ind { RECT_LENGTH_SUM = Rect [Ind] .length Rect_width_sum = Rect [Ind] .width; } } 1/28-11: Use multiplication or other method instead of division, especially in floating point operations. Description: The floating point calculation division should take up more CPU resources. Example: The following expression may take more CPU resources. #define pai 3.1416 RADIUS = Circle_length / (2 * PAI); The floating point division should be changed to floating point multiplication as follows. #DEFINE PAI_RECIPROCAL (1 / 3.1416) // Compiler compiles, the specific floating point number will be generated RADIUS = Circle_length * Pai_Reciprocal / 2; 1/28-12: Do not pursue compact code. Note: Because the compact code does not represent an efficient machine code. 9 quality assurance ¹9-1: Constructing software quality during software design. ¹ 9-2: Code Quality Assurance Priority Principle (1) The correctness is the function of design requirements. (2) Stability, safety, stability, reliable, and safe. (3) Testability, referring to a good testability. (4) Specification / readability, referral writing style, naming rules, etc. should comply with specifications. (5) Global efficiency refers to the overall efficiency of the software system. (6) Local efficiency, refers to itself of a module / submodule / function. (7) Personal expression / personal convenience refers to personal programming habits. ¹ 9-3: Only references to own storage space. Explanation: If the module package is better, it will generally not generate the space for illegal reference to others. ¹ 9-4: Prevent the memory space that has been released. Note: In the actual programming process, it will appear in a module in a module in a module, while the other module uses it again at some point. To prevent this from happening. ¹ 9-5: The memory allocated in the process / function is released before the process / function exits. ¹ 9-6: The file handle applied for the application (for opening file) in the process / function is to turn off before the process / function exits. Description: The allocated memory is not released and the file handle is not closed, which is a more common error, and it is possible to happen. This type of error tends to cause very serious consequences, and it is difficult to position. Example: Under the function before exiting, the allocated memory is not released. TYPEDEF UNSIGNED CHAR BYTE Int example_fun (byte gt_len, byte * gt_code) { BYTE * GT_BUF; gt_buf = (byte *) malloc (max_gt_length); ... // Program Code, Include Check Gt_buf if or not null. / * Global Title Length Error * / IF (GT_LEN> MAX_GT_LENGTH) { Return GT_LENGTH_ERROR; // Forgot to release GT_BUF } ... // Other Program Code } It should be changed as follows. INT EXAMPLE_FUN (Byte Gt_Len, Byte * GT_CODE) { BYTE * GT_BUF; gt_buf = (byte *) malloc (max_gt_length); ... // Program Code, Include Check Gt_buf if or not null. / * Global Title Length Error * / IF (GT_LEN> MAX_GT_LENGTH) { Free (gt_buf); // Release GT_BUF before quit RETURN GT_LENGTH_ERROR; } ... // Other Program Code } ¹ 9-7: Prevent memory operations. Explanation: The memory operation is mainly to correct the operation of array, pointer, memory address, etc. The memory operation offline is one of the main mistakes of the software system. The consequences are often very serious, so we must be careful when we do these operations. Example: Assuming a software system can be used up to 10 users at the same time, the user number is 1-10, then there is a problem with the following procedure. #define max_USR_NUM 10 Unsigned char usr_login_flg [max_usr_num] = "" Void set_usr_login_flg (unsigned char usr_no) { IF (! usr_login_flg [usr_no]) { USR_Login_FLG [USR_NO] = True; } } When USR_NO is 10, the USR_Login_FLG offline will be used. Can be solved as follows. Void set_usr_login_flg (unsigned char usr_no) { IF (! usr_login_flg [usr_no - 1]) { USR_Login_FLG [USR_NO - 1] = TRUE; } } ¹ 9-8: Carefully handle the various errors that the program can encounter. ¹ 9-9: At the beginning of the system, initialize the variables and operating environments to prevent uninitialized variables from being referenced. ¹9-10: At the beginning of the system, the data loaded into the system is consistently checked. Note: Use inconsistent data, easy to enable the system into confusion and unknown state. ¹ 9-11: It is strictly forbidden to change the settings and configuration of other modules or systems. Explanation: When programming, you cannot change the desired changes to the settings such as constants, arrays, etc. ¹ 9-12: You cannot change the interface with other modules. ¹ 9-13: After fully understanding the interface of the system, use the functionality provided by the system. Example: In the interface functions of the B type machine, there is an initialization process to be written by each module, which is scheduled by the initialization message sent by the operating system after the software system is loaded. Therefore, it involves the order of the type of initialization and the order of the message transmission, especially the news order. If it is not clear, it is easy to cause serious consequences. The following example leads from the actual code that has appeared in the B type, where FID_FETCH_DATA is used to initialize the message type, pay attention to the system of the B-type machine to send FID_INITIAL before FID_FETCH_DATA. MID alarm_module_list [max_alarm_mid]; INT FAR SYS_ALARM_PROC (FID FUNCTION_ID, INT HANDLE) { _UI I, J; Switch (Function_ID) { ... // Program Code Case FID_INITAIL: For (i = 0; i { IF (alarm_module_list [i] == BAM_MODULE / / * *) || (ALARM_MODULE_LIST [i] == local_module) { For (j = 0; j { FAR_Malloc (...); } } } ... // Program Code Break; Case FID_FETCH_DATA: ... // Program Code GET_ALARM_MODULE (); // Initialize ALARM_MODULE_LIST Break; ... // Program Code } } Since FID_Initial is executed before FID_FETCH_DATA, the initialization ALARM_MODULE_LIST is made in FID_FETCH_DATA, so it has not been initialized when referring to the ALARM_MODULE_LIST variable in FID_INITIAL (**), it has not been initialized. This is a serious mistake. Corrected: Or before the Get_Alarm_Module function is placed in the FID_INITIAL (**); or if you must consider whether the judgment statement at (**) can be replaced in other ways (not using alarm_module_list variable), or if this judgment can be canceled Scriptures. ¹9-14: When programming, it should be prevented from errors. Description: This type of error is generally due to the fact that "<=" is mistaken "<" or "> =" is written ">", etc., there is a very serious case, so when programming, Be careful in these places. After the program is completed, these operators should be thoroughly examined. ¹9-15: Always pay attention to confusing operators. When you have completed the program, you should check these operators from the head to prevent spelling errors. Description: The operator in form is most likely caused by misuse, such as "=" and "==", "|" and "||", "&&", etc. The compiler is not necessarily to be checked. Example: such as "&" writes "&&", or vice versa. RET_FLG = (PMSG-> RET_FLG & RETURN_MASK); Write as: RET_FLG = (PMSG-> RET_FLG && RETURN_MASK); RPT_FLG = (Valid_Task_NO (Taskno) && Data_not_zero (stat_data)); Write as: RPT_FLG = (Valid_Task_NO (Taskno) & data_not_zero (stat_data)); ¹ 9-16: It is possible, if the IF statement is used to add the ELSE branch, be careful about the statement without the ELSE branch; the Switch statement must have a default branch. ¹9-17: Under UNIX, the sub-threaded quit of multi-threads must use the active exit method, that is, the sub-thread should be Return exit. ¹9-18: Do not abuse the GOTO statement. Note: The GOTO statement will destroy the structure of the program, so it is best not to use the GOTO statement unless it is true. 1/29-1: Do not use a large statement with hardware or operating system, and use the recommended standard statement to improve software portability and reusability. 1/29-2: Unless to meet special needs, avoid using embedded assembly. Note: Embedded compilation in the program generally has a large impact on portability. 1/29-3: Carefully constructed, dividing the child module, and press the "Interface" section and the "kernel" partly organizes the sub-module to improve the portability and reusability of the "kernel" section. Note: For a module in a different function in different products, if the kernel sections are completely or substantially consistent, then no matter the testing, maintenance of the product, it is a great help to the upgrade of the product. 1/29-4: Careful constructor, and test its performance and efficiency. 1/29-5: It is best to use other algorithms to confirm the comparison of other algorithms. 1/29-6: Note if the expression will overflow, underflow. Example: The following procedure will cause a variable overflow. Unsigned char size; While (size -> = 0) // will overflow { ... // Program Code } When Size is equal to 0, then reduce 1 will not be less than 0, but 0xFF, the program is a dead cycle. Should be modified as follows. CHAR size; // Change from unsigned char to char While (size -> = 0) { ... // Program Code } 1/29-7: Pay attention to the case of its boundary value when using variables. Example: For example, the character variable in the C language, the valid value ranges from -128 to 127. Therefore, the calculation of the following expressions has a certain risk. CHAR CHR = 127; INT SUM = 200; CHR = 1; // 127 is the boundary value of the CHR, and then add 1 will overflow the CHR to -128, not 128. SUM = CHR; // Suds SUM's result is not 328, but 72. If Chr and SUM are the same type, or the expression is written as follows, it may be better. SUM = SUM CHR 1; 1/29-8: Note the size of the machine code size (such as the size of the instruction space, the size, the size of the stack, etc.) exceeds the system. 1/29-9: Provide users with a good interface interface, users can fully understand the internal operational status of the system and the relevant system error. 1/29-10: The system should have certain fault tolerance, and some error events (such as user misoperation, etc.) can be automatically remedied. 1/29-11: Some dangerous operation code (such as writing hard drives, delete data, etc.) should be carefully considered to prevent security constitutes of data, hardware, etc., to improve system security. 1/29-12: When using the software development kit or control provided by third parties, pay attention to the following points: (1) Fully understand the application interface, use the environment and precautions when using it. (2) It cannot be excessively believed in correctness. (3) Unless necessary, do not use unfamiliar third-party toolkits and controls. Description: Using toolkits and controls, speed up program development speed, save time, but you have a sufficient understanding of it before use, and third-party toolkit and controls may have problems. 1/29-13: Resource file (multi-language version support), if resources are sensitive to language, allowing this resource to be detached from source code files, specific methods have the following: use separate resource files, DLL files, or other separate Description files (such as database format) 10 code editing, compiling, review ¹ 10-1: Open all alarm switches of the compiler to compile the program. ¹ 10-2: In the product software (project group), the system is unified to compile the switch option. ¹ 10-3: Check the code through the code reading and review. Note: The code reading is mainly inspected by the program's programming style such as annotations, naming, and so on, and can be checked by the content of the error. It can be carried out by the developer's own or developer; the code review is mainly the functionality and procedures for the program. Inspection and review of stability, safety, reliability, etc., can be carried out by self-trial, cross-review or designated sectoral spotting. ¹10-4: Before the test department tests the product, the code will be taken out and review. 1/210-1: Pay attention to save the code, and back up regularly to prevent code loss due to power-on, hard disk damage. 1/210-2: Within the product software (project group), it is best to use the same editor and use the same setting option. Note: The same project group preferably uses the same intelligent language editor, such as Muiti Editor, Visual Editor, etc., and designed, use a set of indent macros and annotation macros, etc., will be transferred to the editor. 1/210-3: Carefully use the block copy function program provided by the editor. Description: When a certain code is similar to another code processing function, many developers have completed the writing of this code with the block copy feature provided by the editor. Due to similar programs, the variables used, the expressions used, etc. may be very similar in function and naming, so when using block copies, pay attention to the corresponding procedure, be sure to carefully use each variable. View it once to change it correctly. Should not be expected to find all such errors, such as when using global variables, it is possible to hide some miscate. 1/210-4: Reasonably design the software system catalog for developers. Description: Convenient, reasonable software system catalog, improve work efficiency. The principle of directory constructs is convenient for storage, query, compilation, link, etc. of the source program. At the same time, the directory should also have a working directory ---- All compilers, links, etc. should be made in this directory, tool directory - - Related to file editor, file lookup and other tools can be stored in this directory. 1/210-5: Some statements have a alarm after compiling, but if you think it is correct, you should remove the alarm information through some means. Note: In Borland C / C , you can turn off or open some alarms with "#pragma warn". Example: #pragma warn -rvl // Close alarm Int examples_fun (void) { // Program, but there is no Return statement. } #pragma warn rvl // Open alarm Compile functions Examples_FUN The "function should have a return value" alarm, but this alarm prompt will not be generated due to the compilation of this alarm message. 1/210-6: Check the source program using the code check tool (such as a PC-LINT). 1/210-7: Code review using a software tool (such as a logiscope). 11 code test, maintenance ¹11-1: The unit test requires at least the statement coverage. ¹11-2: Unit test begins to track every statement and observe changes in data streams and variables. ¹ 11-3: Clean up, organized or optimized the code after review and testing. ¹11-4: The code version upgrade is strictly tested. ¹ 11-5: Maintain the code version using tool software. ¹ 11-6: Any modification of the software on the official version should have a detailed documentation. 1/211-1: Discover errors to modify immediately, and record it. 1/211-2: The key code is tracked in comparison. 1/211-3: Carefully designed and analyze test cases, so that test use cases are as much as possible to improve the efficiency of test cases. 1/211-4: The various errors of the procedure as possible, fully testing the error handling code. 1/211-5: Carefully test the code processing data, the boundary of the variable. 1/211-6: Keep test information for analysis, summing up experience and more fully testing. 1/211-7: The problem should not be solved by "test" to find the root cause of the problem. 1/211-8: Analyze the automatic disappearance error, how to disappear about the error. 1/211-9: Modifying errors must not only govern the form, but also to cure. 1/211-10: Testing should be tried to make few events often occur frequently. 1/211-11: Confirm that modules or functions processes which events and make them often happen. 1/211-12: Adhere to the coding phase to thorough unit testing of the code, don't wait for future testing to discover problems. 1/211-13: Removal of the randomness of the code (such as the useless data, code, and "internal register", etc.), and pay attention to the "internal register", etc.), allowing the function to run to predict, and make the error can be reproduced. 12 macro ¹12-1: When defining an expression with a macro, use the complete parentheses. Example: There is a certain risk in the macro defined below. #define Rectangle_Area (A, B) A * B #define Rectangle_Area (A, B) (A * B) #define Rectangle_Area (A, B) (A) * (B) The correct definition should be: #define Rectangle_Area (A, B) ((a) * (b)) ¹ 12-2: Place a plurality of expressions defined by the macro in the braces. Example: The following statement is performed only the first expression of the macro. To illustrate the problem, the writing of the FOR statement is slightly unconventional. #define inti_rect_value (a, b) / A = 0; / B = 0; For (INDEX = 0; index INTI_RECT_VALUE (Rect.a, Rect.b); The correct usage should be: #define inti_rect_value (a, b) / {/ A = 0; / B = 0; / } For (INDEX = 0; index { INTI_RECT_VALUE (Rect [index] .a, Rect [index] .b); } ¹12-3: When using the macro, the parameters are not allowed to change. Example: Usage as follows can cause errors. #define Square (a) ((a) * (a)) INT A = 5; INT B; B = Square (A ); // Result: A = 7, that is, an increase of 1 is performed twice. The correct usage is: B = Square (a); A ; // Result: A = 6, that is, only one increase is only performed.