zoukankan      html  css  js  c++  java
  • [Tool] 使用CodeMaid自动程序排版

    [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」。

      安装01

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

      安装02

    执行

    1. 使用Visual Studio开启项目。

      执行01

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

      执行02

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

      执行03

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

      执行04

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

      执行05

    延伸

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

    延伸01

    1. Automatically run cleanup on file save

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

    延伸02

    2. Run remove unused using statements

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

    延伸03

    3. Remove multple consecutive blank lines

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

    延伸06

    4. Update #endregion tag with region name

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

    延伸04

    5. Alphabetize members of the same group

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

    延伸05

    参考数据

  • 相关阅读:
    第8月第15天 app支持后台播放
    第8月第12天 python json.dumps danmu
    第7月第27天 c++11 boost
    第7月第26天 iOS httpserver
    第7月第25天 xcode bundle calayer动画
    我曾七次鄙视自己的灵魂
    learning shell display alert function(5)
    learning armbian steps(6) ----- armbian 源码分析(一)
    learning scala 数组和容器
    learning scala ide tools install
  • 原文地址:https://www.cnblogs.com/clark159/p/4003542.html
Copyright © 2011-2022 走看看