Programming specifications-programmers should write code like this (reprinted: http://www.360doc.com/content/051128/22/73_37113.html)

Source: Internet
Author: User

 

 

{
NewHighlight (event)
} "Class =" wenzhang_con "id =" articlecontent "style =" width: 740px "> Basic Requirements

1.1 program structure analysis, easy to understand, the number of program lines of a single function cannot exceed 100.
1.2 What are you going to do? It should be simple, straightforward, and code should be streamlined to avoid spam.
1.3 try to use Standard Library functions and public functions.
1.4 do not define global variables at will. Use local variables whenever possible.
1.5 use parentheses to avoid ambiguity.

2. Readability requirements
2.1 readability first, efficiency second.
2.2 keep the comments exactly the same as the code.
2.3 Each source program file has a description of the file header. For details about the specifications, see the specifications.
2.4 each function has a description of the function header. For details about the specifications, see the specifications.
2.5 comments can reflect the meaning of a major variable (structure, union, class, or object) when defining or referencing it.
2.7 DEFINE a constant.
2.8 each stage of the processing process is annotated.
2.9 annotations are provided before typical algorithms.
2.10 use indentation to display the logical structure of the program. The indentation quantity is consistent and the unit is the Tab key. The Tab is defined as 6 bytes.
2.11 Loop And the branch level should not exceed five layers.
2.12 comments can be in the same line as the statement or in the upstream.
2.13 empty lines and white spaces are also special comments.
2.14 clear statements without comments.
2.15 Annotated Function The range can be definition, reference, condition branch, and a piece of code.
2.16 The number of comments (excluding program headers and function headers) accounts for 1/5 to 1/3 of the total number of lines.

3. Structured requirements

3.1 two equivalent branches are prohibited.
3.2 disable the GOTO statement.
3.3 Use the IF statement to emphasize that only one of the two groups of statements is executed. Disable else goto and else return.
3.4 use CASE to implement multiple branches.
3.5 avoid multiple exits from the loop.
The 3.6 function has only one exit.
3.7 The conditional assignment statement is not used.
3.8 avoid unnecessary branches.
3.9 do not replace logical expressions with conditional branches.

4. correctness and fault tolerance requirements

4.1 programs are correct first, followed by beautiful
4.2 it cannot be proved that your program is not wrong. Therefore, after writing a program, you should check it back.
4.3 A new error may occur when an error is changed. Therefore, the impact on other programs should be considered before modification.
4.4 all variables must be initialized before being called.
4.5 all user input must be checked for validity.
4.6 do not compare equal floating point numbers,
For example: 10.0*0.1 = 1.0, unreliable
4.7 when a program is related to the environment or status, it must take the initiative to handle unexpected events, such as whether the file can be logically locked or whether the printer is online.
4.8 unit testing is also a part of programming. Programs that submit joint debugging tests must pass unit testing.

5. reusability requirements

5.1 duplicate algorithms or code used to complete relatively independent functions should be abstracted as public controls or classes.
5.2 OO ideas should be taken into account for public controls or classes to reduce external connections and to consider independence or encapsulation.
5.3 a template should be created for public controls or classes.

Appendix: C ++ programming specifications, delphi for Reference

. 1 Applicability

This standard applies to developers who develop software programs using objective l c ++ and Borland C ++ ..

. 2 variable naming

The name must have a certain practical significance, in the form of xAbcFgh, x is determined by the variable type, Abc, Fgh represents a continuous meaning string, if only two consecutive meaning strings, can be capitalized. Such as OK.

Specific routine:

BOOL type bEnable;

Ch * char chText
C * Class Object cMain (object instance)
H * Handle (Handle) hWnd
I * int
N * unsigned integer
P * pointer
Sz, str * string
W WORD
X and y coordinates

If the Char or TCHAR type is directly related to the Windows API, use the szAppName [10] format. Otherwise, use the FileName [10] format. A single character can also be expressed by lowercase letters;

Int type nCmdShow;
Long type lParam;
UINT type uNotify;
DWORD type dwStart;
PSTR type pszTip;
LPSTR type lpCmdLine
LPTSTR type lpszClassName;
LPVOID type lpReserved
WPARAM type wParam,
LPARAM type lParam
HWND type hDlg;
HDC;
HINSTANCE type
HANDLE type hInstance,
HICON type hIcon;
Int iTmp
Float fTmp
DWORD dw *
String, AnsiString str *
M _ class member variables m_nVal, m_bFlag
G _ global variable g_nMsg, g_bFlag

The following common variables can be used in local variables: nTemp, nResult, I, J (usually used for cyclic variables ).

Other resource handles are the same as above

. 3 constant naming and macro definition

Constants and macros must have some practical significance;
Constants and macros are defined between # include and function definitions;
Constants and macro definitions must all be written with uppercase letters, and can be connected with underscores Based on the continuity of meaning. A simple comment must be provided on the right of each definition to explain its function;
Resource Name Definition Format:

Menu: IDM_XX or CM_XX
Bitmap: IDB_XX
Dialog Box: IDD_XX
String: IDS_XX
DLGINIT: DIALOG_XX
ICON: IDR_XX

. 4 function naming

The function prototype description includes reference of external and internal functions. The function Source: Module name and file name must be indicated on the right side of the external reference. If it is an internal function, you only need to comment out its definition file name;

The first letter must contain uppercase letters. A combination of uppercase and lowercase letters must be used to name a function. If necessary, an underline interval is available. For example:

Void UpdateDB_Tfgd (TRACK_NAME); // Module Name: r01/sdw. c
Void PrintTrackData (TRACK_NAME); // Module Name: r04/tern. c
Void ImportantPoint (void); // Module Name: r01/sdw. c
Void ShowChar (int, int, chtype); // Local Module
Void ScrollUp_V (int, int); // Local Module

. 5 struct name

The name of the struct type must contain uppercase letters. In principle, the name of the struct type must start with an underscore (_). The variable name must be a combination of uppercase and lowercase letters. The first letter must contain uppercase letters. If necessary, the underline interval is used. For a private data zone, you must specify the process to which it belongs. You only need to pay attention to the usage of global data definition.

Example:

Typedef struct
{
Char szProductName [20];
Char szAuthor [20];
Char szReleaseDate [16];
Char szVersion [10];
Unsigned long MaxTables;
Unsigned long UsedTables;
} DBS_DATABASE;

DBS_DATABASE GdataBase;

6. Control name:
Use a lower-case prefix to indicate a category:
Fm window
Cmd button
Cob combo, drop-down list box
Txt text input box
Lab labal, label
Img image, image
Pic picture
Grd Grid, Grid
Scr scroll bar
Lst list box
Frm fram

7. Notes

In principle, the annotations must be in Chinese;

The initial comments of the file include: company name, copyright, author name, time, module purpose, Background introduction, etc. For complex algorithms, a process description must be added;
Function comments include: input, output, function description, process processing, global variables, and call samples. For complex functions, you need to add variable usage instructions;
Comments in the program include: modification time and author, easy to understand comments, etc;

Reference 1: annotation template at the beginning of a file

/*************************************** ***************************
** File Name:
** Copyright (c) 1998-1999 ********** Technology Development Department of the company
** Creator:
** Date:
** Modifier:
** Date:
** Description:
**
** Version:
**-----------------------------------------------------------------------------
**************************************** **************************/

Reference 2: annotation template at the beginning of a function
/*************************************** **************************
** Function Name:
** Input: a, B, c
** ---
** B ---
** C ---
** Output: x ---
** X is 1, indicating...
** X is 0, indicating...
** Function description:
** Global variables:
** Call module:
** OPERATOR:
** Date:
** Modification:
** Date:
** Version
**************************************** ************************/

Reference 3: annotation template in the program
/*----------------------------------------------------------*/
/* Comment */
/*----------------------------------------------------------*/

8 programs

A. program coding strives to be concise and clear, avoiding too many branch structures and too skillful programs, and avoiding using recursive modes as much as possible.
B. When writing a program, you must also think about the testing method. In other words, the unit test plan should be prepared together during programming.
C. The annotation must be consistent with the program.
D. The old statement must be closed with/**/after version archive. The old statement cannot be deleted or modified on its own and must be recorded in the file and Function Modification records.
E. "{" and "}" at the beginning of each block in the program must be aligned. each nested block has a set of tabs with four spaces, the block type includes keywords such as if, for, while, and do.
F. For a relatively large function, each block and special function call must specify its function. For example:

Count. divisor = 1193280/freq; // compute the proper count
OutByte (unsigned short) 67, (unsigned char) 182); // tell 8253 that acount is coming
OutByte (unsigned short) 66, count. c [0]); // send low-order byte
OutByte (unsigned short) 66, count. c [1]); // send high-order byte
××××××××××××××××××××××××××××××××××××××××××

Name the variable in bcb and delphi:

Follow Hungary Naming rules must be meaningful.

Form: Start with W in uppercase. For example, the copyright form of About is named WAbout.
File: start with an uppercase value of F, such as the About copyright form. The file is named FAbout. cpp.
Button: for example, the exit Button is named btnExit.
......
Base Class: adds a base tag, such as a report base class. The form is named WBaseRep and the file is named FBaseRep. cpp.

 

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.