Huawei software programming specification Learning (I)-typographical

Source: Internet
Author: User

Software Programming specification Learning (I)-typographical

1-1: The program block should be written in the indent style. The number of indented spaces is 4.

Note: The code automatically generated by the development tool may be inconsistent.

1-2: Empty lines must be added between relatively independent program blocks and after variable descriptions

Example: The following example is invalid.

int ni;if (!valid_ni(ni)){    ... // program code}repssn_ind = ssn_data[index].repssn_index;repssn_ni  = ssn_data[index].ni;

Should be written as follows

int ni; if (!valid_ni(ni)){    ... // program code}repssn_ind = ssn_data[index].repssn_index;repssn_ni  = ssn_data[index].ni;

1-3: A long statement (> 80 characters) should be divided into multiple lines of books. A long expression should divide a new line at the lower-priority operator, and the operator should be placed at the first of the new line, the new lines should be properly indented to make the layout neat and the statement readable.

Example:

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 );report_or_not_flag =((taskno < MAX_ACT_TASK_NUMBER)                     &&(n7stat_stat_item_valid (stat_item))                     &&(act_task_table[taskno].result_data != 0));

1-4: If a long expression or statement exists in statements such as loop and judgment, the adaptive division is required. Long expressions need to divide new lines at low-priority operators, place operators at the beginning of a new line

Example:

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}

1-5: if the parameters in a function or process are too long, divide them appropriately.

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: it is not allowed to write multiple phrase sentences in one row, that is, only one statement can be written in one row.

Example: The following example is invalid.

rect.length =0;  rect.width = 0;

Should be written as follows

rect.length = 0;rect.width  = 0;

1-7: if, for, do, while, Case, switch, default, and other statements occupy one row, the execution statement of if, for, do, while, and other statements must be enclosed in brackets no matter how many statements are executed {}

Example: The following example is invalid.

if (pUserCR == NULL)return;

It should be written as follows:

if (pUserCR == NULL){   return;}

1-8: Alignment only uses the Space key instead of the tab key.

Note: When reading programs using different editors, the program layout is not neat due to the number of spaces set by the tab key. Do not use BC as the editor combination version, because BC will automatically change Eight spaces into a tab key, most of the versions that are integrated with BC will be indented.

1-9: the code in the statements such as the start of a function or process, the definition of the structure, and the cycle and judgment must adopt the indent style. In case statements, the statement must also comply with the statement indent requirements.

1-10: the boundary of the block (for example, braces '{' and '}' in C/C ++) should be exclusive to each row and be located in the same column, at the same time, it is left aligned with the statements that reference them. At the beginning of the function body, the definition of the class, the definition of the structure, the definition of enumeration, and the programs in the IF, for, do, while, switch, and case statements must adopt the above indent method.

Example: The following example is invalid.

for (...) {   ... // program code}if (...)    {   ... // program code    }void example_fun(void )    {   ... // program code    }

It should be written as follows.

for (...){   ... // program code}if (...){   ... // program code}void example_fun(void ){   ... // program code}

1-11: When two or more keywords, variables, and constants perform peer-to-peer operations, spaces must be added before, after, or before and after the operators between them. For non-peer operations, if it is an immediate operator (such as->) That is closely related, no space should be added.

Note: The purpose of writing code in this loose way is to make the code clearer.

Since the clarity produced by leaving spaces is relative, there is no need to leave spaces in statements that are already very clear, if the statement is clear enough, no space is required for the inside of the brackets (that is, the front of the left and right brackets), and no space is required for the multiple parentheses, because brackets in C/C ++ are already the clearest sign.

In a long statement, if you need to add a lot of spaces, you should keep the overall clarity, without spaces. Do not leave more than two consecutive spaces for the operator.

Example:

(1) commas and semicolons are only followed by spaces.

int a, b, c; 

(2) Comparison operators, value assignment operators "=", "+ =", Arithmetic Operators "+", "%", logical operators "&&","&", spaces are added before and after binary operators, such as bitwise operators <"," ^ ", and so on.

if (current_time >= MAX_TIME_VALUE)a = b + c;a *= 2;a = b ^ 2;

(3 )"! ","~ "," ++ "," -- "," & "(Address operator), and other single-object operators do not contain spaces.

* P = 'a'; // content operation "*" and content flag =! Isempty; // non-operation "! "And content between P = & MEM; // address operation" & "and content between I ++; //" ++ "," -- "and content

(4) "->", "." Without spaces.

P-> id = PID; // "->" no space before and after the pointer

(5) If, for, while, switch, and so onSpaces should be added between the brackets to make keywords such as if more prominent and obvious.

if (a >= b && c > d)

Note:It is recommended that a program be less than 80 characters long.

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.