Code metrics….the good way to know whether my application is well designed and developed!

Hey!

Recently, during an audit for one of our clients, I used one of the great features embedded in Visual Studio Team System : code metrics! What’s that?!

As you have FxCop integrated in VSTS 2008 you have also a code metrics analyzer to highlight possible design problems for your project. These problems could cause performance issues and make your code more difficult to maintain.

What are code metrics?

Maintainability Index

Index from 0 to 100 used for each solution part (project, class, namespace…) to identify how easy your code will be to maintain. More the index is closed by 0 and more problems you will have to maintain it.

Cyclomatic complexity

It measures the structural complexity of your code. Then, if you have a lot of conditional structures such as if/else or switch or iterations with foreach, while….it means you have much more access points in your code which is more complex to test, maintain and understand!

Depth of Inheritance

This index indicates how many levels of inheritance you have from the root. More levels you have, more complex it will be to identify code dependency.

Class coupling

Measures mainly your code design. If your code has a high coupling it will be very hard to make it extensible and above all to reuse it.

Lines of code

In theory, a class should not have more than 1000 lines of code and a class not more than 100. If it’s not the case, you have to review your code design, you have probably to split your code.

For information, it uses MSIL code so the numbers don’t take in account comments, spaces, lines breaks….

How to use code metrics?

Visual Studio Team System integrates natively this feature. Very simple to use, you have just to do the following steps:

in your solution explorer, right click on the solution name and select “Calculate Code Metrics”.

image

A new window will appear to present results. They are ordered by hierarchy (project, namespace, class…). Then you can have a global view of your solution but also detailed results at a function/method level.

image

So feel free to use it to have interesting measures about your solution design!

More info? https://msdn.microsoft.com/en-us/library/bb385910.aspx

Vincent

Comments