Visual Studio 2017 code metrics is a set of important software tools for measuring the quality, code maintainability, and complexity of code written by a developer. These metrics also provide developers better insight into the code they have written by understanding the types of classes/methods/modules that should be reworked and thoroughly tested, identify potential risks, current state of code, track progress of development, and so forth. Code metrics are available with the VS Community, Premium, and Ultimate editions. Developers also can download the extension and install it in Visual Studio. It's a good practice to use code metrics to validate code; it will not only improve code quality but also make your application maintainable and less complex.
Software Measurements Metrics
Visual Studio provides following the list of the metrics to help you better understand your code:
- Maintainability Index
- Cyclomatic Complexity
- Depth of Inheritance
- Class Coupling
- Lines of Code
Maintainability Index is a software metric that indicates how maintainable (easy to support and change) the source code is. A high value of the Maintainability Index means better code maintainability. The value of this index is between o to 100. Different color codes are used to signify the maintainability index. For example, a green rating, between 20 and 100, indicates that the code has highly maintainability. A yellow rating, between 10 and 19, indicates that the code is moderately maintainable. A red rating, between 0 and 9, indicates low maintainability.
As per Wikipedia: 'Cyclomatic complexity is a software metric (measurement), used to indicate the complexity of a program.' This metric measures the structural complexity of your code. It indicates a number of branches in the code by calculating the number of different code paths in the flow of the program. The value of this metric increases if we have nested if statements or switch statements. Your code will be highly maintainable if the value of this metric is lower.
Depth of Inheritance
This metric calculates how far down a class is declared in the inheritance hierarchy. The deeper the hierarchy in the inheritance tree the class is, the more complex it will be to develop, test, and maintain. A developer must implement inheritance carefully by keeping in mind that code should remain reusable and less complex.
Class coupling is a measure on how one class is connected or dependent with another class. Good practice is to always implement fewer dependent classes or a low class coupling. A lower value of this metric is preferred because it also increases the complexity in your code.
Lines of Code
This one is relatively simple; it indicates the approximate number of lines of code written in a program. The count is based on the intermediate code generated by the Framework. A developer should design methods so that there are fewer lines of code. A high count indicates a method is trying to do too many things; that means a complex structure and it will be hard to maintain.
How To Generate Metrics Data
You can start calculating code metrics from the Analyze menu -> "Calculate Code Metrics for Solution." In case you have multiple project selections, the menu option will also provide the "Calculate Code Metrics for Selected Project (s)" submenu.
Figure 1: Analyze code metrics
Alternatively, you can start code metrics for a single project or an entire solution from the project content menu. Right-click -> "Calculate Code Metrics."
Figure 2: 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.
Figure 3: Code metrics result window
You also can display the "Code Metrics Results" window by clicking the Analyze menu -> Windows -> "Code Metrics Results."
Figure 4: Code metrics result
A developer can filter the Code metrics data based on the type of code metrics and by providing min and max value of indexes of the selected metrics. The filter feature helps a developer easily find the code block or function they need to work on.
Figure 5: Filter option
You can Add/Remove metrics columns by selecting the options from the "Add/Remove Columns" options. By default, all the metrics are checked.
Figure 6: Add/Remove columns
To export result data in Excel format, right-click in Code metrics results and select "Export to Excel." A developer can use the data for analysis once they have the information exported to Excel format.
Figure 7: Export to Excel
That's it for today. Visual Studio's Code Metrics features are very powerful and, when used, ensure your code is written properly and well maintained. Every enterprise has their own standards of code maintenance, but I would recommend running the code metrics tool regularly for better code quality and good practice.