File Coverage

blib/lib/Perl/Critic/Policy/Subroutines/ProhibitExcessComplexity.pm
Criterion Covered Total %
statement 31 32 96.8
branch 3 4 75.0
condition n/a
subroutine 12 12 100.0
pod 4 5 80.0
total 50 53 94.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity;
2              
3 40     40   26421 use 5.010001;
  40         156  
4 40     40   239 use strict;
  40         90  
  40         877  
5 40     40   194 use warnings;
  40         89  
  40         1008  
6 40     40   222 use Readonly;
  40         89  
  40         2288  
7              
8 40     40   279 use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
  40         109  
  40         2102  
9 40     40   33352 use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_sub };
  40         131  
  40         870  
10              
11 40     40   2561 use parent 'Perl::Critic::Policy';
  40         110  
  40         213  
12              
13             our $VERSION = '1.146';
14              
15             #-----------------------------------------------------------------------------
16              
17             Readonly::Scalar my $EXPL => q{Consider refactoring};
18              
19             #-----------------------------------------------------------------------------
20              
21             sub supported_parameters {
22             return (
23             {
24 93     93 0 2123 name => 'max_mccabe',
25             description => 'The maximum complexity score allowed.',
26             default_string => '20',
27             behavior => 'integer',
28             integer_minimum => 1,
29             },
30             );
31             }
32              
33 75     75 1 326 sub default_severity { return $SEVERITY_MEDIUM }
34 74     74 1 358 sub default_themes { return qw(core complexity maintenance) }
35 33     33 1 117 sub applies_to { return 'PPI::Statement::Sub' }
36              
37             #-----------------------------------------------------------------------------
38              
39             sub violates {
40 8     8 1 27 my ( $self, $elem, undef ) = @_;
41              
42 8         41 my $score = calculate_mccabe_of_sub( $elem );
43              
44             # Is it too complex?
45 8 100       63 return if $score <= $self->{_max_mccabe};
46              
47 1         4 my $desc;
48 1 50       7 if ( my $name = $elem->name() ) {
49 1         81 $desc = qq<Subroutine "$name" with high complexity score ($score)>;
50             }
51             else {
52 0         0 $desc = qq<Anonymous subroutine with high complexity score ($score)>;
53             }
54              
55 1         12 return $self->violation( $desc, $EXPL, $elem );
56             }
57              
58              
59             1;
60              
61             __END__
62              
63             #-----------------------------------------------------------------------------
64              
65             =pod
66              
67             =for stopwords McCabe
68              
69             =head1 NAME
70              
71             Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity - Minimize complexity by factoring code into smaller subroutines.
72              
73             =head1 AFFILIATION
74              
75             This Policy is part of the core L<Perl::Critic|Perl::Critic>
76             distribution.
77              
78              
79             =head1 DESCRIPTION
80              
81             All else being equal, complicated code is more error-prone and more
82             expensive to maintain than simpler code. The first step towards
83             managing complexity is to establish formal complexity metrics. One
84             such metric is the McCabe score, which describes the number of
85             possible paths through a subroutine. This Policy approximates the
86             McCabe score by summing the number of conditional statements and
87             operators within a subroutine. Research has shown that a McCabe score
88             higher than 20 is a sign of high-risk, potentially untestable code.
89             See L<http://en.wikipedia.org/wiki/Cyclomatic_complexity>
90             for some discussion about the McCabe number and other complexity
91             metrics.
92              
93             The usual prescription for reducing complexity is to refactor code
94             into smaller subroutines. Mark Dominus book "Higher Order Perl" also
95             describes callbacks, recursion, memoization, iterators, and other
96             techniques that help create simple and extensible Perl code.
97              
98             =head1 CONFIGURATION
99              
100             The maximum acceptable McCabe can be set with the C<max_mccabe>
101             configuration item. Any subroutine with a McCabe score higher than
102             this number will generate a policy violation. The default is 20. An
103             example section for a F<.perlcriticrc>:
104              
105             [Subroutines::ProhibitExcessComplexity]
106             max_mccabe = 30
107              
108             =head1 NOTES
109              
110              
111             "Everything should be made as simple as possible, but no simpler."
112              
113             -- Albert Einstein
114              
115              
116             Complexity is subjective, but formal complexity metrics are still
117             incredibly valuable. Every problem has an inherent level of
118             complexity, so it is not necessarily optimal to minimize the McCabe
119             number. So don't get offended if your code triggers this Policy.
120             Just consider if there B<might> be a simpler way to get the job done.
121              
122              
123             =head1 AUTHOR
124              
125             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
126              
127             =head1 COPYRIGHT
128              
129             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
130              
131             This program is free software; you can redistribute it and/or modify
132             it under the same terms as Perl itself. The full text of this license
133             can be found in the LICENSE file included with this module.
134              
135             =cut
136              
137             # Local Variables:
138             # mode: cperl
139             # cperl-indent-level: 4
140             # fill-column: 78
141             # indent-tabs-mode: nil
142             # c-indentation-style: bsd
143             # End:
144             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :