[Code quality] vs recommend a tool that comes with the code complexity analysis

Reprinted from:  https://blog.csdn.net/zh_geo/article/details/52954145

 

VS2012 -> Analyze -> Calculate code metrics feature. 

Code metrics (Code Metrics) software is used to measure the characteristics of a professional standard. In some aspects, it provides a useful quantitative feedback to developers.

View Code Metrics is simple: in the project or solution right click and choose "Calculate Code Metrics" Here are some similar results:

Results window five different measurement characteristics: cyclomatic complexity (Cyclomatic Complexity), inherited depth (Depth of Inheritance), the degree of coupling class (Class Coupling), the number (Lines of Code) lines of code, and may maintain a total index (Maintainability index). by expanding the tree node on the left, you can observe the class inside the method further from the entire project. but those digital features What does it mean?

 

Cyclomatic complexity (Cyclomatic Complexity)
cyclomatic complexity is the total number of linearly independent paths obtained when calling the method, based on the number if, switch, while and the like of the switch statement. If a method does not present such a sentence, it means the method only a feasible route, ring this case complexity value 1.

public int DoSomething(int x) {
if (x == 1) {
// code that is executed when x is 1
}

if (x == 2) {
// code that is executed when x is 2
}
// code that is executed even if x is neither 1 or 2
return x;
}


In this example, there are three possible routes exist, the value of x depends on the route which ones of:

  • 1. When x is 1
  • 2. When x is 2
  • 3. When x is not 1 nor 2

Cyclomatic complexity tell you two important things. First, it pointed out that the complexity of a comprehensive approach. Value as low as possible, if the number is high, it means that the processing method may cover too many different situations (for example, try to do too multi-fault tolerance), a method needs to be broken down into simpler just to meet the case of single responsibility methods (it will be easier to maintain).

Secondly, in order to ensure that each case is fully tested, you have to create a unique test cases for each route. Cyclomatic complexity need to write to tell you how many test cases to ensure that all possible scenarios have been covered.

If the cyclomatic complexity of a code segment value larger than the number of test cases, you will find that in some cases have not been processed, and may be hiding bug.


Inherited depth (Depth of Inheritance)
Inheritance is a depth of a succession of stages of the class, a starting value of 1 (all classes are inherited from Object) This example has three succession:

This value is as low as possible; if 3 or 4 in the depth of the inheritance above, your code may exceed the design criteria ( over-Engineered ), the code is difficult to maintain described.

数值越高,就越多子类需要修改 -- 消耗时间和金钱,也潜在地引入bug -- 如果你对基类做出重大变更的话.

只要你考虑下每个变更对于子类会引入一些潜在的bug,这些bug会影响到他们引用的所有对象... 你就懂我意思了.


类耦合度(Class Coupling)
类耦合度是计算一个类中引用到的对象类型数,不包括原始的和内置的类型,像int和string. 该值是越低越好; 值越高,就代表类绑定了越多类,也意味着存在越多依赖关系在它的中间环境中.

随着类的耦合度的增加,它就会变得难以分离出来用在另一个项目中. 简单来讲,类的耦合度越高,它的重用性就越低.

我们都看过很多理论案例说类要尽量可重用,但实际编程,大部分类都是高度针对特定项目而编写的,或多或少需要完全重写才能"重用"在另一个项目中.

如果类在它最初被创建的时候设计得更完善,我们就可以在之后省掉很多时间.


代码行数(Lines of Code)
代码行数是通过计算执行的代码行数来量度软件块的大小. 虽然和上面的特征一样,这个值也是越低越好(原因很简单,你以更少的代码完成功能就能减少你思考的负担),但在用这个特征前有几条告诫你需要知道:

  • 1.不是每行代码都相等. 有些行是非常简单,有些却非常复杂,会花费数小时来计算.
  • 2.它不计算已删除或已覆盖的代码
  • 3.有时将一行复杂的代码分割成几行会令它更容易理解; 在这样的情况下,增加了代码行但更容易维护.
  • 4.除非设计完善并彻底测试过,而且已成型的所有代码是出自你本人,否则代码行数不会精确反映投入软件中的努力的量.

要聪明地使用这个度量值. 在以前,代码行数出名地被滥用于测量每日的编程生产力,带来了灾难性的结果. 请记住,比起写代码,软件开发有更多重要的东西.

 

可维护指数(Maintainability Index)
最后是可维护指数,范围是0到100,用来指示所有类,成员,命名空间或项目的可维护性. 事实上,它是一个之前所有度量值的合计值,但它也同时包含一些额外的度量值,像霍尔斯特德量(Halstead Volume),该值用来量度程序的总长度和词汇数. 与之前的度量值不同,这个值是越高越好.

可维护指数还提供了一个红/黄/绿的交通灯图标,让你一眼看见问题的区域. 交通灯会转黄当数值低于20,当数值低于10时会转红,这时你需要重新检查你的代码以躲避危险.


总结
Visual Studio 2008的代码度量不会量度性能或者安全性,也不会帮助你解决任何bug. 它只会在开发时帮你找出软件中的隐藏问题区域.


你越早看到本文,就越节省你的时间和金钱(用于重构不可维护的代码). 你也会自我感觉良好,因为你知道你能够第一时间处理好程序.

 

英文原文: http://www.geekzone.co.nz/vs2008/4773

