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;}




Anupam SinghPosted Apr 22, 2014, 3:30 AM
vary nice informative article.. thanx for sharing buddy..