Programming specification - programmers should write code this
basic requirements
1.1 Program structure is analyzed, simple and easy to understand, the number of lines of individual functions must not exceed 100 lines. 1.2 What is going to do, it is simple, directly when it is straight, and the code is streamlined, and the garbage program is avoided. 1.3 Try to use the standard library function and public functions. 1.4 Do not define global variables at will, try to use local variables. 1.5 Use parentheses to avoid amphibia.
2. Readability requirements 2.1 readability first, efficiency second. 2.2 Hold the comment is exactly the same as the code. 2.3 Each source file has a file header, indicating that the specification is specified. 2.4 Each function has a function head description, indicating that the specification is specified. 2.5 Main variables (structures, joints, class or objects) are defined or referenced, the comments can reflect their meaning. 2.7 Constant Definition (Define) has a corresponding description. 2.8 Each phase of the process has a related note instructions. 2.9 There is a comment before the typical algorithm. 2.10 Using the logical structure of the display program, the indentation volume is consistent, and the Tab is 6 bytes. 2.11 Circulation, the branch level should not exceed five layers. 2.12 Note You can be in the same row with the statement, or you can go up. 2.13 Blank Characters is also a special annotation. 2.14 At a glance, the statement does not comment. 2.15 The scope of the annotation can be: definition, reference, conditional branch, and a piece of code. 2.16 Note (not including the program head and function header) should account for 1/5 to 1/3 of the total number of lines.
3. Structured requirements
3.1 Prohibition of two equivalent branches. 3.2 Prohibiting the goto statement. 3.3 Use the IF statement to emphasize only one of the two groups of statements. Do Else Goto and else return. 3.4 Use CASE to implement multiple branches. 3.5 Avoiding multiple exits from cycles. 3.6 The function has only one exit. 3.7 Do not use the condition assignment statement. 3.8 Avoid unnecessary branches. 3.9 Do not use the conditional branch to replace logic expressions.
4. Correctness and fault tolerance requirements
4.1 The program is first correct, followed by beautiful 4.2 Unable to prove that your program has no error, so after writing a program, you should first check. 4.3 Change an error may generate a new error, so the impact on other programs will be considered before modifying. 4.4 All variables must be initialized before calling. 4.5 Enter all user inputs must be checked. 4.6 Do not compare the equal number of floating point numbers, such as: 10.0 * 0.1 == 1.0, not reliable 4.7 Programs and environmental or status relationships, must actively deal with accidents, such as whether the file can be logically locked, whether the printer is online. 4.8 Unit test is also part of programming, and the procedure for submitting a joint test must pass through unit testing.
5. Reusability requirements
5.1 Repeatedly used algorithms or code relative to independent features should be abstract as a common control or class. 5.2 Public controls or categories should consider OO ideas, reduce external contacts, consider independence or encapsulation. 5.3 Public controls or classes should establish a template.
Attached: C programming specification, Delphi made a corresponding reference
.1 scope of application
This standard applies to people developed by Visul C , Borland C ..
.2 variable naming
Naming must have a certain actual meaning, form is xabcfgh, X by variable type, ABC, FGH represents a continuous meaning string, if the consecutive string is only two, can be capitalized. Such as OK.
Specific routine:
Bool type benable;
CH * char chtextc * class object CMAIN (Object instance) h * handle (handle) hwndi * intN * No symbol integer p * pointer SZ, str * string W Wordx, y coordinate
CHAR or TCHAR Type The Filename [10] in the form of the Windows API, in the form of a single character can also be represented by a lowercase letter; int type ncmdshow; long type lparam; uint type unotify; dWord type DWStart; PSTR type pszTip; LPSTR type lpCmdLineLPTSTR type lpszClassName; LPVOID type lpReservedWPARAM type wParam, LPARAM type lParamHWND type hDlg; HDC type hDC; hINSTANCE type hInstanceHANDLE type hInstance, HICON type hIcon; int iTmpfloat fTmpDWORD dw * String, AnsiString str * m_ class member variables m_nVal , m_bflagg_ global variable g_nmsg, g_bflag
Several general variables can be used in local variables: NTEMP, NRESULT, I, J (generally used for cyclic variables).
Other resource handles
.3 constant naming and macro
Constants and macro definitions must have a certain actual meaning; constant and macro definitions between #include and function definitions; constants and macro definition must be written in uppercase letters, the intermediate can be connected to the continuity of meaning, each defined right side There must be a simple annotation to explain its role; the resource name definition format:
Menu: IDM_XX or CM_XX Bitmap: IDB_XX Dialog: IDD_XX String: IDS_XXDLGINIT: DIALOG_XXCION: IDR_XX
.4 function name
Function prototype instructions include reference external functions and internal functions, external references must be indicated on the right side: module name and file name, such as internal functions, as long as the file name is noted;
The first letter must use uppercase letters, requiring the case where the uppercase letter combination specification function is named, if necessary, the interval can be used, as follows:
void UpdateDB_Tfgd (TRACK_NAME); // Module Name: r01 / sdw.cvoid PrintTrackData (TRACK_NAME); // Module Name: r04 / tern.cvoid ImportantPoint (void); // Module Name: r01 / sdw.cvoid ShowChar (int, INT, ChTYPE); // Local Modulevoid Scrollup_v (int, int); // Local Module
.5 structural naming
The structural type naming must all be used in uppercase letters, in principle, the next step is started; the structural variable naming must be combined with uppercase, the first letter must use uppercase letters, if necessary, can be used underline interval. For private data zones, the process thereof must be indicated. Global data definitions simply pay attention to their use.
Examples are as follows:
Typedef struct {char szproductname [20]; char szauthor [20]; char szvesedate [16]; char szversion [10]; unsigned long maxTables; unsigned long.comles;} dbs_database;
DBS_DATABASE GDATABASE;
6 Control Name: Indicated Category: FM Window CMD button COB Combo, drop-down list box TXT text input box Labal, tag img image, image Pic Picuregrd Grid, Grid SCR Scroll Bar LST list box FRM FRAM
7 note
In principle, annotations require Chinese;
File start comments include: company name, copyright, author name, time, module purpose, background introduction, etc., complex algorithms need to be added to process descriptions; function comments include: input, output, function description, process processing, global variable, call Sample, etc., complicated functions requires a variable application description; the comments in the program include: amendment time and authors, easy-to-understand annotations; reference 1: Note Template at the beginning of the file
/ ************************************************** ****************** File Name: ** Copyright (C) 1998-1999 ******** Company Technology Development Department ** Created: * * Date: ** Modifier: ** Date: ** Description: **** Version: ** -------------------------- -------------------------------------------------- - ********************************************************** ***************** /
Quote 2: Note Template at the beginning of the function / ************************************************* ************************* function name: ** input: a, b, c ** a --- ** b --- ** c --- ** Output: x --- ** x is 1, indicating ... ** x is 0, indicates ... ** Function Description: ** Global Variable: ** Call Module: * * author: * date: * modified: date **: ** version ******************************** ****************************** /
Quaternarian: Note Template in the program / * -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------ * // * Comment content * // * ---------------------- ------------------------------------ * /
8 program
a. Program coding strikes simple, clear structure, avoid too much branch structure and too skillful procedures, try not to use recursive mode. b. When writing a program, you must also want to test the method. In other words, the test plan of "unit test" should be equated to write at the program. c. Note must be consistent with the program. d. Modifications after version storage must use the old statement to / * * / closed, unable to delete or modify itself, and record them in the modification record of the file and function. e. The beginning "{" and "}" of each block in the program must be aligned, nesting blocks, indented a Tab, Tab is 4 space, block type includes if, for, while, do, etc. Keywords are introduced. f. For a relatively large function, each block and special function call must be indicated, for example, as follows:
Count.divisor = 1193280 / freq; // compute the project 67, (unsigned char) 182); // Tell 8253 That ACOUNT IS COMINGOUTBYTE ((UNSIGNED SHORT) 66, Count. C [0]) ; // send low-order byte hostbete ((unsigned short) 66, count. c [1]); // send high-order byte ×××××××××××××××××××× ×××××××××××××××××××××
BCB, variables in Delphi Name:
Follow the Hungarian nomenclature, naming must make sense, formulate the following