120 likes | 313 Views
Email: zainnab@microsoft.com Twitter: @zainnab Blog: blogs.msdn.com/ zainna b. Visual Studio Tips and Tricks Code Metrics. Zain Naboulsi Sr. Developer Evangelist Microsoft. Code Metrics. What are they? Why should I care? Lines of Code (LOC) Cyclomatic Complexity
E N D
Email:zainnab@microsoft.com Twitter: @zainnab Blog:blogs.msdn.com/zainnab Visual StudioTips and TricksCode Metrics Zain Naboulsi Sr. Developer Evangelist Microsoft
Code Metrics • What are they? • Why should I care? • Lines of Code (LOC) • Cyclomatic Complexity • Class Coupling / Coupling Between Objects (CBO) • Depth of Inheritance Tree (DIT) • Maintainability Index • Using the Tool Window
What are they? “[…]a measure of some property of a piece of software or its specifications […]” http://en.wikipedia.org/wiki/Software_metric “[…] a set of software measures […]” http://msdn.microsoft.com/en-us/library/bb385914.aspx
Why should I care? • Find those areas that are (statistically) more likely to produce problems • Determine where to focus limited resources • Understand the current state of a solution / project • Keep track of where we have been and where we are going
Lines of Code (LOC) • Basic Info • Raw count of IL lines • Not a good measure by itself • Stats • N/A when used alone • Code Analysis • N/A http://academic.research.microsoft.com/Paper/2101790.aspx
Cyclomatic Complexity • Basic Info • Count of decisions • More decisions means more errors • Used often with LOC • Stats • 10 Suggested upper limit • Code Analysis • CA1502 • 25 Upper limit http://www.mccabe.com/pdf/mccabe-nist235r.pdf
Depth of Inheritance (DIT) • Basic Info • The maximum length from the node to the root of the inheritance tree • Three assumptions • Deeper == harder to predict behavior • Deeper == greater design complexity • Deeper == more reuse of code • Stats • Low • Less complex • Less reuse of code • High • More complex • More reuse of code • 5 or 6 for upper limit • Code Analysis • CA1501 http://www.pitt.edu/~ckemerer/CK%20research%20papers/MetricForOOD_ChidamberKemerer94.pdf
Class Coupling / Coupling Between Objects (CBO) • Basic Info • Number of classes a single class uses • Stats • 9Upper limit • Code Analysis • CA1506 • 80 for class • 30 for method http://www.pitt.edu/~ckemerer/CK%20research%20papers/MetricForOOD_ChidamberKemerer94.pdf
Maintainability Index • Basic Info • Calculates an index value between 0 and 100 that represents the relative ease of maintaining the code • Stats • High == good • Low == bad • Ratings • Green (20 – 100) • Yellow (10 – 19) • Red (0 – 9) • Code Analysis • CA1505 • 19 or lower threshold