Cyclomatic Complexity

18 April 2016 by Sonali Sorte No comments

Hi all, you may have come across a concept called Cyclomatic complexity while preparing for ISTQB. You may also have read it as a concept under the White box Testing, it’s because we actually need to refer the code of the feature/application for calculating Cyclomatic complexity.

Why Cyclomatic complexity:

1.Cyclomatic Complexity is a kind of structural measure.

2.What is a Structural measure: It means a measurement used to calculate the complexity and vastness of the code, effects of changes made in the code and testing efforts (including reviews)required, also to decide the technique and tool to be used for testing.

What is Cyclomatic complexity:

1.     Cyclomatic complexity is a number which tells us about the complexity of the code/feature.

2.    There are many ways to calculate CC (Cyclomatic Complexity). Some basic of them are as follows

a.    V(g)= E-N+2

V(g)= cyclomatic complexity factor

E= # of edges

N= # of nodes

2= these are termination points(i.e., START point and END point)

b.    V(g)= L+2

Where,

L = # of Loops in the flow diagram

*Loop: It executes a statement based on a given condition. Example: if-else, while,  for etc.

3.    So on calculating CC factor we will get a number, this number will further tell us more about the complexity, cost and efforts required for that feature.

4.    The number is then categorized to find the complexity and other parameters in the following way:

a.   If the number, n<10

Then the project is structured and low complex, since low complex, the testability is high. Also, the efforts required will be less, so the costing also will be less.

Example:
Login page,

{ 
#enter valid username/email id
#enter valid password
#hit login
//if any of the above parameter is invalid-throw an error message
}

In this example system will only check for the valid and invalid parameters and will either permit the user (for valid cases) or display an error message (for invalid cases). Hence, Login page is highly testable as it has limited combinations of input & output. Efforts required are very less and the cost is also less.

b.  If number, 10<n<20

Then the project will have medium complexity hence the testability will also be medium. Efforts required will increase in this case also the cost of the project.

Example: Google Maps: If I try to search for Gateway of India-Mumbai in Gmaps then,

A.   System will first execute the country Loop and will give the output as Asian countries

B.    In Asian countries will search for India

C.    In India will then search for Maharashtra

D.   In Maharashtra will search for Mumbai

E.    In Mumbai will search for Gateway of India

F.    Now will pick my current location

G.    And will calculate and display the distance from my current location to Chinchwad

H.   Also will display the time required to reach there based on the mode of traveling chosen

These will be the loops while using the Gmap hence the code is a bit complex, also, a small change in any of the above loops will have cascading effects, hence the testing efforts also increased. Thus, the costing increases.

c.  If number, 20<n<40

Then the project complexity increases on a higher level by reducing the testability to a lower level. Efforts required and costing of such projects is high.

Example:Banking domain website

 With features like: transactions, payments, shopping points, loan related info, mobile banking, traveling, ticket booking, dining and home delivery of meals, Insurance buying, various investment options etc.

In this case, the efforts required will be more since there are many factors to be considered. Also, security and privacy will be a higher priority. Such projects involve a lot of mathematical calculations with minimum precision deviation. The cost of such projects is too high as security and privacy are targeted and consists of financial transactions.

d.  If number, 40<n

Then the project complexity is immense, hence, the possibilities of end to end testing are less. The efforts required and costing is also too high.

Example:For this case we can consider e-commerce an example. In e-commerce there are continuous release cycles, hence, an end to end testing for each release is less possible. Thus, this increases risk, efforts and costing of the project.

How to use Cyclomatic complexity?

Control flow diagram is always used for calculating the Cyclomatic complexity of any software. We will now go through a short example where we will be using the above concepts of cyclomatic complexity.

Example of CC:

We will take following simple example as client’s requirement :

Calling from one mobile phone to another. Client’s requirement are as follows:

1.     Country code of the dialed number to be displayed

2.    If any ISD number is dialed then display the country name

3.    Also check and display Service provider name and update in DB

4.    If a STD number is dialed apply roaming charges

5.    Call charges will be based on the type of service opted i.e. prepaid or postpaid

6.    If the call is made in night hours, apply night calling charges

7.    Call charges will depend upon the corporate or noncorporate service opted

8.    If the number is bound to any special plans, apply call charges as per the special plans

9.    If the call is answered, then check the answering mode i.e.answered by the user or answering machine.

10.  If the call is answered by an answering machine, then record a voice message after a beep and disconnect the call when the user disconnects.

11.  If the user disconnects the call. End the call.

Please refer below image for a flow chart of above-mentioned example.

Flow chart

 

By using the first formula of CC:

V(g)= E-N+2

= 37- 27+2

= 12

Now we’ll calculate the CC by using the second formula for the same flow chart:

V(g)= L+2

= 9+2

= 11

Since the CC factor obtained is 12 by using formula 1 and by using the second formula we get the CC factor as 11 since both of them fall in the 2nd category, the code is medium complex, required testing efforts are also medium, thus resulting in a medium costing project.

I hope you found my blog to be helpful.

Author: Sonali Sorate
Email : sonali@idyllic-software.com
Linkedin: https://in.linkedin.com/in/sonal-sorte-06757217

Sonali Sorte

Follow me on Twitter

Leave a Reply

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

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home

Contact

Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)