1. C# code style requirements
1.1 Notes
Types, properties, events, methods, method parameters, and annotations as needed .
If the names of types, properties, events, methods, and method parameters are already self-explanatory, no comments are required; otherwise, comments are required.
When adding comments, the way of adding is as shown in the following figure:
1.2 Naming of types (classes, structures, delegates, interfaces), fields, properties, methods, and events
Priority is given to English. If there is no suitable word description in English, Pinyin can be used, and the use of Chinese does not meet the requirements.
The only place where Chinese can be used is the enumeration item of the enumeration, which actually does not belong to the scope of the title of this section. It is just put together to illustrate, as shown in the following figure:
1.3 Not using abbreviations
All types, methods, parameters, and variables should be named without abbreviations, including well-known abbreviations such as msg.
1.4 Code using semi-expansion
The first step, open Visual Studio, go to "Tools", "Options...", as shown below:
The second step, enter "Text Editor", "C#", "Formatting", "New Line", and remove the checkmarks in all the check boxes on the right, as shown in the following figure:
The third step, click "OK" to complete the setting.
1.5 Use Tab as indent and set the indent size to 4
The first step, open Visual Studio, go to "Tools", "Options...", as shown below:
The second step, enter "Text Editor", "C#", "Tab", as shown in the figure below, set the tab.
The third step, click "OK" to complete the setting.
1.6 A .cs source file defines at most two types
If the relationship between the two types is closely related, such as product, product type, then the Product class, and the ProductType enumeration can be defined in the same Product.cs file.
But two unrelated type definitions cannot appear in a .cs file, for example, the Product class and the Reseller class (distributor) are defined in a BasicInfo.cs file.
1.7 Type name and source file name must be the same
When the type is named Product, its source file can only be named Product.cs.
1.8 All namespace and type names use Pascal style (the first letter of the word is capitalized)
As shown in the figure below, the types marked in red are using Pascal style:
Note that ProductType is a private type, regardless of whether the type is public or private, its naming is always in Pascal style.
1.9 Use Camel style for the names of local variables and method parameters (the first letter is lowercase, and the first letter of each subsequent word is uppercase)
Variables or method parameters that use Camel style are marked in red:
1.10 Private methods, protected methods, still use Pascal-style naming
The sample code is as follows:
1.11 If the content of the if statement is only one line, the curly brackets can be omitted, but it must be on the same line as the if statement
The if test in Example 1.9 is actually equivalent to the following statement:
1.12 To call other members of the type, add this; to call members of the parent class, add base
The sample code is as follows:
1.13 Private and protected fields inside types, named in Camel style, but prefixed with "_"
The code example is as follows:
1.14 Public fields cannot appear
If you need public fields, wrap them with attributes.
1.15 Sort Order of Type Members
The order of the type members from top to bottom is:
Fields: private fields, protected fields
Properties: private properties, protected properties, public properties
Events: private events, protected events, public events
Constructor: Constructor with the largest number of arguments, Constructor with a medium number of arguments, Constructor with the least number of arguments
Methods: Overloaded methods are arranged in the same order as constructors, from the most parameters to the least.
1.16 Naming of delegates and events
Delegates are named with an EventHandler suffix, such as SalesOutEventHandler.
The event is formed by removing the EventHandler suffix and adding the On prefix to its corresponding delegate type.
For example, for an event of the SalesOutEventHandler delegate type, the event name is: OnSalesOut.
The sample code is as follows:
1.17 Names of methods and properties that return bool types
If the method returns a bool type, it is prefixed with Is, Can, or Try, for example:
1.18 Common collection type suffix naming
For collection types listed in the table below, the corresponding suffix should be added.
illustrate | suffix | Example |
---|---|---|
array | Array | int[] productArray |
list | List | List<Product> productList |
DataTable/HashTable | Table | HashTable productTable |
dictionary | Dictionary | Dictionay<string,string> productDictionary |
DbSet/DataSet in EF | Set | DbSet<Product> productSet |
1.19 Common suffix naming
All local variables, method parameters, fields, and properties listed in the following table must be suffixed accordingly.
illustrate | suffix | Example | Example description |
---|---|---|---|
cost related | Cost | ShipCost | Shipping fee |
price related | Price | ProductUnitPrice | Product unit price |
news related | Message (Deprecated Note) | SuccessMessage | success message |
date related | Date (Deprecated Time) | OrderDate | order date |
count, quantity | Count (Deprecated Time) | LoginCount | Login times |
Link address related | Url | BlogUrl | Blog link |
Picture related | Image | SignImage | Signature picture |
Amount related | Amount | PrepaidAmount | Prepayments |
Points, points related | Point | MemberPoint | Member Points |
record, log | Record (Deprecated Log) | ErrorRecord | error log |
configuration related | Config | DataBaseConfig | Database configuration |
Status related | Status | OrderStatus | Order Status |
mode, method | Mode | OpenMode | open way |
Kind related | Category / Type choose one | UserCategory | User type |
factory related | Factory | ConnectionFactory | connection factory |
enable related | Enabled | ExportEnabled | Turn on export |
stream correlation | 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
凡存在下表中的类型,需采用下表指定的名称命名。
类型 | 命名 | 类型 | 命名 |
---|---|---|---|
客户 | Customer | 分销商 | Reseller |
零售商 | Retailer | 经销商/批发商 | Dealer |
用户 | UserInfo(User为数据库关键字) | 订单 | OrderInfo(Order为数据库关键字) |
供应商 | Supplier | 管理员 | Admin |
密码 | Password | 会员 | Member |
评论 | Remark(弃用Comment) | 文章 | Article |
新闻 | News | 发票 | Invoice |
导入 | Import | 导出 | Export |
公司、企业 | Company(弃用Enterprise) | 产品 | Product |
省份 | Province | 城市 | City |
区县 | District | 地址 | Address |
角色 | Role(弃用Group) | 权限 | Authority(弃用Permission) |
仓库 | Warehouse | 工厂 | Plant |
登录 | Login(弃用SignIn) | 登出 | LogOut(弃用SignOut) |
创建 | Create(弃用Add) | 编辑 | Edit |
更新 | Update | 删除 | Remove(弃用Delete) |
照片 | Photo | 图片 | Image |
1.21 常见字段、属性命名
字段、属性种类比较繁杂,因此仅列出最常用的几项。
类型 | 命名 | 类型 | 命名 |
---|---|---|---|
Id(int型) | Id(“d”小写,弃用ID) | GuidId(Guid型) | Id |
Name | 名称 | Title | 标题 |
Remark | 备注、描述(弃用Memo、Description) | Category | 种类(弃用Class、Type) |
Linkman | 联系人 |
2. XHTML 代码风格要求
2.1 如果XHTML标记有层次,那么代码也要有层次
下面是书写符合要求的例子:
下面是书写 不符合要求 的例子:
2.2 所有标记必须闭合
示例代码如下:
2.3 如果标记中间代码超过20行,则应在标记末尾加注标识
标注方式如下:
3. CSS 代码风格要求
3.1 代码使用半展开
设置方法参考1.4节。
3.2 使用Tab作为缩进,并设置缩进大小为4
设置方法参考1.5节。
3.3 代码注释
注释主要说明该样式应用于页面的哪个部分,而非说明样式的应用效果,代码注释风格如下所示:
3.4 代码编写
每一个样式设置必须独占一行,不能位于同一行,下面是符合要求的写法:
下面是 不符合要求 的写法:
3.5 嵌入式样式的比例不超过样式表代码总量的10%
嵌入式样式为直接写在HTML标记内部的样式,如下图所示:
3.6 内联式样式的比例不超过样式表代码总量的30%
内联式样式为写在<head></head>中的样式,如下图所示:
内联式样式,不能 写在<body></body>之间。
3.7 外联式样式表的比例不少于样式表代码总量的60%
外联式样式表为写在.css文件中的样式,通过link引入到XHTML页面中,如下图所示:
4. JavaScript 代码风格要求
4.1 代码使用半展开
设置方法参考1.4节。
4.2 使用Tab作为缩进,并设置缩进大小为4
设置方法参考1.5节。
4.3 代码注释
代码注释需要说明“函数功能”、“入口参数”、“返回值”,注释范例如下:
其中第一行说明函数功能;第二行说明入口参数;最后一行说明返回值
4.4 不得出现内嵌式代码
内嵌式代码是指写在XHTML标记中的JavaScript代码,下面的写法是 不符合要求 的:
4.5 内联式代码占JavaScript的总量不得超过40%
内联式代码是指写在<head />或者<body />之间的代码:
4.6 外联式代码占JavaScript的总量至少为60%
外联式代码指写在单独的.js文件中,然后通过script标记连接到XHTML页面中的代码。