Project Code Style requirements

Source: Internet
Author: User
Tags deprecated

1. C # code Style requirements 1.1 comments

Type, properties, events, methods, method parameters, and add comments as needed.

If the name of a type, property, event, method, method parameter is already self-explanatory, no comment is required; otherwise you must add a comment.

When you add a comment, the method is added as shown:

1.2 Types (classes, structs, delegates, interfaces), fields, properties, methods, naming of events

English is preferred, if the English does not have the appropriate word description, you can use pinyin, the use of Chinese is not in line with the requirements.

The only place where you can use Chinese is an enumeration of enumerated items, which are not actually part of the title of this section. Here just put together a description, as shown in:

1.3 Do not use abbreviations

Names of all types, methods, parameters, and variables must not be abbreviated, including the familiar abbreviations, such as MSG.

1.4 Code use semi-expansion

The first step is to open visual Studio and go to Tools, Options ..., as shown in:

In the second step, go to "text editor", "C #", "Formatting", "New Line", and uncheck the check boxes on the right, as shown in:

Third step, click "OK" to complete the setup.

1.5 use Tab as indentation and set indent size to 4

The first step is to open visual Studio and go to Tools, Options ..., as shown in:

In the second step, go to "text editor", "C #", "tab", as shown, set tab.

Third step, click "OK" to complete the setup.

1.6 A. cs source file with a maximum of two types defined

If two types of relationships are closely related, such as products, product types, the product class, and the ProductType enumeration can be defined in the same Product.cs file.

However, you cannot have two unrelated type definitions in a. cs file, such as defining the product class and the Reseller Class (reseller) in a BasicInfo.cs file.

1.7 type name and source file name must be the same

When a type is named product, its source file name can only be Product.cs.

1.8 All namespaces, type names using Pascal style (capitalize the first letter of the word)

As shown, the red flag is used in the Pascal style type:

Note that ProductType is a private type, regardless of whether the type is public or private, and its name is always in Pascal style.

1.9 Local variables, method parameter names using camel style (first letter lowercase, followed by the first letter of each word capitalized)

Red is marked with a camel-style variable or method parameter:

1.10 Private methods, protected methods, still named with Pascal style

The sample code is as follows:

1.11 If the IF statement contains only one row, you can do so without the braces, but you must be on the same line as the IF statement

The If judgment in example 1.9 is actually equivalent to the following statement:

1.12 Call the other members of the type, add this; Call the parent class member, add base

The sample code is as follows:

1.13 Type internal private and protected fields, named with Camel style, but prefixed with "_"

The code examples are as follows:

1.14 Public fields cannot appear

If a public field is required, the property is used for wrapping.

1.15 sort order of type members

The sort order of type members is from top to bottom:

Fields: Private fields, protected fields

Properties: Private property, protected property, public property

Events: Private events, protected events, public events

Constructors: Constructors with the highest number of parameters, constructors with a medium number of parameters, constructors with the fewest number of parameters

Method: The overloaded method is arranged in the same order as the constructor, from the maximum number of parameters down to the lowest parameter.

1.16 Naming of Delegates and events

The delegate is named EventHandler as a suffix, such as salesouteventhandler.

The event takes its corresponding delegate type, removes the EventHandler suffix, and adds an on prefix.

For example, for an event of the Salesouteventhandler delegate type, its event name is: Onsalesout.

The sample code is as follows:

1.17 Returns the method of type bool, the name of the property

If the type of the method returned is type bool, it is prefixed with is, can, or Try, for example:

1.18 Common collection type suffix naming

The corresponding suffix should be added to match the collection types listed in the following table.

Description Suffix Example
Array Array Int[] Productarray
List List List<product> ProductList
Datatable/hashtable Table HashTable producttable
Dictionary Dictionary Dictionay<string,string> productdictionary
The Dbset/dataset in EF Set Dbset<product> Productset
1.19 Common suffix naming

The corresponding suffix must be added to the local variables, method parameters, fields, and properties listed in the table below.

