C # Programming Specification
In order to ensure that everyone uses C # programming and C style, I have made C # program rules on C standards (trial).
Basic Requirements 1 Program Structure Requirements 1.1 The program is clear, easy to understand, the number of lines of single functions must not exceed 100 rows. 1.2 What is going to do, it is simple, straightforward, and the code is streamlined and avoiding the garbage. 1.3 Try to use the .NET library function and public function (without special circumstances, do not use external methods to call the core dynamic link library API). 1.4 Do not define global variables at will, try to use local variables. 2. Readability Requirements 2.1 Readability First, Efficiency Second (Code is a read J). 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 header (using XML note), 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.6 Each phase of the process has a related commentary instructions. 2.7 There is a comment before the typical algorithm, and the algorithm is as simple as possible in the case of meeting the requirements. 2.8 Using the logical structure of the display program, the indentation volume is consistent and the Tab key is used, and the Tab is 4 bytes. 2.9 Circulation, the branch level should not exceed five layers. 2.10 Comments can be in the same row with the statement, or on the upward. 2.11 Blank characters are also a special annotation. 2.12 A glimpse of the statement does not comment. 2.13 Note The scope of action can be: definition, reference, conditional branch, and a piece of code. 2.14 Note (excluding 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 No two equivalent branches are prohibited. 3.2 In addition to the scope of the Switch key, the goto statement is disabled. 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. The 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 Try not to 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 take the initiative to process accidents, such as whether the file can logic lock, the printer is online, etc. For a clear error, there must be a clear fault tolerance code to prompt the user, and use Try Throw Catch in such uncertain occasions. 4.8 Unit test is also part of programming, and the procedure for submitting a joint test must pass through unit testing.
4.9 Try to use the standardized fault tolerance statement.
E.g:
Try
{
}
Catch
{
}
Finally
{
}
5. Reuse requirements 5.1 Re-use of completion of the relative independent function or code should be abstract as an ASP.NET service or class. 5.2ASP.NET services or categories should consider OO ideas, reduce external contacts, consider independence or encapsulation.
Attachment: C # Programming Specification
1 Applicable range This standard applies to the use of Visaul C #, the rest of the language.
2 Variable Name Naming must have a certain actual meaning, form is Xabcfgh, X is determined by the 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;
SZ Char Sztext
SB Sbyte Sbtext
Bt byte bttext
N int NText
UI uint UIText
l Long LText
UL Ulong Ultext
f float ftext
D Double DText
B Bool Btext
de decimal detext
String strText
X, Y coordinate
ATT table properties
M_ class member variable m_nval, m_bflag
S_ class static member variables S_nval, s_bflag
// Do not propose global variables, which are classified, and the letters indicating the characteristics, such as Classtext, Class reflect the characteristics of the classes.
3 constant naming and macro definition constant and macro definition must have a certain actual meaning; constant and macro definition between #include and function definitions; constants and macro definitions must be written in uppercase letters, intermediate can be used under the continuity of meaning, each There must be a simple note to the right side, explaining its role;
4 resource name definition format (I haven't understood more. Net resources), only write a few: menu: IDM_XX or cm_xx bitmap: IDB_XX dialog: IDD_XX String: IDS_XXDLGINIT: DIALOG_XXX: IDR_XX
5 Function Names and Namespaces, Naming of Classes, Named Functions of Interfaces 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 you know its definition File name; the first letter must use uppercase letters, requiring the case where the uppercase letter combination specification function is naming, if necessary, the underscore interval can be used, as follows:
Void PrintTrackData ();
Void Showchar (int, int, char);
Named of the event function:
Void EventH (Object SD, Event E) // Event represents the function of the event response.
Named of the interface
The name of the interface is generally used as the first letter in 'i', for example, in order to class, for example:
Interface IA
Namespaces (such as Namespace a) and naming naming rules are identical to the principles and function names.
6 Structural Name Structural Type Names must all be used in uppercase letters, in principle, the above underline; structural variable naming must be combined with uppercase cases, the first letter must use uppercase letters, if necessary, use underscore 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 szReleaseDate [16]; char szVersion [10]; unsigned long MaxTables; unsigned long UsedTables;} DBS_DATABASE; DBS_DATABASE GdataBase;
7 controls naming:
C # control rules in order to unify, divide the Windows Form program and web program (maybe everyone is not very habitable).
WINDOW FORM program
Expressed with lowercase prefix
FM window CMD button COB Combo, drop-down list box TXT text input box Labal, label img image, image Pic Picturegrd Grid, Grid SCR scroll bar LST list box FRM FRAM
Web program
Expressed with a big write prefix
FM window CMD button COB Combo, drop-down list box TXT text input box Labal, label img image, image Pic Picturegrd Grid, Grid SCR scroll bar LST list box FRM FRAM
8 Note Principle Notes Remarks Requirements Use Chinese; file start annotation includes: company name, copyright, author name, time, module purpose, background introduction, etc., complex algorithms need to be added to process instructions; function comments include: input, output, functions Description, process processing, global variable, call sample, etc., complex functions requires a variable application description; the comments in the program include: modification time and authors, easy-to-understand annotations; reference 1: Note Template starting with the file / ** *********************************************************** *************** File Name: ** Copyright (c) *********** Creation: ** Date: ** Modify: ** Date: ** Description: **** Version: ** ------------------------------- -------------------------------------------- Reference 2: Function Note template
///
///
/// summary>
/// param>
///
///
///
9 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 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 indicate its function.