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) $
6 ##############################################################################
8 package Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity;
15 use Perl::Critic::Utils qw{ :severities :data_conversion :classification };
16 use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_sub };
18 use base 'Perl::Critic::Policy';
20 our $VERSION = '1.088';
22 #-----------------------------------------------------------------------------
24 Readonly::Scalar my $EXPL => q{Consider refactoring};
26 #-----------------------------------------------------------------------------
28 sub supported_parameters {
32 description => 'The maximum complexity score allowed.',
33 default_string => '20',
34 behavior => 'integer',
40 sub default_severity { return $SEVERITY_MEDIUM }
41 sub default_themes { return qw(core complexity maintenance) }
42 sub applies_to { return 'PPI::Statement::Sub' }
44 #-----------------------------------------------------------------------------
47 my ( $self, $elem, undef ) = @_;
49 my $score = calculate_mccabe_of_sub( $elem );
52 return if $score <= $self->{_max_mccabe};
54 my $desc = qq{Subroutine with high complexity score ($score)};
55 return $self->violation( $desc, $EXPL, $elem );
63 #-----------------------------------------------------------------------------
71 Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity - Minimize complexity by factoring code into smaller subroutines.
75 This Policy is part of the core L<Perl::Critic> distribution.
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
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.
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>:
104 [Subroutines::ProhibitExcessComplexity]
110 "Everything should be made as simple as possible, but no simpler."
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.
124 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
128 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
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.
138 # cperl-indent-level: 4
140 # indent-tabs-mode: nil
141 # c-indentation-style: bsd
143 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :