国外的一篇讲述编码风格的,可以参考一下,原文地址 http://www.webdbtips.com/32042/
Basic requirements
1.1 Explicit program structure, easy to understand, a single function of the number of program lines must not exceed 100 lines.
1.2 going to do, should be simple, straightforward, streamlined code to avoid spam program.
1.3 make use of the standard library functions and public functions.
1.4 do not arbitrarily define global variables to make use of local variables.
1.5 Use parentheses to avoid ambiguity.
2 readability requirements
2.1 readability and efficiency.
2.2 comments and the code is exactly the same.
2.3 for each source file, the file has been head instructions, specifications see specification.
2.4 Each function has a function header description, specifications see specification.
2.5 main variables (structure, union, class or object) definition or reference, comments reflect its meaning.
2.7 constant definitions (define) the appropriate instructions.
2.8 each stage of the process are related to the explanatory notes.
2.9 in typical algorithms ago have comments.
2.10 using indentation to show the logical structure of the program, indent consistent Tab key units of the definition of Tab 6 bytes.
2.11 looping, branching level should not exceed five.
2.12 Notes with the statement on the same line, in the upstream.
2.13 Empty lines and whitespace characters is also a special comment.
2.14 at a glance the statement without comment.
2.15 Notes scope: definitions, references, conditional branching, as well as a piece of code.
2.16 Number of comment lines (not including the program header and the function header part) the total number of rows 1/5 to 1/3.
Structured requirements
3.1 prohibits two equal branch.
3.2 prohibits the GOTO statement.
3.3 IF statement to emphasize that only the implementation of a set of two sets of statements. Prohibited ELSE GOTO and ELSE RETURN.
3.4 CASE multi-channel branch.
3.5 avoid leads to multiple exits from the loop.
3.6 function only one exit.
3.7 No conditional assignment statement.
3.8 to avoid unnecessary branch.
3.9 Do not use conditional branch to replace the logical expression.
Correctness and fault tolerance requirements
4.1 program first, followed by the beautiful
4.2 can not prove that your program is no error, finish writing a program, you should check back.
4.3 to change an error may occur when a new error, first consider the impact on other programs before it is modified.
4.4 All variables must be initialized before the call.
4.5 All user input must check for legitimacy.
4.6 Do not compare floating-point numbers are equal,
: 10.0 * 0.1 == 1.0, unreliable
4.7 program with the environment or state relationship, you must take the initiative to deal with the occurrence of unexpected events, such as whether the file can logic locked, the printer is online.
4.8 unit testing is also part of the programming, the program must be submitted to the FBI test unit tests.
5 reusable requirements
5.1 reusable completed relatively independent function algorithm or code should be abstracted as public control or class.
5.2 Common Controls or classes should be considered OO thinking, reduce the external contact consider independence or packaging.
5.3 Common Controls or classes should be established using a template.
Appendix: C + + programming specification, Delphi corresponding reference
.1 Scope
This standard applies to the Visul C + +, Borland C + + software application development personnel.
.2 Variable named
The name must have some practical significance, in the form xAbcFgh, x is determined by the variable type, abc, FGH said the continuous significance strings consecutive significance only two strings can be capitalized as OK.
Specific routines:
BOOL type bEnable;
CH * char chText the
c * the class object CMAIN (object instance)
h * Handle (handle) HWND
i * int
n * unsigned integer
p * pointer
sz, str * string
w WORD
x, y coordinates
The Char or TCHAR type with the Windows API directly linked with szAppName [10] form otherwise
FileName [10] form a single character can be expressed in lowercase letters;
Int type nCmdShow;
Of type LONG lParam;
UINT type uNotify;
Type DWORD dwStart;
The the PSTR type pszTip;
The LPSTR Type lpCmdLine
LPTSTR type, lpszClassName;
The LPVOID Type lpReserved
Type of WPARAM wParam,
The LPARAM type of lParam
Type HWND hDlg;
Type HDC hDC;
The HINSTANCE type of hInstance
Type HANDLE hInstance,
HICON type hIcon;
int ITMP
float fTmp
DWORD dw *
String, AnsiString str *
The m_ class member variables m_nVal, m_bFlag
g_ global variables g_nMsg, g_bFlag
Local variables can be used in follows several common variable: nTemp, nResult, I, J (generally used for the loop variable).
Other resource handle ibid.
.3 Constant naming and macro definitions
Constants and macro definition must have some practical significance;
Constant and macro definitions between # include and function definitions;
Constant and macro definitions must be in all uppercase letters to write, the middle according to the sense of continuity with the underscore, the right side of each definition must have a simple notes indicating its role;
Resource name defined format:
The menu: IDM_XX or CM_XX
Bitmap: IDB_XX
Dialog box: IDD_XX the
String: IDS_XX the
DLGINIT: DIALOG_XX
ICON: IDR_XX
.4 Function named
The function prototype description includes reference to external functions and internal functions, external references function: the module name and the file name, the case of internal functions, annotating definition file name must be indicated on the right;
The first letter must use uppercase letters, a combination of uppercase and lowercase letters gauge function named underscore interval used when necessary, the following 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
The .5 structure named
Structure type name must be all uppercase letters, in principle, beginning with an underscore in front; structure variable name must be a combination of uppercase and lowercase letters, the first letter must use uppercase letters, underscore interval used when necessary. Private data area, you must specify the process to which they belong. Global data defined only need to pay attention to its use.
An example is shown below:
typedef struct
{
the char szProductName [20];
the char szAuthor [20];
the char szReleaseDate [16];
the char szVersion [10];
unsigned long MaxTables;
unsigned Long UsedTables;
} DBS_DATABASE;
DBS_DATABASE GdataBase;
6 control named:
Category lowercase prefix
Lowercase prefix categories:
fm window
cmd button
cob combo drop-down list box
txt text input box
the lab labal tab
img image, image
pic picture
GRD Grid, Grid
scr scroll bar
the lst list of box
frm FRAM
7 Notes
The in principle Comment require the use of Chinese;
The beginning of the file comment include: company name, copyright, author name, time, the module uses background, description of complex algorithms coupled processes;
Function Notes: input, output, function description, flow processing, global variables, call sample, complex functions need to add the variable Application;
Notes in the program include: modification time and author, easy-to-understand notes;
Reference: the beginning of the file comment template
/ ************************************************* *****************
** File name:
** Copyright (c) 1998-1999 ********* Company Technology Development Department
** Created by:
** Date:
** Modified by:
** Date:
** Description:
**
** Version:
** ------------------------------------------------ --------------------------
---
************************************************** **************** /
References: function at the beginning of the comment template
/ ************************************************* ****************
** Function name:
** Input: a, b, c
** ---
** B ---
** C ---
* Output: x ---
** X to 1, which means that ...
** X to 0, which means that ...
** Functional Description:
** Global variables:
** Calling module:
** Author:
** Date:
** Modify:
** Date:
** Version
************************************************** ************** /
Reference: Program notes template
/ * ------------------------------------------------ ---------- * /
/ * Note contents * /
/ * ------------------------------------------------ ---------- * /
8 Program
a program code concise, clear structure, to avoid too many branch structure and too tricky procedure,
Try not to use the recursive mode.
b. preparation process, must also want to test, in other words, unit testing testing program should be programming will be drawn up.
Notes must be consistent with the program.
d version sequestration future changes be sure to old statements / ** / closed, not self-delete, or modify, and modify records in the file and function to be recorded.
e. program at the beginning of each block, "{" and "}" must be aligned nested block each into a set of
Indented one tab, TAB to 4 spaces, block type if, for, while, do keyword leads.
f for the function of each block and special function calls, must indicate its function, for example as follows
:
count.divisor = 1193280 / freq; / / compute the proper count
OutByte ((unsigned short) 67, (unsigned char) 182); / / tell 8253 that a
count is coming
OutByte ((unsigned short) 66, count. C [0]); / / send low-order byte
OutByte ((unsigned short) 66, count. C [1]); / / send high-order byte
× × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × ×
BCB, Delphi variable name:
Follow the Hungarian notation name must be meaningful, and to develop the following requirements
Form: start with a capital W, such as About Copyright form, named WAbout
File: uppercase F, such as About copyright form, file name FAbout.cpp
Button (Button): exit button named btnExit
......
The base class: plus base tag, such as statements of the base class, form name: WBaseRep, file name FBaseRep.cpp
More sharing please pay attention: