天天看點

(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量

(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量

[翻譯]Visual Studio2008的新功能:代碼度量

釋出:yaosansi | 釋出時間: 2008年4月12日 <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> name="google_ads_frame" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5505410570855815&dt=1220876824165&lmt=1220418432&output=html&slotname=6998211651&correlator=1220876824164&url=http%3A%2F%2Fwww.yaosansi.com%2Fpost%2F1252.html&ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fcomplete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26client%3Dfirefox-a%26rls%3Dorg.mozilla%253Aen-US%253Aofficial%26hs%3Doc0%26q%3Dcode%2Bmetrics%26btnG%3DGoogle%2B%25E6%2590%259C%25E7%25B4%25A2%26meta%3Dlr%253Dlang_zh-CN%257Clang_zh-TW%26aq%3Df%26oq%3D&frm=0&cc=100&ga_vid=3262311204943815000.1220876824&ga_sid=1220876824&ga_hid=1548006729&flash=9.0.124&u_h=900&u_w=1440&u_ah=870&u_aw=1440&u_cd=16&u_tz=480&u_his=1&u_java=true&u_nplug=24&u_nmime=71" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" width="468" frame height="15"> <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> name="google_ads_frame" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5505410570855815&dt=1220876824184&lmt=1220418432&prev_slotnames=6998211651&output=html&slotname=8242161180&correlator=1220876824164&url=http%3A%2F%2Fwww.yaosansi.com%2Fpost%2F1252.html&ref=http%3A%2F%2Fwww.google.cn%2Fsearch%3Fcomplete%3D1%26hl%3Dzh-CN%26newwindow%3D1%26client%3Dfirefox-a%26rls%3Dorg.mozilla%253Aen-US%253Aofficial%26hs%3Doc0%26q%3Dcode%2Bmetrics%26btnG%3DGoogle%2B%25E6%2590%259C%25E7%25B4%25A2%26meta%3Dlr%253Dlang_zh-CN%257Clang_zh-TW%26aq%3Df%26oq%3D&frm=0&cc=100&ga_vid=3262311204943815000.1220876824&ga_sid=1220876824&ga_hid=1548006729&flash=9.0.124&u_h=900&u_w=1440&u_ah=870&u_aw=1440&u_cd=16&u_tz=480&u_his=1&u_java=true&u_nplug=24&u_nmime=71" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" width="250" frame height="250">

原文:部落格園- 風海迷沙-http://www.cnblogs.com/fhmsha/archive/2008/04/08/1142354.html

原文發表位址: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:

(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量

下面這張圖檔顯示一個虛構的商業項目應用執行了代碼度量的結果(譯注:我拿自己的一個項目抓個圖,友善使用簡體中文版的朋友進行對比):

(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量

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,這個數字越大,表明改動它時對其它類型的影響就越大。如果這個值比較低則表明它的代碼重用的可能性越高。

下面這張圖耦合值是如何被計算出來的:(譯注:說白了就是沒有這個類,會有幾個類會挂掉。)

(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量

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:

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

(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量

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 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:

下面這張圖展示了複雜度是如何被計算的:

(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量

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:

下面這張圖顯示了代碼行數是如何被計算的:

(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量

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
(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量
High Maintainability Between 20 and 100 inclusive
(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量
Moderate Maintainability Between 10 and 19 inclusive
(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量
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.

可維護性指數的列也包括了一個圖示,它可以直覺的表示出可維護性的總體指數,或是某複雜度很高的部分。下面的表格顯示了每個圖示訓示的範圍:

圖示 級别 範圍
(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量
高可維護性 在20 到100 之間
(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量
中等可維護性 在10 到19 之間
(轉)Visual Studio2008的新功能:代碼度量 [翻譯]Visual Studio2008的新功能:代碼度量
低可維護性 在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好像有第三方插件可以做到:

  • 利用Together For VS.NET檢查所編寫的代碼是否夠規範
  • Together Developer 2005 for Microsoft Visual Studio .NET

這裡有另外的文章也許可以參考:

Visual Studio 2008 Code Metrics Saves you Money

繼續閱讀