[翻译]Visual Studio2008的新功能:代码度量

原文发表地址:Visual Studio 2008 Code Metrics Saves you Money

原文发表时间:Wednesday, October 03, 2007 7:00 AM

原作者:David M. Kean

While we've already briefly spoken about Code Metrics when we announced it, I thought I would discuss it in a little more depth, in particular the metrics it provides.

As mentioned previously, Code Metrics is a new tool window that helps users find and act upon complex and un maintainable areas within an application.

我们已经在这篇文章简要的介绍了关于代码度量功能的信息,我认为应该在这里更深入的讨论一下,特别是它提供的度量标准。

就像之前提到的一样,代码度量功能是一个新的工具窗口,它帮助用户在里面查找到复杂和不可维护的区域。

The following shows the results of running Code Metrics over a fictional business application:

Code Metrics Results tool window

下面这张图片显示一个虚构的商业项目应用执行了代码度量的结果(译注:我拿自己的一个项目抓个图,方便使用简体中文版的朋友进行对比):

20080408CodeMetrics

As you can see from above, for Visual Studio 2008, we're providing five metrics. These are detailed below.

就像你在上面看到的,我们为Visual Studio 2008 提供了5个度量值,以下是它们的详细信息及计算方法:

Class Coupling

At each level, this indicates the total number of dependencies that the item has on other types. This number excludes primitive and built-in types such as Int32, String and Object. The higher this number, the more likely changes in other types will ripple though this item. A lower value at the type level can indicate candidates for possible reuse.

The following shows how coupling is calculated:

类耦合

在每个级别上,它表明了类对其它类型的依赖总数。这个数字不包括基类(原始类型)比如Int32,String和Object,这个数字越大,表明改动它时对其它类型的影响就越大。如果这个值比较低则表明它的代码重用的可能性越高。

下面这张图耦合值是如何被计算出来的:(译注:说白了就是没有这个类,会有几个类会挂掉。)

Class Coupling

For example, as you can see above, Account is coupled to two other types, Address and Order, whereas Country is not dependent on any other type.

以上面的图为例Account 和两个其它类型耦合,AddressOrder,然而Country 没有依赖于任何类型。

Depth of Inheritance

At the type level, depth of inheritance indicates the number of types that are above the type in the inheritance tree. For example, a type that derives directly from Object would have a depth of inheritance of 1. At the namespace and project level, this indicates the highest depth of inheritance of all the types contained within it. This number does not take into consideration the depth of any implemented interfaces. Deep inheritance trees can indicate an over-engineering of a problem and can increase the complexity of testing and maintaining an application.

继承深度

在这个度量值级别上,继承深度指这个类型在继承树上的有多少类型。例如,一个类型直接从Object上继承的话,它的深度就是1。在命名空间和项目中,它表明所有类型(包括自身)的最大的深度值。这个数字不考虑任何应用接口。深度继承树可以在维护一个应用程序表现出过度设计和增加测试复杂度的问题。

The following shows how depth is calculated:

下面展示了深度是如何被计算的:

Depth Of Inheritance

For example, in the above inheritance hierarchy, ListControl and Label have a depth of inheritance of 3, whereas Component has a depth of inheritance of 1.

以上图的继承层次为例,ListControlLabel 位于继承的第3层,然而Component 的深度为1.

Cyclomatic Complexity

At each level, this measures the total number of individual paths through the code. This is basically calculated by counting the number of decision points (such as if blocks, switch cases, and do, while, foreach and for loops) and adding 1. This number is also a good indication on the number of unit tests it will take to achieve full line coverage. Lower is typically better.

圈复杂度

在每个级别上,它代表了代码段的单独路径总数。它基本上是由判断点的个数来计算得出的(例如if块,switch块和do,while,foreach和for的循环)每个判断点累加1。这个数字也很好的表明了单元测试的个数,将覆盖整行。它的数值低表明好一些。

The following shows how complexity is calculated:

下面这张图展示了复杂度是如何被计算的:

Cyclomatic Complexity

Lines of Code

At each level, this is a measure of the total number of executable lines of code. This excludes white space, comments, braces and the declarations of members, types and namespaces themselves. Lower is typically better.

代码行数

在每一个级别上,这是一个可执行代码的总行数。它不包括空白,注解,花括号,成员、类、命名空间的声名。数值低代表好一些。

The following shows how the lines are calculated:

下面这张图显示了代码行数是如何被计算的:

Lines of Code

Maintainability Index

At the member and type level, this is an index from 0 to 100 indicating the overall maintainability of the member or type. At the namespace and assembly level, this is an average of the maintainability index of all types contained within it. This index is based on several other metrics, including Halstead Volume (which factors in the number and use of operands and operators), Cyclomatic Complexity and Lines of Code. A low number indicates code that is complex and hard to maintain.

可维护性指数

在成员的级别上,这个指数是从0到100的,表明了成员或者类的大致的可维护性。在命名空间级别,这是其所包含的所有的类型的可维护性的平均值。这个数是基于多个指标,包括Halstead Volume(操作数和运算符和指数因素),圈复杂度和代码行数。指数较低表明代码复杂和难以维护。

The Maintainability Index column also includes a icon that gives a quick indication as to the overall maintainability and complexity of a particular item. The following table shows the range at which an icon is shown:

Icon Level Range
Green High Maintainability Between 20 and 100 inclusive
Yellow Moderate Maintainability Between 10 and 19 inclusive
Red Low Maintainability Between 0 and 9 inclusive

These icons allow you to see at a glance any trouble spots that you should start to focusing on or filling bugs against.

可维护性指数的列也包括了一个图标,它可以直观的表示出可维护性的总体指数,或是某复杂度很高的部分。下面的表格显示了每个图标指示的范围:

图标 级别 范围
Green 高可维护性 在20 到100 之间
Yellow 中等可维护性 在10 到19 之间
Red 低可维护性 在0 到9 之间

这些图标可以让你迅速浏览并找到问题所在。

Conclusion

Now that you know what these metrics measure, and how they are calculated, the next thing you'll want to know is what you should do when you start to see some red in your application. In a future post, I will cover why should should care about each metric, and how to go about fixing some of the common issues that cause particular metrics to high (or low in the case of Maintainability Index).

小结

现在我们知道这些度量的尺度、以及它们如何被计算出来,下面你可能想知道当你在你的应用程序中看到一些红色的图标应该如何做。在将来的文章中,我将告诉大家为什么应该关注每个度量,以及如何来修复一些引起特别度量值太高的问题(或者在可维护性中的值太低)。

 

 

(译注:作为微软预览Visual Studio 2007(Orcas)新功能之一的程序复杂度度量(Code Metrics)这一新功能,今天偶尔看到,却不知列出的指标为何,又是如何计算,在园子里找了一下,虽然文章过时但好像还没有发过,msdn有篇中文:VS 2008 - 如何使用 Code Metrics,但介绍过于简单,由其最后一句:如上圖所視,這個功能目前計算 5 個指標:Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Class Coupling, and Lines of Code。這些值的意義稍後會再討論。

于是找来英文介绍,译之。

这个功能的调用非常简单,在您的解决方案上点击右键,约中间位置会出现“计算代码度量值”,点击即可。

vs2005好像有第三方插件可以做到:

这里有另外的文章也许可以参考:

Visual Studio 2008 Code Metrics Saves you Money

希望能对您有所帮助

祝编程愉快!)

风海迷沙

Tag标签: vs2008
posted @ 2008-04-08 14:43 风海迷沙 阅读(3424) 评论(37)  编辑 收藏

  回复  引用  查看    
#1楼 2008-04-08 15:08 | WilsonWu      
喜欢你右侧的大图
  回复  引用  查看    
#2楼 2008-04-08 15:18 | dudu      
请修改一下摘要中有问题的代码:
<br&nbsp;&nbsp;
  回复  引用  查看    
#3楼 2008-04-08 15:34 | 老翅寒暑      
Cyclomatic Complexity 最正确的翻译叫做“循环复杂度”
  回复  引用  查看    
#4楼 2008-04-08 15:50 | Midapex Village      
我也稀饭那个图。
  回复  引用  查看    
#5楼 2008-04-08 15:50 | Justin      
文章不错,公告栏你的图片更牛!哈
  回复  引用  查看    
#6楼 2008-04-08 16:05 | BlueMountain      
多谢
  回复  引用  查看    
#7楼 2008-04-08 16:06 | works guo      
应该组成一个Not MVP
  回复  引用  查看    
#8楼 [楼主]2008-04-08 16:11 | 风海迷沙      
@dudu
已修复
  回复  引用  查看    
#9楼 [楼主]2008-04-08 16:15 | 风海迷沙      
@老翅寒暑
但是vs简体中版中译作圈复杂度,偶也只好跟着译了。原来以为是类的继承循环呢,看了翻译才知道是循环嵌套复杂度
  回复  引用  查看    
#10楼 2008-04-08 16:36 | Anders Cui      
翻译的不错,图很酷
  回复  引用    
#11楼 2008-04-08 17:00 | A.Z! [未注册用户]
申请NVP需要什么条件。
  回复  引用  查看    
#12楼 2008-04-08 17:06 | 风之影      
喜欢你的NVP哈哈,
  回复  引用  查看    
#13楼 2008-04-08 17:10 | Midapex Village      
--引用--------------------------------------------------
A.Z!: 申请NVP需要什么条件。
--------------------------------------------------------
条件是你不是MVP就可以了。
  回复  引用  查看    
#14楼 2008-04-08 17:37 | TT.Net      
期待你的下一篇这方面的介绍
  回复  引用  查看    
#15楼 2008-04-08 18:23 | Cure      
我的VS2008专业版的,居然不支持。。。。。。。。
  回复  引用  查看    
#16楼 2008-04-08 18:43 | 李涛      
VS2008好爽
  回复  引用  查看    
#17楼 2008-04-08 20:46 | 生鱼片      
微软的东西都用最全的版本
  回复  引用  查看    
#18楼 2008-04-08 21:39 | 天方      
@风海迷沙
圈复杂度这种叫法没错。
  回复  引用  查看    
#19楼 2008-04-08 22:07 | 李战      
这个东东貌似不错,等俺好好琢磨一番。
要是能实现软件可靠性分析就耕牛了
  回复  引用  查看    
#20楼 2008-04-08 22:14 | 李战      
哇,楼主挂的牌子有一种英雄气概啊!顿时让所有的MVP的牌子,黯然失色!
我赌楼主是O型血,如何?
如果俺赢了,牌子借我挂挂。如果楼主赢了,俺送楼主一块金色的NVP。

  回复  引用  查看    
#21楼 2008-04-09 03:34 | 怪怪      
嗯我支持NVP的说法 :)

建立个小组讨论一下? 如果想认真的来, 为那些不被商业公司看重的勤勤恳恳的程序员做些什么, 我愿意出自己的一份力, 包括但不限于: 大家想拍什么我可以当免费枪手, 组织吃喝嫖赌抽等活动, 联系真正的民工兄弟帮助打架, 等等~

还有这篇文章不错,像我这样的一般都不会注意这些功能, 白白的把有用的东西给忽略了, 感谢一下。
  回复  引用  查看    
#22楼 2008-04-09 08:31 | zoti      
這個功能在哪里打開的啊?沒找到呢。
哪位告訴我一下。
  回复  引用  查看    
#23楼 2008-04-09 08:35 | zoti      
我知道了,原來是我的這個版本沒有這個功能。
  回复  引用    
#24楼 2008-04-09 09:26 | imxjb [未注册用户]
嘿嘿,大家对nvp的关注倒超过了对文章本省。
  回复  引用  查看    
#25楼 [楼主]2008-04-09 09:51 | 风海迷沙      
@李战
我也是偶然间发现的这个功能,可维护性指数类似于给自己的代码打分的功能。有助于我们写出高质量的代码:)
我是B型。。。
@怪怪
那张图只是无聊时自己PS的,为自己没有机会申请到MVP的自嘲行为,如果不小心得罪了某些人莫要见怪呀。
  回复  引用  查看    
#26楼 [楼主]2008-04-09 09:52 | 风海迷沙      
@zoti
我的是vs2008 team suite简体中文版,从官网下来的。express版没有这个功能吗?
  回复  引用    
#27楼 2008-04-09 10:37 | kenjacky [未注册用户]
关注ing
正考虑更换到vs2008
  回复  引用  查看    
#28楼 2008-04-09 11:59 | RicCC      
这个东西非常好,谢谢分享
  回复  引用  查看    
#29楼 2008-04-09 13:04 | 怪怪      
@风海迷沙
呵呵, 技术人员有什么可怕的, MVP有MVP的好, NVP有NVP的好, 谁看不起别人或者谁因为这点小事生气, 咱就B4谁~ :)
  回复  引用  查看    
#30楼 [楼主]2008-04-09 13:07 | 风海迷沙      
@RicCC
感谢你找到了某些人找不到这个功能的原因!
以下为引用
“重新在VS 2008安装向导中选择Team Developer and Tester Tools,估计应当是包含在Code Analysis Tools中,安装好这个组件后项目和解决方案右键菜单就有这个功能了”
http://www.cnblogs.com/RicCC/archive/2008/04/09/1144618.html
  回复  引用  查看    
#31楼 2008-04-09 14:01 | romce      
xuexizhong...
  回复  引用  查看    
#32楼 2008-04-09 19:33 | works guo      
所以可以组一个关于组。NVP!把处在进步中的人群组织起来
  回复  引用  查看    
#33楼 2008-07-29 18:11 | 金色海洋(jyk)      
1、好像 代码行数 指的是IL的行数。

  回复  引用  查看    
#34楼 2008-07-29 18:14 | 金色海洋(jyk)      
代码的行数 – 指明代码中的大概行数。该计数基于 IL 代码,因此并不是源代码文件中的确切行数。计数过高可能表示某个类型或方法正在尝试执行过多的工作,应予以拆分。还可能表示该类型或方法可能难以维护。

摘自 http://msdn.microsoft.com/zh-cn/library/bb385914.aspx
  回复  引用  查看    
#35楼 2008-08-03 22:44 | Steven Chen      
喜欢你左边的大图

另,代码行数到底指什么 lz和ls给讲讲哦

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2008-04-08 16:29 编辑过


相关链接:

历史上的今天:
2006-04-08 test