Thanks:  0
Likes:  0
Dislikes:  0

1. ## cyclomatic complexity

Wanted to know that how would we calculate cyclomatic compelxity of a source code with for loops.. Take below source code as an exp.

for (i=0;i&lt;=5;i++)
statement

2. ## Re: cyclomatic complexity

This is how I calculate cyclomatic complexity.

If = + 1
Else = +1
Else If = +1
For = +x (x being the number of iterations)
While = +y (y being a high average number of expected iterations)
Case = +1

Then I add +1 at the end for a baseline scenario.

So in your statement above the cyclomatic complexity would be 7 (0-5 = 6 +1)

3. ## Re: cyclomatic complexity

Brent,

That doesn't sound right!!!

Where is the IF condition? I make it 3.

A cyclomatic compelxity of 5-10 is considered fairly complex. That isn't!

4. ## Re: cyclomatic complexity

I don't know why an application with a cyclomatic complexity of 5-10 would be fairly complex. Let's make sure we're on the same page. When I refer to cyclomatic complexity I am referring to the total number of independent paths through an application.

So that means we start with 1 to begin, if there are no conditional statements. This is as simple an application as we can make. It will always behave the same way.

If we add an IF conditional, then we now have a case where that conditional can either be TRUE or FALSE. So we now have two paths, hence the +1.

If we are talking about a statement like "For i = 0 to 5", we are now adding 6 more paths to the application (where i could either be 0, 1, 2, 3, 4, or 5).

On top of that, if we were to nest an IF statement within a For statement then we need to add x squared for that For statement (it's not simply a matter of adding +1 in that case), since for each iteration there could true or false here.

My apologies to the poster, too, because I listed Else as a +1, butthat should have been Else If to begin with, but I didn't notice I added it to a separate line. Else doesn't could as anything, since it is the contrary to the IF condidtion, so if the IF condition is not met, the Else will always be hit, unless it doesn't exist. However, that still means that the application will always perform the same when the path is FALSE for that conditional statement.

Why 3 for an If statement TurboGrafx? Maybe I'm missing something myself. Or was it with regards to that Else statement?

5. ## Re: cyclomatic complexity

I guess we would have to ask Mr MaCabe why it is fairly complex!!

I agree with what your saying, except :

1. I dont see an IF condition in the code, it's just a loop
2. I consider a loop to be 2 paths. Each value is taking the same path through the code.

6. ## Re: cyclomatic complexity

[ QUOTE ]
2. I consider a loop to be 2 paths. Each value is taking the same path through the code.

[/ QUOTE ]

Yeah, I would agree with this. I actually used to do it this way myself. I also think this is how it is outlined by McCabe himself, because you're touching on the independent paths through the application. So it would be any the number of loops in a given application + 1.

I include iterative data within my cyclomatic complexity for other reasons, but at the base, you're right, you do only need to count any loop, regardless of what kind of loop it is.

7. ## Re: cyclomatic complexity

So, Brent, you aren't describing McCabe Cyclomatic Complexity. You have redefined into Paine Cylclomatic Compexity.....

8. ## Re: cyclomatic complexity

LOL!!! As a manner of speaking, I guess so, yes.

Actually, what's funny is that when I originally posted I actually believed this is how it was, completely forgetting that back a few years now I just switched this up to show the implied complexity or the total number of lines executed.

Then, I guess the lines blurred somewhere along the line until TG contested it and I thought back real hard.

Maybe I should create my own theory, you know, since I already redefined someone elses, lol.

9. ## Re: cyclomatic complexity

Should not complexity levels be stated as WTFs?

10. ## Re: cyclomatic complexity

Yes, but how can you create a valid metric for WTFs?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.