码迷,mamicode.com
首页 > 其他好文 > 详细

[Tool] 使用CodeMaid自动程序排版

时间:2014-10-01 23:56:21      阅读:587      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   os   使用   ar   数据   sp   

[Tool] 使用CodeMaid自动程序排版

前言

「使用StyleCop验证命名规则」这篇文章,指引开发人员透过StyleCop这个工具,来自动检验项目中产出的程序代码是否合乎命名规则。

但是在项目开发的过程中,如果只是验证命名规则、而没有统一程序排版,项目中很容易就会出现类似下列范例的程序代码产出。这样的产出,虽然能够正常地提供项目功能、并且符合微软的命名规则,但是因为程序排版凌乱的问题,大幅降低了这份程序代码的可维护性。

  • Bad Code

    public class Class1
    {
        private string _name = "Clark";
    
        public string GetResult()
        {
            return (_count01 + _count02).ToString();
        }
    
    
    
    
        private int _count01 = 1;
    
        private int _count03 = 3;
    
        public string GetName()
        {
            return _name;
        }
    
        private int _count02 = 2;
    }
    

本篇文章介绍如何透过CodeMaid这个工具,来自动整理项目中程序代码的排版,在不增加开发人员负担的前提下,让团队的程序代码产出趋于一致、大幅提高程序代码的生产质量。主要为自己留个纪录,也希望能帮助到有需要的开发人员。

  • Good Code

    public class Class1
    {
        private int _count01 = 1;
        private int _count02 = 2;
        private int _count03 = 3;
        private string _name = "Clark";
    
        public string GetName()
        {
            return _name;
        }
    
        public string GetResult()
        {
            return (_count01 + _count02).ToString();
        }
    }
    

安装

  1. 首先至微软的官方网站,下载CodeMaid安装档:「CodeMaid_v0.7.4.vsix」。

    bubuko.com,布布扣

  2. 执行CodeMaid安装档:「CodeMaid_v0.7.4.vsix」,来安装CodeMaid。

    bubuko.com,布布扣

执行

  1. 使用Visual Studio开启项目。

    bubuko.com,布布扣

  2. Visual Studio上方工具栏中,开启CODEMAID选单、点选Configuration来开启CodeMaid设定画面。

    bubuko.com,布布扣

  3. CodeMaid设定画面中,进入Reorganizing->General设定页面,勾选「Run organize at start cleanup」后,点击Save按钮完成设定。

    bubuko.com,布布扣

  4. 后续就可以从Visual Studio上方工具栏中,开启CODEMAID选单、点选「Cleanup all Code」来自动排版项目内的所有程序代码。

    bubuko.com,布布扣

  5. 自动排版功能执行结束之后,开启项目内的程序代码,会发现程序代码内容已经排列整齐、干净,大幅提高程序代码的可维护性。

    bubuko.com,布布扣

延伸

CodeMaid所提供的程序代码自动排版功能,用起来很方便、排版结果也很简洁。但是在一些细节上,总是会有些许的排版定义,不符合团队成员对于程序代码质量的要求。不过还好的是,CodeMaid开放了许多排版条件的设定项目,让开发人员可以调整排版条件,来让排版结果趋近于团队成员对程序代码产出的要求。

bubuko.com,布布扣

1. Automatically run cleanup on file save

「Automatically run cleanup on file save」:位于Cleaning->General设定页面。当该选项设定为勾选时,会在档案存盘的同时,自动执行程序代码排版功能。

bubuko.com,布布扣

2. Run remove unused using statements

「Run remove unused using statements」:位于Cleaning->Visual Studio设定页面。当该选项设定为勾选时,会在执行程序代码排版功能时,移除没有使用的using定义。(开发阶段建议不要勾选该选项,因为移除未使用的using定义,会造成使用LINQ时找不到扩充方法的问题。)

bubuko.com,布布扣

3. Remove multple consecutive blank lines

「Remove multple consecutive blank lines」:位于Cleaning->Remove设定页面。当该选项设定为勾选时,会在执行程序代码排版功能时,移除连续多行的空白行。

bubuko.com,布布扣

4. Update #endregion tag with region name

「Update #endregion tag with region name」:位于Cleaning->Updae设定页面。当该选项设定为勾选时,会在执行程序代码排版功能时,为#endregion区域卷标加上区域名称。

bubuko.com,布布扣

5. Alphabetize members of the same group

「Alphabetize members of the same group」:位于Reorganizing->General设定页面。当该选项设定为勾选时,会在执行程序代码排版功能时,依照成员类型排序之后,增加依照字母顺序排序的工作项目。

bubuko.com,布布扣

参考数据

[Tool] 使用CodeMaid自动程序排版

标签:style   blog   http   io   os   使用   ar   数据   sp   

原文地址:http://www.cnblogs.com/clark159/p/4003542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!