I. Code specifications
A good code style comes from the same code specification. Code with good style not only has readability and maintainability, but also provides a pleasant experience.
According to Microsoft statistics, 70-80% of Indian Engineers used the same type of algorithms or modules during development based on the Microsoft platform; in the same survey, only 20% of Chinese engineers are basically the same. This shows that our code production process needs to be standardized.
Real name
All types, variables, constants, methods, and other identifiers use corresponding English meanings. If two independent words are involved, the spacing is separated by an underscore or the first letter of a word (both methods are acceptable). If the length of an identifier exceeds 30 letters, in this case, three letters are selected from the re-stressed syllable of an English word, such as RPT for repeater and Mgt for management.
Case sensitivity rules
Currently, there are generally two case-insensitive rules:
Pascal is case-sensitive. The first letter and other letters of all words are in upper case.
Camel is case-sensitive. Except the first word, the first letter and other letters of all words are in upper case.
NClass Name usagePascalCase Sensitive
Public class helloworld (or hello_world, which is the same as below and will not be repeated)
{
...
}
NUsagePascalCase Sensitive
Public class helloworld ()
{
Void sayhello (string name)
{
...
}
}
NVariable and method parameter usageCamel Case Sensitive
Public class helloworld ()
{
Int totalcount = 0;
Void sayhello (string name)
{
String fullmessage = "hello" + name;
...
}
}
NDo not use the Hungary method to name variables
In the past, most programmers liked to use the data type as the prefix of the variable name and M _ as the prefix of the member variable. Example: String m_sname; int Nage;
However, this method is not recommended in. Net encoding specifications. All variables are in the camel case format, instead of using the Data Type and M _ as the prefix.
Replace Nam, ADDR, and Sal with name, address, and salary.
Do not use single-letter variables such as I, n, and X. Use index, temp, etc. Variable exceptions used for loop iteration:
If the variable is only used for iterative counting and does not appear elsewhere in the loop, you can use a single letter of variable name instead of an actual name.
The file name must match the class name. For example, for the class helloworld, the corresponding file name should be helloworld. CS.
Indentation and interval
N indent: Use tab instead of spaces.
N annotations must be aligned with the code.
N follows the automatic alignment rules of vs2005 and does not need to be adjusted manually.
N uses a blank line to separate the logical grouping of code.
N in a class, the implementation bodies of each method must be separated by blank lines, and the large arc "{}" must be an independent line.
N is null before and after each operator and bracket. For example:
If (showresult = true)
{
For (INT I = 0; I <10; I ++)
{
//
}
}
Instead:
If (showresult = true)
{
For (INT I = 0; I <10; I ++)
{
//
}
}
Good Programming habits
N avoid using large files. If the code in a file exceeds 300 ~ Line 3: Separate the code into different classes.
N to avoid writing too long. A typical method code is in the range of 1 ~ Between 30 rows. If a method sends more than 30 lines of code, you should consider breaking it into different methods.
N method names need to be able to see what it does. Do not use a name that may cause misunderstanding. If the name is clear, you do not need to use documents to explain the functions of the method.
N a method is used to complete only one task. Do not combine multiple tasks into one method, even if those tasks are very small.
N uses the special type of C # instead of the alias type defined in the system namespace. For example:
Int age;
String name;
Object contactinfo;
Instead:
Int16 age;
String name;
Object contactinfo;
This is based on the following two reasons: (1) standardization and consistency; (2) Ease of cross-Language Platform transplantation.
N should not use a fixed value in the program, instead of a constant. Do not use string constants. Use source files whenever possible.
N avoid using many member variables, declare local variables, and pass them to methods.
N. Do not share member variables between methods. If a member variable is shared among several methods, it is difficult to know which method modifies its value. Use Enum if necessary. Do not use numbers or strings to indicate discrete values.
N. Do not declare the member variables as public or protected. Are declared as private and use the properties of public/protected.
N does not use the specific path and drive name in the code, use the relative path, and make the path programmable. Never imagine that your code is running on the "C:" disk. You don't know, some users run programs on the network or "Z:" disk.
N perform "self-check" when the application starts and ensure that the required files and attachments are in the specified location. If necessary, check the database connection and give the user a friendly prompt if any problem occurs.
N if the required configuration file cannot be found, the application must be able to create and use the default value by itself. If an error value is found in the configuration file, the application will throw an error and a message will be prompted to tell the user the correct value. The error message must help you solve the problem.
Note
N. Separate each line of code. Every declared variable is annotated. Comment in the desired place.
N code with high readability requires few comments. If all the variables and methods have meaningful names, the code will be highly readable without too many comments. Comments with few lines will make the Code look elegant.
N if complex and difficult principles are used for some reason, the program must be well documented and annotated in detail.
N. Check the spelling of comments to ensure correct use of syntaxes and punctuation marks.
Ii. Database Design Specifications table classification and naming
N data table category
U system tables support data tables of business models, such as process models and system management tables.
U business table product provides tables related to common business function modules, such as general business query.
U User table data tables related to specific businesses used by users for secondary development.
N data table name
All U tables are named at the beginning of the letter "T" (table), and are separated by an underscore (_) following an actual word.
U System Table System table prefix: tsys _
U business table prefix: tbiz _
U user tables are defined by users. However, we recommend that you do not use the naming rules of system tables and business tables.
N field name
The naming rules of fields refer to the naming rules of code identifiers, but note that the reserved words of the database are avoided. For example, do not use the following field names: Index, field, password, ID, Oracle, SQL, and so on.
For system tables that involve technical core, we recommend that you use a table similar to "F1, F2, F3..." to prevent analysis ...... FN. But do not use "F0" because this name is not allowed in some databases, such as InterBase.
Index creation
N indexes are a double-edged sword. Indexes increase the query efficiency, but reduce the efficiency of insert, delete, and update.
N in general, the frequency and time limit of data editing are far less than the requirements for database queries. Therefore, you must create an index for data tables with many records and frequently queried records.
N most databases automatically create indexes for primary key fields. Note that indexes are created for Foreign keys.
N. Do not index large fields. This will make the index occupy too much storage space.
N try not to index small tables that are frequently edited.
The N identify field should not be associated with other tables as the table's primary key, which will affect the data migration of the table. If you want to support multiple databases, we recommend that you use the unique value generated by the Program for the primary key.
N if a large table requires frequent insert/delete/update operations and high-concurrency queries, we recommend that you split the Table Based on the Data Access frequency, then create an index.
Procedures and functions
Database vendors provide rich and personalized processes and functions to highlight their own advantages.
To improve product scalability and Data independence, do not use the processes and functions related to a specific database or store procedure. We recommend that you use the intermediate layer Business Objects of the application server.
Field/domain definition
N avoid blob as much as possible. If so, do not index blob and do not define multiple blobs.
N do not use the date field. Use the string char (19) instead, for example, 12:22:08.
N for strings of determined length, please fix the length of field type, such as char (80), do not use varchar.
N for value type fields, use the corresponding Database Value Type instead of a string.
Iii. com and. Net interoperability specifications
. NET technology has become the mainstream of the Microsoft platform, but in the Win32 era, many com and DCOM components have been developed. As a result, the development of COM components has invested a lot of manpower and financial resources. it is more meaningful to reuse these COM components in the. NET environment.
. Net supports two-way interoperability between COM, COM +, local winapi calls and unmanaged code during runtime. To achieve interoperability, you must first introduce.. NET Framework system. runtime. interopservices namespace.
C # Syntax:
Using system. runtime. interopservices;
(1). NetAccessAPI
. Net allows C # To access functions of unmanaged DLL. To call the MessageBox function of Windows user32.dll:
Int MessageBox (hwnd, lpctstr lptext, lpctstr lpcaption, uint utype)
You can declare a static extern method with the dllimport attribute:
Using system. runtime. interopservices;
[Dllimport ("user32.dll")]
Static ertern int MessageBox (INT hwnd, string text, string caption, int type );
Then you can directly call it in the code. Note that the stringbuilder object is used in the API that calls the returned string.
(2). NetAccessComComponents
Slave.Netcalls comkit for ease of comparison. You only need to use tlbimp.exe to generate warpclass in the form of COM assembly, and then call it in the. NET project.
Note that the COM type information is described in the Type Library file. the. NET accessories are self-described. Tlbimp is used to generate custom accessories from COM components and their types.
1. Compile COM components
Compile and generate a comaccount. dll.
2. You can use the. Net Package class (assembly.pdf to generate. Net accessories using tlbimp.exe.
Tlbimp/out: netaccount. dll comaccount. dll
3. Access in. Net code
By referencing netaccount. dll, the. NET code can access COM components just like accessing. Net accessories.
Iv. Exception Handling principles
N handles all general exceptions in the application-level (thread-level) Error processor. In the event of an "unexpected general error", the error processor should capture the exception and send a message to the user to record the error message before the application is closed or the user selects "ignore and continue.
N you do not need to use try-catch for each method. It is used only when a specific exception may occur. For example, when a file is written, the exception fileioexception is handled.
N. Do not write too many try-catch modules. If necessary, write a separate try-catch module for each executed task. This will help you find out which code generates an exception and send a specific error message to the user.
N if the application needs it, you can write your own exception classes. Custom exceptions should not be derived from the base class systemexception, but must inherit from iapplicationexception.
N in the development phase, you do not have to capture general exceptions in all methods. Deliberately indulgent exceptions will help you discover most errors during the development cycle.
Exception Handling prompt
N. do not capture exceptions, but do nothing. It seems that the system is running normally. If an exception is hidden, you will never know whether or why the exception occurred.
N. a friendly message is provided to the user when an exception occurs. However, it is necessary to precisely record all possible details of the error, including the occurrence time, related methods, and class names.
N never use error messages such as "application error", "an error found", and so on. Instead, give a message similar to "database update failed. please ensure that the login ID and password are correct ." And so on.
N when an error message is displayed, you should also be prompted how to solve the problem. For example, "An error occurred while updating the database. Make sure that the login ID and password are correct .", Instead of simply saying "database update failed ".
N the message displayed to the user must be short and friendly. However, all possible information should be recorded to help diagnose the problem.
Code instance for Exception Handling
The following exception handling modes are recommended:
Void readfromfile (string filename)
{
Try
{
// Read the file.
}
Catch (fileioexception ex)
{
// Records exception logs
// Re-throw targeted exception information
Throw;
}
}
The following exception handling mode is not recommended:
Void readfromfile (string filename)
{
Try
{
// Read the file
}
Catch (exception ex)
{
// Capturing general exceptions will never let us know whether it is a file error or another error.
// Hide an exception and we will never know that an error has occurred.
Return "";
}
}
V. Application and release of object instances
The. NET platform's garbage collection mechanism can automatically dispose object instances that are no longer referenced, so many developers do not take the initiative to release the requested object Resources. In fact, the object will not be released before its lifecycle ends.
However, when an object is within its lifecycle, we do not use it any more, so that resources can be released to improve system efficiency. Therefore, it is necessary to release the requested resources.
Never give over what you can to the operating system. It is a good habit to release unused resources in a timely manner.
Vi. Database Access
Database Access is always the bottleneck of the system. Selecting an efficient and robust database access mode is the basic guarantee of product performance.
N never assume that your application system is built on a database. Therefore, you must have a unified and transparent database access mechanism.
N using ADO. Net to access the database based on the efficiency and stability considerations, using the Microsoft platform native database access mode ADO. net. Use ADO. net can access the database through oledb and ODBC modes. We recommend that you use the oledb mode provided by the database vendor. This mode bypasses ODBC, greatly improving the database cursor performance and improving the efficiency.
N using a third-party data persistence layer tool similar to nhib.pdf can improve development efficiency, but it reduces system performance and elasticity. Performance is much more important than development efficiency for products. Moreover, development based on vs2005 is not a problem. Remember: third-party tools cannot be the core technology of your products. Data Access mechanisms are the bottleneck of system efficiency.
N data objects using proprietary property rights directly adopt ADO.. Net encapsulates the underlying data access methods: insert, delete, update, and transaction management. The client and server adopt the same data access mechanism and set up a connection buffer pool to improve data access efficiency.
VII. Distributed Transaction Management
For multi-layer distributed applications, database transactions are characterized by "remote and distributed", making it difficult to manage transactions.
For ADO. net, transactions are bound to database connections. Therefore, you must manage individual transactions or nested transactions for each database connection in the Data Access Object. If you want to access the database, the data access object on the server will be automatically allocated with a specific connection and perform data operations based on the connection ID. No matter how many remote client processes the transaction is distributed, the server data object can be easily managed by locking the connection ID.
8. Smart Clients
A smart client is a client application that is easy to deploy and manage. It integrates the advantages of thin clients and fat clients, and uses local resources and smart connections to distributed data resources as a whole, quick Response and rich interactive experience are provided.
Smart Clients include Windows form, office client, and mobile client. They have the following features:
N use local resources
N Use Network Resources
N users supporting occasional connections
N provides smart installation and update
N provides client device flexibility
The. NET Framework base class library is embedded with rich sets that support Smart Clients. By using the Common Language Runtime Library (CLR), you can develop smart clients using any language supported by. net.
The Smart Client is a powerful alternative to the thin customer segment and also a client model recommended by Microsoft. Try to use a smart client instead of a browser. If you can, build your client system on the office platform, such as outlook.