1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/ControlStructures/ProhibitDeepNests.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::ControlStructures::ProhibitDeepNests;
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Code structure is deeply nested};
23 Readonly::Scalar my $EXPL => q{Consider refactoring};
25 #-----------------------------------------------------------------------------
27 sub supported_parameters {
31 description => 'The maximum number of nested constructs to allow.',
32 default_string => '5',
33 behavior => 'integer',
39 sub default_severity { return $SEVERITY_MEDIUM }
40 sub default_themes { return qw(core maintenance complexity) }
41 sub applies_to { return 'PPI::Statement::Compound' }
43 #-----------------------------------------------------------------------------
46 my ( $self, $elem, undef ) = @_;
48 my $nest_count = 1; #For _this_ element
51 while ( $parent = $parent->parent() ){
52 if( $parent->isa('PPI::Statement::Compound') ) {
57 if ( $nest_count > $self->{_max_nests} ) {
58 return $self->violation( $DESC, $EXPL, $elem );
69 #-----------------------------------------------------------------------------
73 =for stopwords refactored
77 Perl::Critic::Policy::ControlStructures::ProhibitDeepNests - Don't write deeply nested loops and conditionals.
81 This Policy is part of the core L<Perl::Critic> distribution.
86 Deeply nested code is often hard to understand and may be a sign that
87 it needs to be refactored. There are several good books on how to
88 refactor code. I like Martin Fowler's "Refactoring: Improving The
89 Design of Existing Code".
94 The maximum number of nested control structures can be configured via a value
95 for C<max_nests> in a F<.perlcriticrc> file. Each for-loop, if-else, while,
96 and until block is counted as one nest. Postfix forms of these constructs are
97 not counted. The default maximum is 5. Customization in a F<.perlcriticrc>
100 [ControlStructures::ProhibitDeepNests]
105 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
109 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
111 This program is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself. The full text of this license
113 can be found in the LICENSE file included with this module.
119 # cperl-indent-level: 4
121 # indent-tabs-mode: nil
122 # c-indentation-style: bsd
124 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :