blib/lib/Perl/Critic/Policy/Subroutines/ProhibitExcessComplexity.pm

filter: Statementstmt 85.7% 30 / 35 Branchbran 25.0% 1 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 83.9% 47 / 56 CC 3 SCAR 6.9
CC
3
Cov
79%
violates
5.7
BEGIN
1.0
BEGIN
1.0
CRAP 2.0
« blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedOpen.pm ↑ Summary blib/lib/Perl/Critic/Policy/Miscellanea/ProhibitFormats.pm »
1 package Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity;
2  
3 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 40 ▶use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
Subroutine BEGIN: called CC=1 SCAR=0.0
9 40 ▶use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_sub };
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 40 ▶use parent 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 40 our $VERSION = '1.150';
14  
15 #-----------------------------------------------------------------------------
16  
17 40 Readonly::Scalar my $EXPL => q{Consider refactoring};
18  
19 #-----------------------------------------------------------------------------
20  
21 sub supported_parameters {
22 return (
23 {
24 91 ▶ name => 'max_mccabe',
Subroutine supported_parameters: called CC=1 SCAR=0.0
Pod undocumented
25 description => 'The maximum complexity score allowed.',
26 default_string => '20',
27 behavior => 'integer',
28 integer_minimum => 1,
29 },
30 );
31 }
32  
33 75 ▶sub default_severity { return $SEVERITY_MEDIUM }
Subroutine default_severity: called CC=1 SCAR=0.0
Pod documented
34 74 ▶sub default_themes { return qw(core complexity maintenance) }
Subroutine default_themes: called CC=1 SCAR=0.0
Pod documented
35 31 ▶sub applies_to { return 'PPI::Statement::Sub' }
Subroutine applies_to: called CC=1 SCAR=0.0
Pod documented
36  
37 #-----------------------------------------------------------------------------
38  
39 sub violates {
40 5 ▶ my ( $self, $elem, undef ) = @_;
Subroutine violates: called CC=3 SCAR=17.3
Pod documented
41  
42 5 my $score = calculate_mccabe_of_sub( $elem );
43  
44 # Is it too complex?
45 5 ▶ return if $score <= $self->{_max_mccabe};
Branch: 1 branch
BranchTrueFalseTotal
if $score <= $self->{"_max_mccabe"} 5 0 5
46  
47 0 my $desc;
48 0 ▶ if ( my $name = $elem->name() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $elem->name) { } 0 0 0
49 0 $desc = qq<Subroutine "$name" with high complexity score ($score)>;
50 }
51 else {
52 0 $desc = qq<Anonymous subroutine with high complexity score ($score)>;
53 }
54  
55 0 return $self->violation( $desc, $EXPL, $elem );
56 }
57  
58  
59 40 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 :
« blib/lib/Perl/Critic/Policy/InputOutput/RequireCheckedOpen.pm ↑ Summary blib/lib/Perl/Critic/Policy/Miscellanea/ProhibitFormats.pm »