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.
使用圖例如下: