[翻譯]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 和兩個其它類型耦合,Address 和Order,然而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.

以上圖的繼承層次爲例,ListControl 和Label 位於繼承的第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 dowhile, 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, includingHalstead 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

希望能對您有所幫助

祝編程愉快!)


轉載自:風海迷沙

原文網址: http://www.cnblogs.com/fhmsha/archive/2008/04/08/1142354.html

發佈了39 篇原創文章 · 獲贊 104 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章