NCover Code Quality Metrics

Code Coverage Analysis is very crucial to writing better code and improving quality of software applications.Code Coverage Analysis Tools helps us in automated analysis of code and pinpoints the untested lines of code or areas which has become extremely complex and prone to error/performance bottlenecks.In this post I will discuss about NCover – a leading .NET Code Coverage Tool.

NCover tracks the following code metrices:

  • Symbol Coverage – Symbols Coverage measures how many sequence points are executed within the code.Sequence points are where a VS Studio Debugger highlights during step debugging a code.This avoids the chance of any partial execution but there can be single statement which can be executed multiple times e.g. a for loop.To test the results I have used the following block of code:
class Program
{
static void Main(string[] args)
{
Test(false);

Console.Read();
}
static void Test(bool x)
{

if (x)
{
Console.WriteLine("1");
}
else
{
Console.WriteLine("2");
}

}
}

Here NCover gives me the following output:

It shows the maximum visit count against each line i.e the maximum number of times the line can be executed.

The Symbol Coverage of the method Test is (3/4) i.e 75% .Here we have 4 sequence points marked as 1,2,3,4 in the figure above in blue.But in this code the only three sequence points can be possibly executed as the condition if(x) will always evaluate to false i.e the statement highlighted in red will be never executed.

  • Branch Coverage – Branch Coverage takes into consideration the number of basic blocks executed in the control flow graph.Branch Coverage provides a little higher level view than symbol coverage as it takes into consideration the if blocks which were evaluated to true as well as false.However the branch coverage statistics is generated based on IL opcodes and might not match with what we understand from source code.For the method Test we have 2 basic blocks in the Control Flow Graph and Branch Coverage is 100%.
  • Cyclomatic Complexity – This is the number of independent path from start to end of execution.For the method Test cyclomatic complexity will be 3
  • Method Coverage – This is a module level metrics and is the measure of actual number of methods executed vs actual number of methods in the module.In this it should be 100% but NCover shows 66.67% as it considers the constructor of Program class as one unused method.
This entry was posted in Framework & Libraries. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *