| 1 |
package Analizo::Metric::AverageCycloComplexity; |
| 2 |
use strict; |
| 3 |
use parent qw(Class::Accessor::Fast Analizo::ModuleMetric); |
| 4 |
use Statistics::Descriptive; |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
Analizo::Metric::AverageCycloComplexity - Average Cyclomatic Complexity per Method (ACCM) metric |
| 9 |
|
| 10 |
=head1 DESCRIPTION |
| 11 |
|
| 12 |
The metric calculation is based on the following article and calculates the |
| 13 |
cyclomatic complexity of the program. |
| 14 |
|
| 15 |
Article: |
| 16 |
I<McCabe, Thomas J. "A complexity measure." IEEE Transactions on software Engineering 4 (1976): 308-320>. |
| 17 |
|
| 18 |
The Average Cyclomatic Complexity per Method is calculated counting the |
| 19 |
predicates (i.e., decision points, or conditional paths) on each method plus |
| 20 |
one, then a mean of all methods is returned as the final value of ACCM. |
| 21 |
|
| 22 |
The cyclomatic complexity of a program represented as a graph can be calculated |
| 23 |
using a formula of graph theory: |
| 24 |
|
| 25 |
v(G) = e - n + 2 |
| 26 |
|
| 27 |
Where C<e> is the number of edges and C<n> is the number of nodes of the graph. |
| 28 |
|
| 29 |
Another good reference is: |
| 30 |
I<Woodward, Martin R., Michael A. Hennell, and David Hedley. "A measure of control flow complexity in program text." IEEE Transactions on Software Engineering 1 (1979): 45-50>. |
| 31 |
|
| 32 |
=cut |
| 33 |
|
| 34 |
__PACKAGE__->mk_accessors(qw( model )); |
| 35 |
|
| 36 |
sub new { |
| 37 |
my ($package, %args) = @_; |
| 38 |
my @instance_variables = ( |
| 39 |
model => $args{model} |
| 40 |
); |
| 41 |
return bless { @instance_variables }, $package; |
| 42 |
} |
| 43 |
|
| 44 |
sub description { |
| 45 |
return 'Average Cyclomatic Complexity per Method'; |
| 46 |
} |
| 47 |
|
| 48 |
sub calculate { |
| 49 |
my ($self, $module) = @_; |
| 50 |
|
| 51 |
my @functions = $self->model->functions($module); |
| 52 |
if (scalar(@functions) == 0) { |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
my $statisticalCalculator = Statistics::Descriptive::Full->new(); |
| 57 |
for my $function (@functions) { |
| 58 |
$statisticalCalculator->add_data($self->model->{conditional_paths}->{$function} + 1); |
| 59 |
} |
| 60 |
|
| 61 |
return $statisticalCalculator->mean(); |
| 62 |
} |
| 63 |
|
| 64 |
1; |