天天看点

使用StyleCop进行代码审查

        stylecop analyzes c# source code to enforcea set of style and consistency rules. it can be run from inside of visual studio orintegrated into an msbuild project 

        sourceanalysis(stylecop)不是代码格式化(代码美化)工具,而是代码规范检查工具(codereview 工具),它不仅仅检查代码格式,而是编码规范,包括命名和注释等。sourceanalysis(stylecop)目的是帮助项目团队执行一系列常用的源代码格式规范,这些规范是关于如何开发布局规整,易读,易维护并且文档良好的优雅代码的(help

teams enforce a common set of best practices for layout, readability,maintainability, and documentation of c# source code)。

        sourceanalysis (stylecop) 现在包含了 200 个左右的最佳实践规则(bestpractice rules),这些规则与 visualstudio 2005 和  visualstudio 2008 中默认的代码格式化规则是一致的。

  sourceanalysis(stylecop)可以作为 visual studio 的插件运行.

  同时sourceanalysis (stylecop)也可以作为 msbuild 任务(安装时有选项)通过命令行执行。

  sourceanalysis(stylecop)是代码级别的,更适合于程序员在编程过程中使用。

  sourceanalysis(stylecop)不提供灵活的规则设置,而是使用所谓 one-size-fits-all的方式强制人们用同样的习惯书写代码,因此 sourceanalysis (stylecop)的终极目标是:the ultimate goal of source analysis is to allow you to produce elegant,consistent code that your team members and others who view your code

will findhighly readable.

  sourceanalysis(stylecop)检查的规则包括:

           布局(layout of elements, statements,expressions, and query clauses )

           括号位置(placement of curly brackets,parenthesis, square brackets, etc )

           空格(spacing around keywords andoperator symbols )

行距(line spacing )

           参数位置(placement of method parameterswithin method declarations or method calls )

           元素标准排列(standard ordering of elementswithin a class )

           注释格式(formatting of documentationwithin element headers and file headers )

命名(naming of elements, fields andvariables )

           内置类型的使用(use of the built-in types )

           访问修饰符的使用(use of access modifiers )

           文件内容(allowed contents of files )

           debugging文本(debugging text)

        开始使用这些工具时可能会觉得对我们要求太苛刻,但根据微软自己的经验:aftera short adjustment period, they came to appreciate the rules enforced by sourceanalysis, and even began to find it difficult to read

code not written in thisstyle.

使用图例如下:

使用StyleCop进行代码审查

继续阅读