1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Modules/ProhibitExcessMainComplexity.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Modules::ProhibitExcessMainComplexity;
15 use Perl::Critic::Utils qw{ :severities };
16 use Perl::Critic::Utils::McCabe qw{ calculate_mccabe_of_main };
18 use base 'Perl::Critic::Policy';
20 #-----------------------------------------------------------------------------
22 our $VERSION = '1.088';
24 #-----------------------------------------------------------------------------
26 Readonly::Scalar my $EXPL => q{Consider refactoring};
28 #-----------------------------------------------------------------------------
30 sub supported_parameters {
34 description => 'The maximum complexity score allowed.',
35 default_string => '20',
36 behavior => 'integer',
42 sub default_severity { return $SEVERITY_MEDIUM }
43 sub default_themes { return qw(core complexity maintenance) }
44 sub applies_to { return 'PPI::Document' }
46 #-----------------------------------------------------------------------------
49 my ( $self, $doc, undef ) = @_;
51 my $score = calculate_mccabe_of_main( $doc );
54 return if $score <= $self->{_max_mccabe};
56 my $desc = qq{Main code has high complexity score ($score)};
57 return $self->violation( $desc, $EXPL, $doc );
65 #-----------------------------------------------------------------------------
73 Perl::Critic::Policy::Modules::ProhibitExcessMainComplexity - Minimize complexity in code that is B<outside> of subroutines.
77 This Policy is part of the core L<Perl::Critic> distribution.
82 All else being equal, complicated code is more error-prone and more expensive
83 to maintain than simpler code. The first step towards managing complexity is
84 to establish formal complexity metrics. One such metric is the McCabe score,
85 which describes the number of possible paths through a block of code. This
86 Policy approximates the McCabe score by summing the number of conditional
87 statements and operators within a block of code. Research has shown that a
88 McCabe score higher than 20 is a sign of high-risk, potentially untestable
89 code. See L<http://www.sei.cmu.edu/str/descriptions/cyclomatic_body.html> for
90 some discussion about the McCabe number and other complexity metrics.
92 Whereas L<Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity> scores
93 the complexity of each subroutine, this Policy scores the total complexity of
94 all the code that is B<outside> of any subroutine declaration.
96 The usual prescription for reducing complexity is to refactor code into
97 smaller subroutines. Mark Dominus book "Higher Order Perl" also describes
98 callbacks, recursion, memoization, iterators, and other techniques that help
99 create simple and extensible Perl code.
103 The maximum acceptable McCabe score can be set with the C<max_mccabe>
104 configuration item. If the sum of all code B<outside> any subroutine has a
105 McCabe score higher than this number, it will generate a Policy violation.
106 The default is 20. An example section for a F<.perlcriticrc>:
108 [Modules::ProhibitExcessMainComplexity]
114 "Everything should be made as simple as possible, but no simpler."
119 Complexity is subjective, but formal complexity metrics are still incredibly
120 valuable. Every problem has an inherent level of complexity, so it is not
121 necessarily optimal to minimize the McCabe number. So don't get offended if
122 your code triggers this Policy. Just consider if there B<might> be a simpler
123 way to get the job done.
127 L<Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity>
131 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
135 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
137 This program is free software; you can redistribute it and/or modify
138 it under the same terms as Perl itself. The full text of this license
139 can be found in the LICENSE file included with this module.
145 # cperl-indent-level: 4
147 # indent-tabs-mode: nil
148 # c-indentation-style: bsd
150 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :