Code metrics are various mathematical and statistical ways to analyze source code and attempt to rank items according to various different yardsticks. Visual Studio 2008 code metrics rate a project's classes, modules, component classes and namespaces using various measurements that attempt to highlight potential sources of trouble.
Below is a brief explanation for each of these metrics. For the maintainability index, a green box indicates an acceptable measurement, a yellow triangle is a warning, and a red box is a trouble indicator; other measurements are expressed numerically. More detail is available at the Visual Studio Code Analysis Team Blog entry, "New for Visual Studio 2008 -- Code Metrics.")
- Maintainability Index: At member and type levels, this is expressed as a value between 0 and 100 to assess maintainability. For namespaces and assembly level items this is the average of the maintainability index for all constituent types. The index derives from the Halstead Volume (which is based on the number and use of operators and operands), cyclomatic complexity (see below), and lines of code (also below).
Higher is better than lower for this metric: values of 100-20 get a green box, 10-19 a yellow and 0-9 a red.
- Cyclomatic Complexity: A measure of the total number of paths through a code item. It is calculated by counting the number of decision points or total number of paths traveled through these code constructs: if, switch, do, while, foreach and for. This number also indicates how many unit tests will be needed to achieve complete line coverage.
Lower values are better than higher ones.
- Depth of Inheritance: At the type level, this value indicates how many types occur above the type being measured in the inheritance tree. At the namespace and project level, this value identifies the highest depth of inheritance for all constituent types.
Higher values can indicate over-engineered implementations that may increase test and maintenance complexity.
- Class Coupling: Indicates the number of other classes upon which an item depends, excluding primitives and built-in types (String, Object, Int32).
The higher the value, the more likely it is that the item will be affected by changes in those other classes. Low values at the type level, by contrast, indicates potential candidates for code reuse.
- Lines of Code: Total number of executable lines of code, excluding white space, comments, braces, declarations of members, types and namespaces themselves. Though a crude measure, it is reasonably predictive of complexity.
Lower counts are better than higher ones.
To generate Visual Studio 2008 code metrics, start from Solution Explorer, then right-click a solution or project and select Generate Code Metrics from the resulting pop-up menu. That's all there is to it! Working with these metrics helps to improve code readability, reusability, reliability and maintainability. What more could you want? Stay tuned, and I'll let you know in future tips!
Ed Tittel is a full-time writer and trainer whose interests include XML and development topics, along with IT Certification and information security topics. E-mail Ed with comments, questions, or suggested topics or tools to review.
Dig Deeper on .NET Framework 3.5 and Visual Studio 2008 development