Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Metrics Calculation in Visual Studio

What is the prefered score range for the code metrics calculation for the following

  • Maintainability Index
  • Cyclomatic Complexity
  • Depth of Inheritance
  • class Coupling
like image 598
Asad Avatar asked Jan 09 '10 14:01

Asad


People also ask

What is calculate code metrics in Visual Studio?

Developers can use Visual Studio to generate code metrics data that measure the complexity and maintainability of their managed code. Code metrics data can be generated for an entire solution or a single project.

How do I use code metrics in Visual Studio?

Right-click -> “Calculate Code Metrics.” Once you run the code metrics, Visual Studio will analyze the selected project/projects and display the code metrics results in the “Code Metrics Results” window. From the result window, you can drill down to each and individual method to analyze their metrics data.

How do you check code complexity in Visual Studio code?

From Visual Studio's menu, select Analyze -> Calculate Code Metrics. Then, either select “For Solution”, if you want check all the projects in the solution, or select the project you want. The results will be displayed in the Output window. Visual Studio shows the cyclomatic complexity for each function and property.


1 Answers

The theoretically optimal values are:

  • Maintainability index: 100. Higher values indicate better maintainability.
  • Cyclomatic complexity: 1. The number of different paths that code can take.
  • Depth of inheritance: 1. The number of class definitions above this one in the inheritance tree, not including interfaces.
  • Class coupling: 0. Number of other entities this entity is dependent on.

There are no hard and fast "good" ranges, though it's possible to make some general statements.

  • Having high per-method cyclomatic complexity suggests a method is getting too complicated.
  • Having an inheritance depth more than about 3 or 4 (of your own classes, not the framework's) is a trouble sign that you may be unnecessarily representing abstract relationships that aren't really in your software's domain.
  • Low class coupling is in general better, but sometimes it's unavoidable. To the extent possible, you should definitely minimize the dependency between namespaces, since there's much less reason for dependencies here.

A project could only reach all four values simultaneously by essentially doing nothing and being useless: software that does nothing and depends on nothing is certainly maintainable, but not a very good use of client dollars.

Therefore, all complexity is a tradeoff: additional so-called inherent complexity encodes more sophistication into the program, allowing it to expand the feature set. What you would like to avoid is accidental complexity introduced by a poor or deficient implementation.

like image 82
John Feminella Avatar answered Sep 22 '22 16:09

John Feminella