This article is all about performance improvement and measured code complexity and best practices. You have started to read and then I guess you are a good developer but never measure the performance and best practices of your piece of code. Fine, this is a place to start.
This article is targeted to beginners in this field (haha, not in .NET but performance measurement). I am using Visual Studio 2012 for this example. So, if you are using a different Visual Studio then the output screen look and view may differ for you.
Ok, so let's start with a little theory. What is the code matrices and Cyclomatic Complexity? Why are they important to be measured in an application?
Let me answer from my personal experience. I am working for a software company and on the first day of project discussion, in a meeting our manager said that we need to follow the company's naming convention of best practices and need to measure the code performance using including code coverage and Cyclomatic complexity.
Ok, so this is the scenario in most software organizations, but why? There are many noble causes for that; first of all , the client may provide the criteria at the time of product development or if it is their own product, they want to implement best practices and want to follow proper development methodology to improve maintainability and performances of the applications.
The term maintainability has great impact in the industry and people only know those who work to enhance some features of an existing application or bug fix.
So, those are the reasons for why a project manager or tech lead person always directs a junior developer in the right direction.
Fine, so let's understand Cyclomatric complexity. Cyclomatic complexity measures the amount of decision logic in a single software module. Now the module represents a unit of code; it may be a single function, one class or a set of classes within a certain namespace or an entire application.
It is used for two related purposes in the structured testing methodology. First, it gives the number of recommended tests for software. Second, it is used during all phases of the software lifecycle, beginning with design, to keep software reliable, testable and manageable. Cyclomatic complexity is based entirely on the structure of software's control flow graph.
And it's always a best practice to reduce the value of Cyclomatric complexity for obvious reasons. Just for example, a mid-sized function (let's assume 20 lines of code) may contain 5/10 as the Cyclomatric complexity value.
A function with Cyclometric complexity more than 30/40 is considered to be very risky and not easily maintainable.
So, our ultimate goal will be to always reduce Cyclomatric complexity.
Formulae to calculate Cyclomatric complexity
Cyclomatic complexity is defined for each module to be e - n + 2, where e and n are the number of edges and nodes in the control flow graph, respectively.
Ok, enough theory, let's try to measure cyclomatric complexity of a function in Visual Studio. Just use one console application and add the following code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace ConsoleApp
{
class testComplex
{
public int a{get;set;}
public int b{get;set;}
public int Div(int tmpa, int tmpb)
{
return tmpa / tmpb;
}
}
class Program
{
static void Main(string[] args)
{
testComplex objt = new testComplex();
objt.Div(100, 10);
}
}
}
The code is very simple, we have just added one class that contains one Div() function and that returns an integer value as a result. To make it simple, we are not even printing the result, haha.. not necessary.
Fine, now we are interested in measuring the Cyclomatric complexity of the Div() function. Just right-click on your project and you will see the calculate code metrics option there. Here it is in the following screen.
Once you click on that, it will calculate code metrics of the entire project, have a look at the following screen.
Since we are not interested in looking at the entire application, we will find a specific function and we are seeing that the Cyclomatric Complexity value for the Div() function is 1. The reason is there is only one exit path. Now, let's make a little modification in the Div() function as in the following. We are just checking whether or not the value of tempb is zero. If not then it will return the result otherwise it will throw an exception.
So, now there are two exits of the function. It may satisfy either case.
And we are seeing that the Cyclomatric Complexity value is 2, since there are two exit paths.
Conclusion
It is always a best practice to measure cyclomatric complexity of four function to improve performances and best practices. Happy learning.