catalogue
preface
Schools have taught us how to write code, but they have not taught us how to write code in a standardized way. In programming, it is very important to have a good code writing specification. The standardized code can reduce bugs, strong readability and later maintenance. The following describes the considerations for writing canonical code.
1, Typesetting
1. The program block shall be written in indentation style as far as possible, and the number of indented spaces is recommended to be 4.
Note: the code automatically generated by different development tools can be inconsistent.
2. A blank line must be added between relatively independent program blocks and after the variable description.
Example: The following examples do not meet the specifications
if (!valid_ni(ni)) { ... // program code } repssn_ind = ssn_data[index].repssn_index; repssn_ni = ssn_data[index].ni;
As written below
if (!valid_ni(ni)) { ... // program code } repssn_ind = ssn_data[index].repssn_index; repssn_ni = ssn_data[index].ni;
3. Long statements (> 80 characters) should be written in multiple lines, and long expressions should be divided into new lines at low priority operators,
Operators shall be placed at the head of new lines, and the divided new lines shall be properly indented to make the layout neat and the statements readable.
perm_count_msg.head.len = NO7_TO_STAT_PERM_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 );
4. If there is a long expression or statement in the statements such as loop and judgment, it shall be divided appropriately, and the long expression shall be in the low level
A new line is divided at the priority operator, and the operator is placed at the top of the new line.
if ((taskno < max_act_task_number) && (n7stat_stat_item_valid (stat_item))) { ... // program code } for (i = 0, j = 0; (i < BufferKeyword[word_index].word_length) && (j < NewKeyword.word_length); i++, j++) { ... // program code } for (i = 0, j = 0; (i < first_word_length) && (j < second_word_length); i++, j++) { ... // program code }
5. If the parameters in a function or procedure are long, they should be divided appropriately
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 );
6. It is not allowed to write multiple phrase sentences in one line, that is, only one sentence in one line.
The following examples are out of specification
num.data = 0; num.size = 0;
Because it is written as follows
num.data = 0; num.size = 0;
7, If, for, do, while, case, switch, default and other statements occupy one line, and if, for
No matter how many execution statements of do, while and other statements are, parentheses should be added {}.
The following do not conform to the specifications
if (time > time_1s) LED1_ON;
Because it is written as follows
if (time > time_1s) { LED1_ON; }
8. Alignment uses only the spacebar, not the TAB key.
Note: to avoid the program layout caused by the different number of spaces set by TAB key when reading the program with different editors
If it is not neat, do not use BC as the combined version of the editor, because BC will automatically change 8 spaces into a TAB key,
Therefore, most versions that use BC merge will confuse the indentation.
9. The beginning of a function or procedure, the definition of a structure, and the code in statements such as loops and judgments should all be indented
The case processing statement under the statement should also comply with the statement indentation requirements.
10. There must be a certain boundary between program blocks.
11. A line should be 80 characters, not too long, too long is not conducive to reading.
2, Notes
1. Generally, the effective annotation of the source program is more than 20%, which should not be too much or too little. The purpose of annotation is to help the program read. The annotation must be accurate, easy to understand and concise
2. The header of descriptive files (such as header file. h file,. inc file,. def file, compilation description file. cfg, etc.) shall
The notes must list: copyright description, version number, generation date, author, content, function, and other documents
Relationship, modification log, etc. the comments of the header file shall also have a brief description of the function.
The header file comments can refer to the following, but are not limited to this
/************************************************* Copyright (C), 1988-1999, Huawei Tech. Co., Ltd. File name: // file name Author: Version: Date: // Author, version and completion date Description: // Used to describe in detail the main functions completed by this program file, and other modules // Or function interface, output value, value range, meaning and control between parameters // System, order, independence or dependence, etc Others: // Description of other contents Function List: // List of main functions. Each record shall include function name and brief description of function 1. .... History: // Modification history list. Each modification record shall include modification date and modification date // Brief description of the author and modification 1. Date: Author: Modification: 2. ... *************************************************/
3. The header of the source file shall be annotated, listing: copyright description, version number, generation date, author, module purpose / function
Main functions and their functions, modification log, etc.
The following source file comments are very standard and can be used as a reference.
/************************************************************ Copyright (C), 1988-1999, Huawei Tech. Co., Ltd. FileName: test.cpp Author: Version : Date: Description: // Module description Version: // Version information Function List: // Main functions and functions 1. ------- History: // History modification record <author> <time> <version > <desc> David 96/10/12 1.0 build this moudle ***********************************************************/
4. The function header shall be annotated to list the purpose / function of the function, input parameters, output parameters, return value and call
Relationships (functions, tables), etc.
/************************************************* Function: // Function name Description: // Description of function, function, performance, etc Calls: // List of functions called by this function Called By: // List of functions calling this function Table Accessed: // Accessed table (this is only for programs involved in database operations) Table Updated: // Modified table (this item is only applicable to programs involved in database operation) Input: // Input parameter description, including the operation of each parameter // Use, value description and relationship between parameters. Output: // Description of the output parameters. Return: // Description of the return value of the function Others: // Other instructions *************************************************/
3, Identifier naming
1. The name of the identifier shall be clear, clear and have clear meaning. At the same time, complete words or words that can be understood by everyone shall be used
Abbreviations to avoid misunderstanding.
2. Special and abbreviations are used in naming, and notes are required.
3. Their own unique style should be consistent and cannot be changed back and forth.
4. It is forbidden to use single characters (such as i,j,k,x,y, etc.), and it is recommended to use identifiers that can represent specific meaning and variable type.
summary
Standard code is not practiced in a day or two. We need to pay more attention to typing code and form good habits.