Programming specification

zhaozj2021-02-08  263

Programming specification

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 key is used, and the Tab is 6

byte.

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 right, 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 floats,

Such as: 10.0 * 0.1 == 1.0, unreliable

4.7 When the program is related to the environment or state, it must take the initiative to process an unexpected event, such as whether the file can

Logic lock, whether the printer is online, etc.

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 is determined by the variable type, ABC, FGH means continuous

Rightening strings, if the consecutive string is only two, it 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 * No symbol integer

P * pointer

SZ, STR * string

w word

X, Y coordinate

CHAR or TCHAR Types with Windows APIs have direct contact with Szappname [10] in the form of SZAPPNAME [10]

Filename [10] form, single characters can also be represented 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 type HDC;

Hinstance Type Hinstance

Handle Type Hinstance,

Hicon type hic;

Int ITMP

FLOAT FTMP

DWORD DW *

String, ANSISTRING STR *

M_ class member variable m_nval, m_bflag

G_ 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 certain practical significance;

Constants and macros define between #include and function definitions;

Constants and macro definitions must be written in uppercase letters, and the intermediate can be connected with the continuity of meaning, each

The right side of the definition must have a simple note to explain its role;

Resource name definition format:

Menu: IDM_XX or CM_XX

Bittap: IDB_XX

Dialog: IDD_XX

String: IDS_XX

DLGINIT: DIALOG_XX

Icon: IDR_XX

.4 function name

Function prototype instructions include reference external functions and internal functions, and external references must be indicated on the right side: Modes

Block Name and File Name, such as internal functions, as long as you know its definition file name;

The first letter must use uppercase letters, requiring the case with uppercase letters combination specification function, if necessary, can be used underline

Interval, examples are as follows:

Void updatedb_tfgd (track_name); // module name: r01 / sdw.c

Void PrintTrackData (TRACK_NAME); // Module Name: R04 / Tern.cvoid ImportantPoint (Void); // Module Name: R01 / SDW.C

Void showchar (int, int, chtype); // local module

Void scrollup_v (int, int); // local module

.5 structural naming

The structural type naming must all be used in uppercase letters, in principle, the above scribe is started; the structural variable naming must be used

Case, the first letter must use uppercase letters, if necessary, use the underscore interval. For private numbers

According to the district, it must be indicated by the process. Global data definitions simply pay attention to their use.

Examples are as follows:

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 naming:

Expressed with lowercase prefix

Expressed in a lowercase prefix:

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 note

In principle, annotations require Chinese;

File start comments include: company name, copyright, author name, time, module purpose, background introduction, etc.

Miscellaneous algorithms need to be added to the process description;

Function Note includes: input, output, function description, process processing, global variable, call sample, etc., complex functions

Need to add variable uses;

Comments in the program include: modification time and authors, easy-to-understand annotations, etc .;

Quote 1: Note Template at the beginning of the file

/ ************************************************** ******************

** file name:

Company Technology Development Department ********

** founder:

** Japan:

** Modify:

** Japan:

** Description:

**

** version:

** ------------------------------------------------- ----------------------------

---

*********************************************************** **************** /

Quote 2: Note Template at the beginning of the function

/ ************************************************** *****************

** Function name:

** Infit: a, b, c

** a -----

** b ----

** C -----

** output: x ---

** x is 1, indicating ...

** X is 0, indicating ...

** Function Description:

** Global variable:

** Call Module:

** Author:

** Japan:

** Remove:

** Japan:

** version

*********************************************************** ************* /

Quote 3: Comment template in the program

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------- * /

/ * Comment content * /

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------- * /

8 program

a. Program coding strikes simple, clear structure, avoid too much branch structure and too much 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 "unit test" test plan should

It is also intertwined when writing.

c. Note must be consistent with the program.

d. Modifications after version storage must use the old statement to / * * / closed, can not delete or modify itself, and

Record in the modification record of the file and function.

e. The beginning "{" and "}" of each block in the program must be aligned, nested Block per entered,

Net into a Tab, Tab is 4 spaces, and the block type includes keywords such as if, for, while, do.

f. For a relatively large function, each block and special function call must indicate its function, for example,

:

Count.divisor = 1193280 / freq; // compute the properties

Outbyte (UNSIGNED SHORT 67, (unsigned char) 182); // Tell 8253 That A

Count is coming

Outbyte 66, Count. C [0]); // send low-order byte

Outbyte (UNSIGNED SHORT 66, Count. C [1]); // send high-order byte

×××××××××××××××××××××××××××××××××××××××

BCB, variables in Delphi Name:

Follow the Hungarian nomenclature, life

Name must make sense, formulate the following

Form: Start with uppercase, such as the About copyright form, named WABOUT

File: Start with uppercase, such as the About copyright form, file named fabout.cpp

Button: If you exit buttons, name BTNEXIT

......

Base class: add BASE tag, such as report base class, form named: wbaserep, file named FBASEREP.CPP

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

New Post(0)