views:

140

answers:

3

Hi all,

How to find the cyclomatic complexity of a function with multiple exit points? The wiki page says p-s+2 where p is the number of decision points and s is the number of exit points.

But should not more exit points increase the cyclomatic complexity as it may lead to more independent paths?

Cheers,

Aman

+1  A: 

Why not try a trial of NDepend? It will calculate cyclomatic complexity and many other code metrics.

Richard Ev
+2  A: 

CC measures linearly independent paths. Exit points don't ADD paths to the code, they TERMINATE paths, thus reducing CC (or in the very least, they certainly don't increase CC).

To put it another way, the ONLY way to add exit points is to ADD more paths (conditions like IF). Otherwise, the code after the 'naked' exit point is unreachable, so it is the conditionals that add complexity, not exit points.

Michael Bray
A: 

Thanks michael. I had realised my mistake after i posted the question. My mistake stemmed from the observation that JavaNcss(which uses the source file) and Xdepend (which uses the jar file) both seemed to overestimate the CC for a piece of code which had multiple exit points. I have posted the code here . But using the formula p-s+2 the answer seems to be 4. Is there some simple explanation i am missing ?

@Richard: I have tried Xdepend( The non .NET version of Ndepend). It looks a good tool. But when it uses jar files, it overestimates the CC ( which they have accepted in their documentation). At this stage i am exploring different tools. Are you aware of any better one?

Cheers.

Aman Neelappa