百度百科: http://baike.baidu.com/view/2830155.htm

 

代码度量(Code Metrics)是用来测量专业标准的软件特性. 在某些方面,它提供了量化的有用反馈给开发者.

查看代码度量值很简单:在项目或解决方案上按右键,选择"Calculate Code Metrics". 以下是一些类似的结果:

结果窗口中有五个不同的度量特性:圈复杂度(Cyclomatic Complexity), 继承深度(Depth of Inheritance), 类耦合度(Class Coupling), 代码行数(Lines of Code), 和一个总的可维护指数(Maintainability Index). 通过展开左边的树结点,你可以从整个项目深入观察类里面的方法. 但是特征中的那些数字是什么意思呢?

 

圈复杂度(Cyclomatic Complexity)
圈复杂度是调用方法时取得的线性独立路径的总数,基于if,switch,while和类似的分支语句的数量. 如果一个方法没有这样的语句存在,就表示方法中只有一条可行路线,这样的情况下圈复杂度的值为1.

public int DoSomething(int x) {
if (x == 1) {
// code that is executed when x is 1
}

if (x == 2) {
// code that is executed when x is 2
}
// code that is executed even if x is neither 1 or 2
return x;
}


在这个例子中,有三条可行的路线存在,走哪条路线取决于x的值:

  • 1.当x是1
  • 2.当x是2
  • 3.当x不是1也不是2

圈复杂度告诉你两件重要事. 首先,它指出一个方法的综合复杂度. 值越低越好,如果数值较高,就说明方法可能覆盖了过多不同情况的处理(例如,尝试做太多容错处理),说明方法需要分解成更简单的只满足一种情况的单一职责方法(这样会更容易维护).

其次,为了确保每种情况都被完全地测试,你必须为每条路线创建唯一的测试用例. 圈复杂度告诉你需要写多少个测试用例来确保所有可能的情况都已经被覆盖.

如果一个代码段的圈复杂度值多于测试用例的数量,你就会发现有些情况没有被处理,而且可能藏匿着bug.


继承深度(Depth of Inheritance)
继承深度是表示一个类的继承的级数,起始值为1(所有类都是继承自Object). 这个例子有3级继承:

该值是越低越好; 如果继承深度在3或4以上,你的代码可能超出设计标准(over-engineered),说明代码是难以维护的.

数值越高,就越多子类需要修改 -- 消耗时间和金钱,也潜在地引入bug -- 如果你对基类做出重大变更的话.

只要你考虑下每个变更对于子类会引入一些潜在的bug,这些bug会影响到他们引用的所有对象... 你就懂我意思了.


类耦合度(Class Coupling)
类耦合度是计算一个类中引用到的对象类型数,不包括原始的和内置的类型,像int和string. 该值是越低越好; 值越高,就代表类绑定了越多类,也意味着存在越多依赖关系在它的中间环境中.

随着类的耦合度的增加,它就会变得难以分离出来用在另一个项目中. 简单来讲,类的耦合度越高,它的重用性就越低.

我们都看过很多理论案例说类要尽量可重用,但实际编程,大部分类都是高度针对特定项目而编写的,或多或少需要完全重写才能"重用"在另一个项目中.

如果类在它最初被创建的时候设计得更完善,我们就可以在之后省掉很多时间.


代码行数(Lines of Code)
代码行数是通过计算执行的代码行数来量度软件块的大小. 虽然和上面的特征一样,这个值也是越低越好(原因很简单,你以更少的代码完成功能就能减少你思考的负担),但在用这个特征前有几条告诫你需要知道:

  • 1.不是每行代码都相等. 有些行是非常简单,有些却非常复杂,会花费数小时来计算.
  • 2.它不计算已删除或已覆盖的代码
  • 3.有时将一行复杂的代码分割成几行会令它更容易理解; 在这样的情况下,增加了代码行但更容易维护.
  • 4.除非设计完善并彻底测试过,而且已成型的所有代码是出自你本人,否则代码行数不会精确反映投入软件中的努力的量.

要聪明地使用这个度量值. 在以前,代码行数出名地被滥用于测量每日的编程生产力,带来了灾难性的结果. 请记住,比起写代码,软件开发有更多重要的东西.

 

可维护指数(Maintainability Index)
最后是可维护指数,范围是0到100,用来指示所有类,成员,命名空间或项目的可维护性. 事实上,它是一个之前所有度量值的合计值,但它也同时包含一些额外的度量值,像霍尔斯特德量(Halstead Volume),该值用来量度程序的总长度和词汇数. 与之前的度量值不同,这个值是越高越好.

可维护指数还提供了一个红/黄/绿的交通灯图标,让你一眼看见问题的区域. 交通灯会转黄当数值低于20,当数值低于10时会转红,这时你需要重新检查你的代码以躲避危险.


总结
Visual Studio 2008的代码度量不会量度性能或者安全性,也不会帮助你解决任何bug. 它只会在开发时帮你找出软件中的隐藏问题区域.


你越早看到本文,就越节省你的时间和金钱(用于重构不可维护的代码). 你也会自我感觉良好,因为你知道你能够第一时间处理好程序.

 

英文原文: http://www.geekzone.co.nz/vs2008/4773

百度百科: http://baike.baidu.com/view/2830155.htm

 

Guess you like

Origin www.cnblogs.com/0616--ataozhijia/p/11609167.html