Debian Codegraph @

lib/Analizo/GlobalMetric/ChangeCost.pm

raw
1 package Analizo::GlobalMetric::ChangeCost;
2 use strict;
3 use parent qw(Class::Accessor::Fast);
4 use List::Util qw( sum );
5 use Graph::TransitiveClosure::Matrix;
6
7 =head1 NAME
8
9 Analizo::GlobalMetric::ChangeCost - Change Cost global metric
10
11 =head1 DESCRIPTION
12
13 The metric calculation is based on the following article and calculates the
14 degree to which a change to any file causes a (potential) change to other files
15 in the system.
16
17 Article: Exploring the Structure of Complex Software Designs: An Empirical
18 Study of Open Source and Proprietary Code by Alan MacCormack, John Rusnak and
19 Carliss Baldwin.
20
21 See the paragraph about Change Cost in the article:
22
23 "... characterize the structure of a design is by measuring the degree of
24 'coupling' it exhibits, as captured by the degree to which a change to any
25 single element causes a (potential) change to other elements in the system,
26 either directly or indirectly (i.e., through a chain of dependencies that exist
27 across elements).
28
29 ... measures the percentage of elements affected, on average, when a change is
30 made to one element in the system."
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 "Change Cost";
46 }
47
48 sub calculate {
49 my ($self) = @_;
50 my $reachability_matrix = $self->model->files_graph->transitive_closure_matrix;
51 my @vertices = sort $reachability_matrix->vertices;
52 my $rows = scalar @vertices;
53 return unless $rows;
54 my @fan_out = (0) x $rows;
55 my $n = 0;
56 foreach my $i (@vertices) {
57 foreach my $j (@vertices) {
58 $fan_out[$n] += $reachability_matrix->[0]->get($i, $j);
59 }
60 $fan_out[$n] = $fan_out[$n] / $rows;
61 $n++;
62 }
63 sprintf("%0.02f", sum(@fan_out) / $rows);
64 }
65
66 1;