现在的位置: 首页 > 综合 > 正文

StyleCop(C#代码规范分析工具)—2.常用规则介绍(一)

2012年03月07日 ⁄ 综合 ⁄ 共 2734字 ⁄ 字号 评论关闭

寄菜鸟

  对于像我这样还是菜鸟级的程序员来说,Leader分配给我的任务,只要按时做完就OK,哪有时间去理代码的优雅,可读性!就算有,就咱这个水平,。。。!别人看不懂管他呢!只要我看得懂不就行了!由于平时没有注重,老大让我去维护自己以前做过的项目,结果完全傻眼了,咋一点印象都没有了呢!这不可能是我做的吧!代码混乱不堪,定义的语句只能去猜它的本意,明明只要修改一个小地方,却要从头到尾去了解整个程序,耽误时间!回想起当初写代码时为了追求能尽早的完成任务,忽略了代码的规范性,现在真是追悔莫及!所以决定从现在开始养成一个良好的编码风格,虽然编码的速度会有所降低,但是从长远出发,是很值得的!但是自己水平有限,有没有类似功能的工具呢!功夫不负有心人,它就是StyleCop.

 

体会

  刚开始时,说实话感觉这个家伙有点变态,普通的一个程序,就几十个警告!而且有些不太理解!如果觉得自己这样做有充分的理由,也不必遵循StyleCop的规则。下面开始介绍StyleCop的一些我常常违反的一些规则。

 

常用规则

基础

Invalid spacing around the comma :’,’后面需要加空格(几个无所谓)

Invalid spacing around the opening curly bracket:’{‘前后需要加空格(仅仅一个,要不然就报’The code contains multiple spaces in a row. Only one space is needed’)

Invalid spacing around the closing curly bracket: ‘}’前面需要加空格(同上)

All using directives must be placed inside of the namespace: using指令需要移到命名空间内

例如:

using System;
using System.Text;

Namespace StyleCopDemo
{
Public class Person
{
}
}
//应该为
Namespace StyleCopDemo
{
using System;
using System.Text;

Public Class Person
{
}
}

这个我也有点晕了!不过有一篇文章http://blogs.msdn.com/b/abhinaba/archive/2006/08/21/709051.aspx,介绍了这样做的好处!

Adjacent elements must be separated by a blank line:紧邻的元素之间必须用空格行隔开,例如using命名空间和namespace之间。

An opening curly bracket must not be followed by a blank line: ‘{‘下面一行不允许是空行

A closing curly bracket must not be preceded by a blank line: ‘}’上面一行不允许是空行

Using directives must be sorted alphabetically by the namespaces:using语句必须要按照字母排序(顺序)

The code must not contain multiple blank lines in a row:代码中不允许一行中有多行空行。

错误:

正确为:

文档规范:

The class must have a documentation header:定义的类必须含有文件头标志’///’而且其中的内容介绍最好是带有空格的(有点变态),不然可能会报’ The documentation text within the summary tag does not contain any whitespace between words, indicating that it most likely does not follow a proper grammatical structure required for documentation text’,例如:

    /// <summary>
/// 用户类 用户基本信息
/// </summary>
public class Person
{
}

The file has no header, the header Xml is invalid, or the header is not located at the top of the file:

需要在文件开头上加:

//-------------------------------------------------------
// <copyright file="Person.cs" company="MyCompany">
// Copyright MyCompany. All rights reserved.
// </copyright>
//-------------------------------------------------------

注意缩进格式。

A C# document may only contain a single class at the root level unless all of the classes are partial and are of the same type:【猜】C#文档一般只能包含对一个类的描述。除非这些类是partial类型或者是同类型的。这种错误一般是在一个*.cs文件中有多个不同类型的类。

函数规范:

The method must have an access modifier:方法需要访问修饰符(public private protected..)

The method must have a documentation header:方法必须要文档说明就是以‘///’开头的。

‘///’规范要求:

标签内的内容不允许为空。内容最好是用空格隔开否则会报‘The documentation text within the summary tag does not contain any whitespace between words, indicating that it most likely does not follow a proper grammatical structure required for documentation text’;

实例:

 

字段规范:

The field must have an access modifier:字段必须要有修饰符

Variable names must start with a lower-case letter:字段的名字必须是小写开头的

The field must have a documentation header:字段必须要有文档说明,以///开头的

有些程序员喜欢以_开头来命名字段,但是StyleCop是不推荐的。(Field names must not start with an underscore)

抱歉!评论已关闭.