Friday, 19 April 2013

Visual Studio Features: Code Analysis, Code Metrics, and Code Clones

In this post I am going to introduce 3 Visual Studio features that are very interesting and helpful; but, alas, may not be that popular. I came to discover them recently and I instantly fell in love with them! I am sure you will love them too after you get to know and try them out. 

They are, namely: Code Analysis, Code Metrics, and Code Clones.

Code Analysis

The Code Analysis feature of Visual Studio examines the code by performing static code analysis to detect common pitfalls, defects, and violations of good programming practices. Thus helping developers identify potential design, globalization, performance, interoperability, and other issues in their code.   

The tool works to raise warnings that differ from compile errors and warnings; because the tool mainly searches for code patterns that are “logically” valid, though may still cause problems on the long run for you or for people who may check/use your code later.

Code Analysis can be run manually on a snippet of code, or even setup to automatically run as part of a Team Build or check-in policy for TFS [Team Foundation Server].  

Learn more about Code Analysis, What's New in Visual Studio 2012, and how to run Code Analysis on your system here.    

Code Metrics

Another feature that I truly like in Visual Studio 2012 is Code Metrics. Code Metrics is a tool introduced within Visual Studio that measures the evolvability and maintainability of a software system by calculating five important metrics. This helps the development team identify potential risks, understand the current state of their project, and track progress during software development.

The five metrics are:
  • Maintainability Indexa value between 0 and 100 that represents the relative ease of maintaining the code. Higher values indicate better maintainability.
  • Cyclomatic Complexity: this metric is a measure of the structural complexity of the code; it calculates the number of different code paths in the flow of the program. The actual value of this metric differs from one Visual Studio version to another. But a rule of thumb is that no individual method or property should have a Cyclomatic Complexity that is greater than ten.
  • Depth of Inheritance: it indicates the number of class definitions that extend to the root of the class hierarchy. The deeper the hierarchy the more difficult it might be to maintain and understand the code. However, a high depth of inheritance can also indicate a greater level of code reuse. So it is difficult to judge what a good depth is.
  • Class Coupling: this one measures the coupling to unique classes through parameters, local variables, return types, method calls, and other types in the source code. High values of coupling limit the value of a type to be reused; so a lower value [lower than ten] is what you should aim for.
  • Lines of Code: a value indicating the approximate number of lines in the code. A very high number indicates that the method/type is trying to do too much and should be split up. It may also indicate that the type/method is hard to maintain. 

More about this important topic and how to generate Code Metrics in Visual Studio 2012 can be found here and here

Code Clones

A personal favorite! It is a new feature introduced in Visual Studio 2012 that detects ALL code fragments that are copied throughout the solution. The tool detects the separate fragments of code that are very similar. Usually, code clones make it hard to change your application because you have to look for each fragment to update. Now it is easier as Visual Studio helps you find these clones so that you can refactor or modify them. Or just get to know the amount of “potential” copy/paste used in the system you are working on! :)

More about this, here

1 comment:

  1. run code analysis provides complete information about error in the source code. the run code analysis automatically run with each build of a code project, and you can also run code analysis manually... good information shared ..

    ReplyDelete