Description Suffix Example Example description
Fee-related Cost Shipcost Shipping
Price related Price Productunitprice Product price
Message-related Message (Discard note) Successmessage Success Message
Date related Date (deprecated time) OrderDate Date of order
Count, quantity related Count (deprecated time) Logincount Number of Logins
Link Address related Url Blogurl Blog links
Photo related Image Signimage Signature picture
Amount related Amount Prepaidamount Advance
Points, integral related Point Memberpoint Member points
Records, log related Record (Discard log) Errorrecord Error logging
Configuration related Config Databaseconfig Database configuration
Status related Status Orderstatus Order Status
mode, method related Mode OpenMode Open mode
Kinds of related Category/type two Choose one UserCategory Type of user
Factory class related Factory ConnectionFactory Connection Factory
Enable related Enabled Exportenabled Turn on Export
Flow-related Stream Uploadstream Upload Stream
Reader-related Reader Excelreader Excel Reader
Writer-related Writer Excelwriter Excel writer
Adapter-related Adapter Introopadapter Introop Adapter
Provider-related Provider Memebershipprovider Member Information provider
Wrapper-related Wrapper Productwrapper Product Wrapper
Connection related Connection Excelconnection Excel connection
1.20 Common Type naming

Where the types in the following table exist, they should be named with the name specified in the following table.

Type Named Type Named
Customer Customer Distributors Reseller
Retailers Retailer Distributor/wholesaler Dealer
User UserInfo (user is database keyword) Order OrderInfo (order as database keyword)
Suppliers Supplier Administrator Admin
Password Password Member Member
Comments Remark (deprecated comment) Article Article
News News Invoice Invoice
Import Import Export Export
Company, Enterprise Company (deprecated Enterprise) Products Product
Provinces Province City City
District District Address Address
Role Role (deprecated Group) Permissions Authority (deprecated permission)
Warehouse Warehouse Factory Plant
Login Login (deprecated signin) Log out LogOut (deprecated SignOut)
Create Create (Discard Add) Edit Edit
Update Update Delete Remove (Discard Delete)
Photo Photo Image Image
1.21 common fields, property naming

Fields and attributes are more complex, so only the most commonly used items are listed.

Type Name Type Name
Id (int type) ID ("d" lowercase, deprecated ID) Guidid (GUID type) Id
Name Name Title Title
Remark Remarks, Description (deprecation memo, Description) Category Type (deprecated class, type)
Linkman Contact
2. XHTML Code Style Requirements 2.1 If the XHTML tag is hierarchical, then the code has to be layered

Here are some examples of writing to meet your requirements:

The following are examples of non-conforming writing requirements :

2.2 All marks must be closed

The sample code is as follows:

2.3 If the Tag intermediate code is more than 20 lines, you should raise the identification at the end of the tag

The labeling method is as follows:

3. CSS code style requires 3.1 code use semi-expanded

Set method reference section 1.4.

3.2 Use Tab as indentation and set indent size to 4

Set method Reference section 1.5.

3.3 Code comments

The comments mainly describe which part of the page the style is applied to, not how the style is applied, and the code comment style is as follows:

3.4 Code Writing

Each style setting must be exclusive on one line, not on the same line, and the following are the required wording:

The following are the non-conforming wording:

3.5 The scale of the inline style does not exceed 10% of the total style sheet code

The inline style is a style that is written directly inside the HTML tag, as shown in:

3.6 The scale of inline styles does not exceed 30% of the total style sheet code

Inline styles are written in the

Inline style, cannot be written between <body></body>.

3.7 The scale of an external style sheet is not less than 60% of the total stylesheet code

The outer style sheet is the style written in the. css file, which is introduced to the XHTML page via link, as shown in:

4. JavaScript code style requires 4.1 code using semi-expanded

Set method reference section 1.4.

4.2 Use Tab as indentation and set indent size to 4

Set method Reference section 1.5.

4.3 Code comments

The code comment needs to describe "function function", "entry parameter", "return value", and the following example:

The first line describes the function function, the second line describes the entry parameter, and the last line describes the return value

4.4 No inline code

Inline code refers to JavaScript code written in XHTML markup, and the following is not a requirement :

4.5 Inline code must not exceed 40% of the total JavaScript

Inline code refers to the code written between

4.6 The total amount of code that is external to JavaScript is at least 60%

The external code is written in a separate. js file and then connected to the code in the XHTML page through the script tag.

Project Code Style requirements

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.