good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
NOTE: This has been cross posted on the sw-design mailing list as well. So I was thinking about this Cylcomatic Complexity thing, and it occured to me that B::Concise actually gives up some of the necessary information (at least I think it does). Given this code: I got this output: Now I freely admit that both my understanding of Cylcomatic Complexity and B::Concise's output are most likely radically flawed, but I am going to ramble a bit here anyway. Using this definition for CC (Cylcomatic Complexity) Cyclomatic complexity may be considered a broad measure of soundness and confidence for a program. Introduced by Thomas McCabe in 1976, it measures the number of linearly-independent paths through a program module.And with this interpretation of the B::Concise output: (opcode-sequence-number) <0> (opcode_name) -> (next-opcode-sequence-number)It seems to me that our code could be viewed as the following graph: 1 -> 2 2 -> 3 3 -> 4 4 -> 5 5 -> 6 6 -> (end)Simple right? Since there is only one "linearly-independent path" the CC-metric for this might be 1. Now let's take a look at a slightly more complex bit of code: And here is the B::Concise output: Now here is the 2 graphs for this: 1 -> 2 2 -> 3 3 -> 4 4 -> 5 5 -> 6 6 -> 7 (<<< conditional here) 7 -> 8 8 -> 9 9 -> (end)and: 1 -> 2 2 -> 3 3 -> 4 4 -> 5 5 -> 6 6 -> b (<<< conditional here) b -> c c -> d d -> e e -> f f -> g g -> (end)We now have two "linearly-independent path" the CC-metric for this might be 2. It seems to get a little tricky when we have subroutines, here is some code: And here is the B::Concise output (notice we need to pass the sub-names to get them to output): If we look at the "main program:" label as a starting place: h -> i i -> j j -> k k -> l l -> *start ( 1 -> 2 2 -> 3 3 -> 4 4 -> *test ( c -> d d -> e e -> f f -> g (return) ) -> 5 6 -> 7 7 -> 8 8 -> 9 9 -> a a -> b (return) ) -> m m -> n n -> o 0 -> (end)We now only have one "linearly-independent path" so the CC-metric for this would be 1. Now of course there are some issues with this. To start with B::Concise's sequence numbers are in base 36 and values greater than 62 are not supported according to the docs. However, if you use the B::Concise functional interface, it seems that is might be possible to get around this restriction by using the perl opcode hex addresses instead of the sequence numbers. However now I am getting into needing to really write some code, and I am currently out of time and need to get back to "real" work. But anyway I am interested in comments from the group.
-stvn
In reply to Re: Cyclomatic Complexity of Perl code
by stvn
|
|