Use code metrics in visual studio to improve code quality

Time:2020-10-30

Code metrics are a set of software metrics that enable developers to better understand the code they are developing. With code metrics, developers can understand which types and / or methods should be reworked or tested more thoroughly. The development team can identify potential risks, understand the current status of the project, and track the progress of software development.

1 Software Metrics

1.1 maintainability index

Represents the maintainability of the source code. The higher the value, the better the maintainability. The value is between 0 and 100. A green rating of 20 to 100 indicates that the code is highly maintainable; a yellow rating of 10-19 indicates that the code is moderately maintainable; and a red rating of 0-9 indicates low maintainability.

1.2 cycle complexity

It is created by calculating the number of different code paths in the program flow to represent the complexity of a program. The program with complex control flow needs more tests to obtain good code coverage and is not easy to maintain.

In the following two cases, 1 is added to the calculation of cycle complexity:

Branch (if, while, do)

Case statement in switch

For example:

//Cycle complexity 1
 public void Method()
 {
 Console.WriteLine("Hello World!");
 }
 //Cycle complexity 3
 public void Method(bool condition1, bool condition2)
 {
 if (condition1 || condition2)
 {
  Console.WriteLine("Hello World!");
 }
 }
 //Cycle complexity 8
 public void Method(DayOfWeek day)
 {
 switch (day)
 {
  case DayOfWeek.Monday:
  Console.WriteLine("Today is Monday!");
  break;
  case DayOfWeek.Tuesday:
  Console.WriteLine("Today is Tuesday!");
  break;
  case DayOfWeek.Wednesday:
  Console.WriteLine("Today is Wednesday!");
  break;
  case DayOfWeek.Thursday:
  Console.WriteLine("Today is Thursday!");
  break;
  case DayOfWeek.Friday:
  Console.WriteLine("Today is Friday!");
  break;
  case DayOfWeek.Saturday:
  Console.WriteLine("Today is Saturday!");
  break;
  case DayOfWeek.Sunday:
  Console.WriteLine("Today is Sunday!");
  break;
 }
 }

1.3 inheritance depth

To calculate the inheritance depth in the inheritance hierarchy, the deeper the hierarchy in the inheritance tree, the more complex the development, testing, and maintenance of classes are. Because the deeper the hierarchy is, the more difficult it is to understand the definition and / or redefinition of specific methods and fields.

1.4 coupling degree

Class coupling is a measure of how one class connects or depends on another. It is good practice to always implement fewer dependent classes or low-level coupling.

1.5 lines of code

Represents the number of lines of code, and the count is based on IL code, so it is not the exact number of lines in the source code file. A very high count may indicate that a type or method is trying to do too much work and should be separated. It may also indicate that the type or method may be difficult to maintain.

2 calculate code metrics

Step 01 run calculate code metrics

Step 02 view code metrics results

Step 03 using code measure results

Screening:

Export:

Column management:

Calculate Code Metrics:

summary

The above is what Xiaobian introduced to you, through the visual studio code metrics to improve the code quality, I hope to help you, if you have any questions, please leave me a message, the editor will reply you in time. Thank you very much for your support to the developeppaer website!