Programming writing rules

zhaozj2021-02-16  49

Programming writing rules

He Zhi Dan collects the process of writing programs, especially when multiplayer programming, the standardized program writing format can improve the readability of the program, improve the efficiency of writing programs, and make the writes have higher portability. In the process of many years of software development, the company summarizes many rules that make the program clearer and readable and gradually form their own programming style. Comprehensive understanding and familiar with these rules, and strictly comply with these rules in actual programming, it is very important for each programmer. The following is the rules that should follow during the programming process: 1. Basic requirements 1.1 file name (excluding the extension) does not exceed 8 characters, that is, try not to use long file names. 1.2 Tab is usually set to 8, and the Tab character should be spaced. 1.3 Generally, the definition of class, structure, enumeration, the declaration of the function is placed in. The 1.4 function, the implementation of the member function is put in. CPP, do not put them in .h. (Except in the inline function) 1.5 The dividing statement is indented to two characters. For example: if (i == 10) value = 10; 1.6 List of columns Generally, the columns need to be written from 8N ​​ 1 column from the list. A column that needs to be written in a file is written from the same column. 1.7 Local variables as possible to define the place to use, do not define all in the beginning of the file. 1.8 When designing the data structure, you must check 4 bytes in the compilation option in the compilation option. When you write, press 8 bytes to write. When there is byte flight out, use the REV to indicate so that this space is first used when you need space. For example: struct structa {byte a; Byte Rev [3]; DWORD C;} 1.9 #include "Headerfile.h" The write header file of the write header file is written in the same order. After all files are written after all files, use the #inclde hdrstop to identify. For example: A.CPP in B.CPP #include #include #pragma hdrstop # prgma hdrstop # include #include #include #include ... 2. Program framework here: The program framework is to reflect the program structure, and the source file is divided into the source file in the form of a note. Specific method: 2.1 Module Naming module name should make sense, clearly indicating the functionality of the module. If a module is just a class, the module name should be "Class Class Name". For example: Class BaseWnd2.2 module declares lists all defined module names after pre-processing. The module name should be written. If the sub-module is defined in the module, the sub-module name is written relative to its parent module. For example: / * (Uder Writing) Module1 Name Module2 Name Child Module Name (PR) ............... * / 2.3 Module Definition of the module's start / end position with module with begin / END method is indicated. The format is as follows: // module1 name begin ... (module1) // module1 name end2.4 CPP file Declaration and definition must be written at the same time, and if the module is just a class in the H file, you can Only write the declaration part. 2.5 Entrance (import): The function of other modules used in the module may be changed. 2.6 Exit: The function used by other modules is used.

Third, Note 3.1 Note The symbol and program code are placed in the same line, and should be separated by more than one space and program code. 3.2 "//" There is an empty part between the comment literals. 3.3 Note Use "//" should be written as much as possible. 3.4 Uncomfortable part must be annotated. Fourth, name 4.1 Should be named with a specific meaning, which can reduce comments. 4.2 Variable names and function names are written in XXXXXX format. In addition to the circulating variable, the variables of a single letter should also be represented by uppercase letters. For example: MaxItems, M4.3 cyclic variables can be named using a single lowercase letter. For example: for (int i = 0; i <10; i ) array [i] = 0; 4.4 Prerequisites All letters in the macro name should be capitalized, and the middle can be separated by "_", even if the XXX_XXX format is used. The definition of a constant macro and variable macros use this method. Such as constant max_path, pi4.5 class name, structural name, enumeration name will be written in XXXXXXXX format. If only one word consists, the entire word is uppercase (XXX format). For example: CreateFontInfo4.6 has only the following data type variable name plus prefix, other types of non-prefix: prefix P: pointer type variable name; prefix H: handle type variable name; prefix B: Boolean variable name. 4.7 class members variables and functions cannot be additionally prefixed. 4.8 Parameter Naming of Functions No Use In / OUT to identify incoming / out. V. Expression 5.1 The expression of priority needs to be judged, "()" is used to indicate different priorities so that the program is read. 5.2 When the expression is too long, when you write (more than 80 characters), it should be divided into several lines. 5.3 The basic rules of dividing expressions are as follows: After a low priority operator, it begins to divide. Place this operator on the new line. Make the new line relative to the first line of the statement. If the expression is divided into 3 rows, each line starts from the third line and the second line. For example: value = theseDatabase.search (userQuery-> criterium, currentations.domain, ...); 5.4 When dividing the expression from parenthesis, the new line can be written to "(" the contents of "("). For example, : Value = theDatabase.search (userQuery-> criterium, currentsion.domain); 5.5 When assigning expression, you can also let the new row and "=" after writing .Value = MaxNumber (Number1, Number2) - MinNumber Number3, Number4); 5.6 If the expression contains a function call, you should divide it as much as possible to call it to ensure the integrity of the function call. Unless the function's call part is very long, it must be divided. For example: Do not in the previous example Divided value = maxNumber (Number1, Number2) - MinNumber (Number3, Number4) as follows; 5.7 When the conditional expression is divided, the new line and the expression start position is equal. For example: if (Condition == Green && status == OK) Grantclearancce (); six, statement 6.1 Basic rules 6.1.1 Continuous two spaces are not allowed in the expression. 6.1.2 in ",", ";" The left side of the symbol can't have spaces, but the right side is spaced.

