Visual Studio 2015: Code Metrics

visual studio 2015.png

Many of us programmers  have the mentality that it’s just okay to implement codes in anyway as long as it is running. Well to tell you the truth you should already change or move away from that kind of mindset. Software development has its own guidelines like abstraction, refinement, modularity and other same concepts that you can use to guide you during coding and avoid the so-called programming pasta. One tool I use in Visual Studio 2015 is Code Metrics. For today I will show you how to generate Code Metrics on your project. Then I plan to discuss deeper on the different code metrics in the next succeeding articles.

CODE METRICS

Code complexity and maintainability is a big issue to deal with software engineering, especially in large solutions consuming different libraries. This is where Code metrics come in, where in it is a software measure tool to give developers an easiest way to understand the code that they are developing. In that way developers can review their codes that need to be fixed or refactored. Code Metrics provided by visual studio concentrate on five different parts (Lines of Code, Class Coupling, Depth of Inheritance, Cyclomatic Complexity and Maintainability Index).

START CALCULATING CODES IN VISUAL STUDIO

We will just re-use one of our previous sample application used in C#: Processing Json Data and use it to generate our code metrics.

There are two ways to calculate code metrics, First is through the Solution Explorer window, Right click on the Project solution, then move your mouse to the Analyze window as shown below:

JsonSerializerDeserializerCodeMetrics.PNG

Another way is through the Menu bars under the Analyze Menu, just click the Calculate Code Metrics and select on where project or the whole solution you want to generate the code metrics result.

BuildRunCodeAnalysis.PNG

Then after we have Calculated our code metrics, It will build your current project, then wait for the Code Metrics window to appear as shown below:

CodeMetricsWindows.PNG

As you can see the Code Metrics result was broken down into six columns, namely Hierarchy, Maintainability Index, Cyclomatic Complexity, Depth of Inheritance, Class Coupling and Lines of Code. Let me explain briefly each section.

Hierarchy

Hierarchy contains the breakdown of your project so in our case we have the two models (CompanyInformation and Section) then the Program part of our application. Each section of application has their own report about the generated code metrics.

Maintainability.PNG

Maintainability Index

Maintainability Index is just a rating between 0 – 100 wherein if it falls between 20-100 (Green Color) indicates that the code has a better maintainability, range between 10-19 (Yellow Color) indicates that the code is moderately maintainable and the range between 0-9 (Red Color) which indicates poor maintainability and requires to refactor your code.

Cyclomatic Complexity

Cyclomatic Complexity generates a report about how complex the structure of your code. It is calculated based on the different number of paths that a program can flow. The more higher the rating of your Cyclomatic Complexity the more complex your code structure is.

Depth Of Inheritance

Depth of Inheritance is just the position of the class based on the Inheritance hierarchy. So the more the value is larger the more your codes difficult to understand because of complex Inheritance design.

Class Coupling

Class Coupling is just the measure on how one class is related or dependent with one another.The higher the value the more it is difficult to reuse and maintain because of their dependencies with other classes. It is recommended that the design of classes should be high cohesion and low coupling.

Lines of Codes

Lines of Code is just simply the count of your codes per line. In general, we should write methods in fewer possible line. If the Lines of Code are high for a method then we should consider checking it and refactoring. Another indicator of high Lines of Code is the violation of The Single Responsibility principle where in the Method should have only one responsibility which keeps it more reusable, more clean and easy to understand.

Conclusion

So we have just discussed on how to use the code metrics tool in visual studio 2015 then also we have briefly tackled the different code metrics generated by the said tool. In the next part of this article I will tackle more on Lines of Code.

Happy Coding 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s