admin管理员组

文章数量:1530517

2024年7月2日发(作者:)

微软编码规范检查工具StyleCop 介绍

微软编码规范检查工具StyleCop 介绍

一. 功能介绍

StyleCop analyzes C# source code to enforce a set of style and consistency rules. It can be run

from inside of Visual Studio or integrated into an MSBuild project

当前最新版本为:

StyleCop 4.3.3.0

支持 Visual Studio 2010 和 VS 2008!

下载地址:

/sourceanalysis/Release/?ReleaseId=1425#

VoteBreakdown

SourceAnalysis (StyleCop)不是代码格式化(代码美化)工具,而是代码规范检查工

具(Code Review 工具),它不仅仅检查代码格式,而是编码规范,包括命名和注释等。

SourceAnalysis (StyleCop)目的是帮助项目团队执行一系列常用的源代码格式规范,

这些规范是关于如何开发布局规整,易读,易维护并且文档良好的优雅代码的(help teams

enforce a common set of best practices for layout, readability, maintainability, and

documentation of C# source code)。

SourceAnalysis (StyleCop) 现在包含了 200 个左右的最佳实践规则(best practice

rules),这些规则与 Visual Studio 2005 和 Visual Studio 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 find highly readable.

SourceAnalysis (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 )

furenjun

微软编码规范检查工具StyleCop 介绍

 命名(Naming of elements, fields and variables )

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

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

文件内容(Allowed contents of files )

Debugging文本(Debugging text)

开始使用这些工具时可能会觉得对我们要求太苛刻,但根据微软自己的经验:after a

short adjustment period, they came to appreciate the rules enforced by Source Analysis, and

even began to find it difficult to read code not written in this style.

二. 使用示例

2.1 安装好后,新建一个测试项目,在解决方案资源管理器中右击要检查的类,在弹出的快

捷菜单中将看到 Run StyleCop 选项.

furenjun

微软编码规范检查工具StyleCop 介绍

2.2 检查后的结果如下图所示。对不合规范的写法将在错误列表中结出警告。

2.3 右击错误列表中的警告项,在弹出的快捷菜单中,点击“显示错误帮助项”,styleCop 将

会给出错原因、规则定义及解决方法与示例说明。

furenjun

微软编码规范检查工具StyleCop 介绍

2.4出错原因、规则定义及解决方法与示例说明

furenjun

微软编码规范检查工具StyleCop 介绍

furenjun

微软编码规范检查工具StyleCop 介绍

2.5 完整的无警告信息的示例类

附:

代码规范:Microsoft .NET 类库开发的设计准则

/zh-cn/library/

furenjun

本文标签: 工具规范检查