| 1 |
package Analizo::Metric::LackOfCohesionOfMethods; |
| 2 |
use strict; |
| 3 |
use parent qw(Class::Accessor::Fast Analizo::ModuleMetric); |
| 4 |
use Graph; |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
Analizo::Metric::LackOfCohesionOfMethods - Lack of Cohesion of Methods (LCOM4) metric |
| 9 |
|
| 10 |
=head1 DESCRIPTION |
| 11 |
|
| 12 |
The metric calculation is based on the following article and calculates the |
| 13 |
cohesion of the class. |
| 14 |
|
| 15 |
Article: |
| 16 |
|
| 17 |
M. Hitz and B. Montazeri, "Measuring coupling and cohesion in |
| 18 |
object-oriented systems," in Proceedings of the International. Symposium on |
| 19 |
Applied Corporate Computing, 1995. |
| 20 |
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.409.4862 |
| 21 |
|
| 22 |
The LCOM4 value for a module is the number of connected components of an |
| 23 |
undirected graph, where the nodes are the module's subroutines (methods, |
| 24 |
functions etc.), and the edges indicate that two subroutines use at least one |
| 25 |
attribute/variable in common, or that one subroutines calls the other. These |
| 26 |
connected components represent independent parts of a module, and modules |
| 27 |
that have more than one of them have independent, distinct responsibilities. |
| 28 |
|
| 29 |
You can see a study using the LCOM4 metric on the following paper: |
| 30 |
|
| 31 |
Terceiro, Antonio, et al. "Understanding structural complexity evolution: A |
| 32 |
quantitative analysis." Software Maintenance and Reengineering (CSMR), 2012 |
| 33 |
16th European Conference on. IEEE, 2012. NBR 6023 |
| 34 |
http://ieeexplore.ieee.org/abstract/document/6178856/ |
| 35 |
|
| 36 |
For a comparative study on the various LCOM1, LCOM2, LCOM3, LCOM4 and LCOM5 |
| 37 |
see: |
| 38 |
|
| 39 |
Kaur, Taranjeet, and Rupinder Kaur. "Comparison of various lacks of Cohesion |
| 40 |
Metrics." International Journal of Computer Trends and Technology (IJCTT) |
| 41 |
4.5 (2013). NBR 6023 |
| 42 |
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.686.2543 |
| 43 |
|
| 44 |
=cut |
| 45 |
|
| 46 |
__PACKAGE__->mk_accessors(qw( model graph )); |
| 47 |
|
| 48 |
sub new { |
| 49 |
my ($package, %args) = @_; |
| 50 |
my @instance_variables = ( |
| 51 |
model => $args{model}, |
| 52 |
graph => Graph->new, |
| 53 |
); |
| 54 |
return bless { @instance_variables }, $package; |
| 55 |
} |
| 56 |
|
| 57 |
sub description { |
| 58 |
return "Lack of Cohesion of Methods"; |
| 59 |
} |
| 60 |
|
| 61 |
sub calculate { |
| 62 |
my ($self, $module) = @_; |
| 63 |
my $graph = Graph->new; |
| 64 |
my @functions = $self->model->functions($module); |
| 65 |
my @variables = $self->model->variables($module); |
| 66 |
for my $function (@functions) { |
| 67 |
$graph->add_vertex($function); |
| 68 |
for my $used (keys(%{$self->model->calls->{$function}})) { |
| 69 |
# only include in the graph functions and variables that are inside the module. |
| 70 |
if ((grep { $_ eq $used } @functions) || (grep { $_ eq $used } @variables)) { |
| 71 |
$graph->add_edge($function, $used); |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
my @components = $graph->weakly_connected_components; |
| 76 |
return scalar @components; |
| 77 |
} |
| 78 |
|
| 79 |
1; |
| 80 |
|