Cyclomatic complexity

T.Oumaima
2 min readFeb 6, 2021

If you are a software engineer, or a computer science student, you might have already heard about cyclomatic complexity concept. It is mainly an interesting metric that helps software developers or designers to measure the quality of their code and detect easily any potential unwanted maintenance cost due to the existence of some bad written code.

Cyclomatic complexity has a variance of definitions.Thomas J.McCabe describes it as the number of lineary independant path through a program and it can be computed according to him based on a control flow graph with the following formula:

E — N + 2 where :
E stands for the edges of the graph
N stands for the number nodes in the graph

A node stands also for the basic block of the program and the edge is the connection between two basic locks. more details can be found in https://en.wikipedia.org/wiki/Cyclomatic_complexity.

A simpler interpretation of cyclomatic complexity definition J.McCabe gives could be the number of decision points in the program plus one, or in an other word : number of if and ifelse, the loops with conditions and cases in a switch plus one.

Example:
if (condition1 and condition2) //+2
statment1;
else if(condition3)//+1
statment2;
for(intialization;condition;counter++)//+1
statment3;

The cyclomatic complexity of this pseudo program is 5 since the number condition points are 4 then plus one is 5.

Cyclomatic complexity is a very strong concept that reveals the complexity of a program and also that helps developers to find easly the spaguitti code. It also helps testers to find out the minimum number of test cases that needs to presents in the unit tests. J.McCabe concider a program to be very complexe when CC is equal to 10.

--

--

T.Oumaima

My blog is a balance between my love for technology, poetry and books.