For example: for (INT I, J = 0; I> 4; i ) array [i] [j] = 0; 6.1.3 The two-dimensional calculative operator, the relationship operator is left and right. But if one of the operands is a small number, don't be spaced. For example: sum = number1 number2; sum = number 5; 6.1.4 No space between one dimensional operator and operand. For example: i 6.1.5 Indirect access operator "*" and add address operators "&", right on the right. They are spaced between the left data types. For example: void function (CHAR * PSTRING, INT & NUM); FUNCTION (& Buffer, Number); "*", "&" variable definitions and declared column alignment methods as shown in the following example: int Num; char * pstr; int & Value; 6.1.6 "Arrow" operator "->" is not spaced. 6.1.7 Domain Operators No spaces around the "::". 6.1.8 Unrecognizable Names and Neglings are not spaced between "[". For example: Array [3] 6.1.9 Brand Use Rules Use Cigpex "{" and "}", put them on a separate line, to improve program readability. Brand is aligned with the statement that references it. The text in the braces should be written. For example: While (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);} 6.1.10 Continuous writing statement should not exceed 10 lines, to be empty in the appropriate place. 6.1.11 The statements of different uses should be properly empty. 6.1.12 You have to be empty before and after many rows of statements. 6.1.13 Do not exceed one line each time. 6.1.14 Try to write a definition of constants together. 6.1.15 Preprocessing part is placed in the beginning of the source program. For example: #include 6.1.16 The pre-processing instruction is divided into multi-line, and the "/" after each line should be written. #Define get_max_num (a, b) / if (a> b) / RET = a; / else / Ret = B; 6.1.17 if, while, for, do-while, switch, casse statement One line, centered on braces. Written by the use rules of braces. 6.1.18 if, while, if the statement is only a statement, there is no parentheses. 6.1.19 Statements should be written. 6.1.20 Statement keywords and after "(", there is no space. 6.1.21 The statement is integrated as much as possible, and the portion that can be written is not written. For example: return (RET); it should be written as return return; The expression can be included in the brackets, or you can do not write this pair of parentheses. At this time, it is required to write a small bracket. 6.1.22 Array definition array is defined in one line, array content and "{", " } "There is a space between" = "and" {".

For example: int Array [] = {1, 2, 3}; When the contents of the array need to write multiple lines, the first line is divided from "=", and the specific content is written separately, and other rows from " "" Before starting to divide, write, "{", "}" accounts for a separate line. For example: char Array [] [20] = {"String1", "String2", "String3", "String4"}; 6.1.23 GOTO statement as little as possible. 6.2 for statement 6.2.1 In the expression of the for statement, the left sign is empty. 6.2.2 When dividing the expression of the FOR statement, it should be divided after the semicolon position in the for statement should be divided. 6.2.3 When the FOR statement is divided, if only the second expression is very long, you can only divide two expressions and write the statement into two lines. For example: for (i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-25731.html

New Post(0)