Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / Subroutines / ProhibitExcessComplexity.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Subroutines/ProhibitExcessComplexity.pm $
3 #     $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
16 use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_sub };
17
18 use base 'Perl::Critic::Policy';
19
20 our $VERSION = '1.088';
21
22 #-----------------------------------------------------------------------------
23
24 Readonly::Scalar my $EXPL => q{Consider refactoring};
25
26 #-----------------------------------------------------------------------------
27
28 sub supported_parameters {
29     return (
30         {
31             name            => 'max_mccabe',
32             description     => 'The maximum complexity score allowed.',
33             default_string  => '20',
34             behavior        => 'integer',
35             integer_minimum => 1,
36         },
37     );
38 }
39
40 sub default_severity { return $SEVERITY_MEDIUM                }
41 sub default_themes   { return qw(core complexity maintenance) }
42 sub applies_to       { return 'PPI::Statement::Sub'           }
43
44 #-----------------------------------------------------------------------------
45
46 sub violates {
47     my ( $self, $elem, undef ) = @_;
48
49     my $score = calculate_mccabe_of_sub( $elem );
50
51     # Is it too complex?
52     return if $score <= $self->{_max_mccabe};
53
54     my $desc = qq{Subroutine with high complexity score ($score)};
55     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> distribution.
76
77
78 =head1 DESCRIPTION
79
80 All else being equal, complicated code is more error-prone and more
81 expensive to maintain than simpler code.  The first step towards
82 managing complexity is to establish formal complexity metrics.  One
83 such metric is the McCabe score, which describes the number of
84 possible paths through a subroutine.  This Policy approximates the
85 McCabe score by summing the number of conditional statements and
86 operators within a subroutine.  Research has shown that a McCabe score
87 higher than 20 is a sign of high-risk, potentially untestable code.
88 See L<http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html>
89 for some discussion about the McCabe number and other complexity
90 metrics.
91
92 The usual prescription for reducing complexity is to refactor code
93 into smaller subroutines.  Mark Dominus book "Higher Order Perl" also
94 describes callbacks, recursion, memoization, iterators, and other
95 techniques that help create simple and extensible Perl code.
96
97 =head1 CONFIGURATION
98
99 The maximum acceptable McCabe can be set with the C<max_mccabe>
100 configuration item.  Any subroutine with a McCabe score higher than
101 this number will generate a policy violation.  The default is 20.  An
102 example section for a F<.perlcriticrc>:
103
104   [Subroutines::ProhibitExcessComplexity]
105   max_mccabe = 30
106
107 =head1 NOTES
108
109
110   "Everything should be made as simple as possible, but no simpler."
111
112                                                   -- Albert Einstein
113
114
115 Complexity is subjective, but formal complexity metrics are still
116 incredibly valuable.  Every problem has an inherent level of
117 complexity, so it is not necessarily optimal to minimize the McCabe
118 number.  So don't get offended if your code triggers this Policy.
119 Just consider if there B<might> be a simpler way to get the job done.
120
121
122 =head1 AUTHOR
123
124 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
125
126 =head1 COPYRIGHT
127
128 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
129
130 This program is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.  The full text of this license
132 can be found in the LICENSE file included with this module.
133
134 =cut
135
136 # Local Variables:
137 #   mode: cperl
138 #   cperl-indent-level: 4
139 #   fill-column: 78
140 #   indent-tabs-mode: nil
141 #   c-indentation-style: bsd
142 # End:
143 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :