代码审查工具StyleCop之二:规则说明

StyleCop检查的规则包括:

  • 布局(Layout of elements, statements, expressions, and query clauses )
  • 括号位置(Placement of curly brackets, parenthesis, square brackets, etc )
  • 空格(Spacing around keywords and operator symbols )
  • 行距(Line spacing )
  • 参数位置(Placement of method parameters within method declarations or method calls )
  • 元素标准排列(Standard ordering of elements within a class )
  • 注释格式(Formatting of documentation within element headers and file headers )
  • 命名(Naming of elements, fields and variables )
  • 内置类型的使用(Use of the built-in types )
  • 访问修饰符的使用(Use of access modifiers )
  • 文件内容(Allowed contents of files )
  • Debugging文本(Debugging text)

常用规则

基础

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:代码中不允许一行中有多行空行。

错误:

using System;
using System.Collections.Generic;
using System.Text;
using RNOP.UIDS.Model.GSM.WorkForm;


namespace RNOP.UIDS.WorkFlow.BLL
{

正确为:

using System;
using System.Collections.Generic;
using System.Text;
using RNOP.UIDS.Model.GSM.WorkForm;

 

namespace RNOP.UIDS.WorkFlow.BLL
{


文档规范:

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’;

实例:

2013年01月16日 - liuhz138 - liuhz138的博客

字段规范:

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)

posted @ 2015-12-09 17:07  帅姐  阅读(973)  评论(0编辑  收藏  举报