StyleCop

      目前我们项目接近一个milestone,在十月中旬交付一个版本之后,会有一些新人加入项目开发的过程。为了新人能够尽快上手,在十一前的团队会议上,我们决定给所有的代码添加注释。然后我想起当时实习的时候用过的一个代码规范的一个小工具,如果所有的代码都通过小工具的检测,那么会让这些代码从某些方面看起来很像。这样会使得新人能够更好的理解代码。所以,我就去尝试找到一款支持Visua Studio的代码规范的工具。在网上搜了一下之后找到了StyleCop,这是一个开源的代码规范工具,还是挺好用的。下面简单介绍一下这个工具:

1. 安装和下载(这个去官网下载就好了,项目组的同学去server上找也有)

2. 使用

       在安装好之后,StyleCop会以插件的形式集成到Visual Studio。在代码上单击右键之后,会出现下图所示的界面。单击Run StyleCop就可以了。

     检查完了之后,会在Error List里面给出一系列的warnings……:

      然后逐一修改就好了。具体的警告内容可以看下载包里面的本地手册,也可以去查看在线的规则说明文档:http://www.stylecop.com/docs/StyleCop%20Rules.html。其中有一个规则是文件需要文件头,这一块我的解决方案是采用博文:http://www.cnblogs.com/deepnight/archive/2010/11/15/1877569.html中的解决方案。我把修改了之后的代码块贴出来:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics

Public Module FileHeaderCreator
    Sub DocumentFileHeader()
        Dim doc As Document
        Dim docName As String
        Dim companyName As String = "your company"
        Dim authoerName As String = "your name"
        Dim copyrightText As String = String.Format("Copyright (c) {0} {1}. All rights reserved.", Date.Now.Year, companyName)

        ' Get the name of this object from the file name
        doc = DTE.ActiveDocument

        ' Get the name of the current document
        docName = doc.Name

        ' Set selection to top of the document
        DTE.ActiveDocument.Selection.StartOfDocument()

        ' Write copyright tag
        DTE.ActiveDocument.Selection.Text = "// ------------------------------------------------------------------------------------"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// <copyright file = """ + docName + """ company = """ + companyName + """>"

        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "//    " + copyrightText


        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "//  </copyright>  "

        DTE.ActiveDocument.Selection.NewLine()

        ' Write author name tag
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// <author>" + authoerName + "</author>"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.NewLine()

        DTE.ActiveDocument.Selection.Text = "// <date>" + String.Format("{0:D}", Date.Now) + "</date>"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.NewLine()

        DTE.ActiveDocument.Selection.Text = "// <description>  "


        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// </description>  "

        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "// ------------------------------------------------------------------------------------"
        DTE.ActiveDocument.Selection.NewLine()
    End Sub

End Module

      在使用StyleCop之后,就准备把Unit Test补上。至于其他的测试就再说吧……路漫漫其修远兮……

posted on 2012-10-05 10:55  hibix  阅读(554)  评论(0编辑  收藏  举报

导航