“代码审查”或是“代码评审”(Code Review),这是一个流程,当开发人员写好代码后,需要让别人来review一下他的代码,这是一种有效发现BUG的方法。由此,我们可以审查代码的风格、逻辑、思路……,找出问题,以及改进代码。因为这是代码刚刚出炉的时候,所以,这也是代码重构,代码调整,代码修改的最佳时候。所以,Code Review是编码实现中最最重要的一个环节。

长时间以来,Code Review需要有一些有效的工具来支持,这样我们就可以更容易,更有效率地来进行代码审查工作。

StyleCop是代码规范检查工具(Code Review 工具),它不仅仅检查代码格式,而是编码规范,包括命名和注释等。StyleCop可以帮助你更容易地进行这项活动。StyleCop现在包含了 200 个左右的最佳实践规则(best practice rules),这些规则与 Visual Studio 2005 和  Visual Studio 2008 中默认的代码格式化规则是一致的。它会根据预定义的C#代码格式的最佳实践,对我们的源代码进行检查,并给出不符合编码风格的错误提示。这一点来说与微软的另一个代码检查工具 FxCop 很相似,但 FxCop 是对 dll (compiled binaries) 进行检查,所以 FxCop 适用于新项目通过持续集成工具来使用的情况。也就是说 FxCop 是项目级别的,而 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:代码中不允许一行中有多行空行。

错误:

正确为:

文档规范:

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)

StyleCop放在 http://stylecop.codeplex.com/,最新版本是2012年7月5日发布 4.7.44版本。更多的信息可以关注StyleCop开发团队的blog:http://blogs.msdn.com/sourceanalysis

StyleCop提供了简单和有效的方式来对项目的代码编写风格进行检查。StyleCop可以多种方式运行,可以插件的方式在Visual Studio的IDE中运行;也可以MSBuild任务的方式运行,可整合到程序构建流程中;或者以命令行的方式运行,可针对一个或多个代码文件进行检查。

StyleCop的命令行使用方法:

Usage:    StyleCopCmd [options] [path]

Options:    -a         Process all source files found under the start path (default)    -cs {file} Analyze the specified file.    -f         Perform a full analyze (ignore cached results)    -u         Do not write results cache files.    -o {file}  Apply the given StyleCop options file to all projects and files    -l {file}  Save violation report at the given location. If omitted, saves               StyleCopViolations.xml in the current directory.    -p {path}  Attempts to discover StyleCop AddIn modules under the given               path. This flag may be specified multiple times to search under               multiple paths    -i         Ignore the default StyleCop AddIn path under the All Users\Applica tion Data folder

Conditional Compilation Flags:    -define:FLAG1;FLAG2;FLAG3

Path:    Specifies the path to begin search for source files.    If this is omitted, uses the current directory as the    start path.

CodeProject上有一篇文章详细的讲述StyleCop的使用方法 C# Code Reviews using StyleCop – Detailed Article

代码审查工具StyleCop的更多相关文章

  1. 代码审查工具 StyleCop 的探索

    最近我们Advent Data Service (ADS) 在项目上需要按照代码规范进行代码的编写工作,以方便将来代码的阅读与维护. 但是人工检查起来容易遗漏或者格式不统一, ReSharper又是收 ...

  2. C#代码审查工具 StyleCop

    SourceAnalysis (StyleCop)的终极目标是让所有人都能写出优雅和一致的代码,因此这些代码具有很高的可读性. SourceAnalysis (StyleCop)不是代码格式化(代码美 ...

  3. 静态代码审查工具FxCop插件开发(c#)

    FxCop是一款微软官方提供的.net平台代码审查工具,目的是检查我们编写的程序集的代码是否符合规范.FxCop默认提供的是微软默认的审查规则,而且该规则符合<Framework DesignG ...

  4. 代码审查工具之PMD操作指南

    上周客户要求对OA系统的代码质量进行了一个整体审查,并且要出一份报告给领导. 为此花了半天时间把代码审查工具PMD琢磨了下,现将具体操作步骤描述如下,以供大家参考! 1 前言 质量是衡量一个软件是否成 ...

  5. 代码走查工具StyleCop建议采用的规则总结

    代码走查工具StyleCop建议采用的规则总结 续接上篇:代码走查工具篇SytleCop的规则总结与翻译,本篇主要是以我个人的观点总结的一份建议使用的Rule点. 建议使用的Rule点 1.公共的接口 ...

  6. [转载] Jupiter代码审查工具使用参考

    转载自http://blog.csdn.net/jemlee2002/article/details/5715355 一.       Jupiter 是什么? 这里的 Jupiter 是一个开源的代 ...

  7. 代码审查工具Sonarqube安装

    前言:在项目开发当中,完成需求并上线是一件很开心的事情,但为了能按时上线功能不得不为了完成功能而写代码,写的时候觉得先把功能上了以后再回头优化此处代码,但真正上线之后你就会发现你再也不想去修改之前遗留 ...

  8. Jupiter Code Review Reference -- Jupiter代码审查工具使用参考

    Jupiter Code Review Reference -- Jupiter代码审查工具使用参考 (修改版) 原创 2010年07月06日 10:43:00 标签: 审查 / reference  ...

  9. Phabricator是什么,代码审查工具

    Phabricator是什么? Phabricator支持两种代码审查工作流:"review"(提交前审查)和 "audit"(提交后审查). Phabrica ...

随机推荐

  1. C# 类中索引器的使用二

    索引器(Indexer)是C#引入的一个新型的类成员,它使得类中的对象可以像数组那样方便.直观的被引用.索引器非常类似于属性,但索引器可以有参数列表,且只能作用在实例对象上,而不能在类上直接作用.定义 ...

  2. 经典.net试题

    经典.net面试题目 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有成员, 在类的内部才可以访问. pr ...

  3. gitlab 建仓的流程

    repository:仓库 Git global setup: git config --global user.name "Administrator" git config - ...

  4. siblings 使用

    //$(object).siblings().each(function () { // $(this).find("img").attr("class", & ...

  5. 突破python缺陷,实现几种自定义线程池 以及进程、线程、协程的介绍

    Python线程 Threading用于提供线程相关的操作,线程是应用程序中工作的最小单元. #!/usr/bin/env python # -*- coding:utf-8 -*- import t ...

  6. 11g Physical Standby配置

    一,准备   Database DB_UNIQUE_NAME Oracle Net Service Name Primary PROD PROD Physical standby PRODDG PRO ...

  7. .NET Nancy 详解(四) Self Host

    Self Host 使得Nancy 能够在任意application 中启动,无论是console 还是windows service.这期我们使用的版本是Nancy v0.4.0. Demo 首先看 ...

  8. 物化视图刷新慢--有可能是mv log被多个mv使用造成的

    同事说物化视图刷新慢,经检生产环境,发现部分物化视图刷新慢的原因是:由于同一个物化视图日志(mv log)被多个物化视图(mv)使用,不同的物化视图(mv)使用不同的刷新间隔,导致物化视图日志(mv ...

  9. 5.linux内核模块基础,内核模块学习

    linux内核模块基础 一.定义 Linux 内核的整体结构非常庞大,其包含的组件也非常多,如何使用这些组件呢: 方法 1:把所有的组件都编译进内核文件,即:zImage 或 bzImage,但这样会 ...

  10. Codeforces Beta Round #89 (Div. 2) E. Bertown roads(Tarjan、边双连通分量)

    题目链接:http://codeforces.com/problemset/problem/118/E 思路:首先要判断图是否是边双连通,这个Tarjan算法可以判断,若low[v] > dfn ...