Perl::Critic::Policy::Bangs::ProhibitVagueNames - Don't use generic variable names.


Perl-Critic-Bangs documentation Contained in the Perl-Critic-Bangs distribution.

Index


Code Index:

NAME

Top

Perl::Critic::Policy::Bangs::ProhibitVagueNames - Don't use generic variable names.

AFFILIATION

Top

This Policy is part of the Perl::Critic::Bangs distribution.

DESCRIPTION

Top

Variables should have descriptive names. Names like $data and $info are completely vague.

   my $data = shift;      # not OK.
   my $userinfo = shift   # OK

See http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_variable.html for more of my ranting on this.

CONFIGURATION

Top

This policy has two options: names and add_names.

names

To replace the list of vague names, specify them as a whitespace delimited set of prohibited names.

    [Bangs::ProhibitVagueNames]
    names = data count line next

add_names

To add to the list of vague names, specify them as a whitespace delimited set of prohibited names.

    [Bangs::ProhibitVagueNames]
    add_names = foo bar bat

AUTHOR

Top

Andy Lester <andy at petdance.com> from code by Andrew Moore <amoore at mooresystems.com>.

ACKNOWLEDGMENTS

Top

Adapted from policies by Jeffrey Ryan Thalhammer <thaljef@cpan.org>, Based on App::Fluff by Andy Lester, "<andy at petdance.com>"

COPYRIGHT

Top


Perl-Critic-Bangs documentation Contained in the Perl-Critic-Bangs distribution.
package Perl::Critic::Policy::Bangs::ProhibitVagueNames;

use strict;
use warnings;
use Perl::Critic::Utils qw( :booleans :severities );
use base 'Perl::Critic::Policy';

our $VERSION = '1.08';

#----------------------------------------------------------------------------

sub supported_parameters {
    return (
        {
            name           => 'names',
            description    => 'Words to prohibit as variable names.',
            behavior       => 'string list',
            default_string => 'data info var obj object tmp temp',
        },
        {
            name           => 'add_names',
            description    => 'Additional words to prohibit as variable names.',
            behavior       => 'string list',
        },
    );
}

sub default_severity     { return $SEVERITY_MEDIUM        }
sub default_themes       { return qw( bangs readability ) }
sub applies_to           { return 'PPI::Token::Symbol'    }

sub initialize_if_enabled {
    my ( $self, $config ) = @_;

    $self->{_names} = { %{ $self->{_names} }, %{ $self->{_add_names} } };

    return $TRUE;
}


sub violates {
    my ( $self, $elem, $doc ) = @_;

    # make $basename be the variable name with no sigils or namespaces.
    my $canonical = $elem->canonical();
    my $basename = $canonical;
    $basename =~ s/.*:://;
    $basename =~ s/^[\$@%]//;

    foreach my $naughty ( keys %{ $self->{'_names'} } ) {
        if ( $basename eq $naughty ) {
            my $desc = qq(Variable named "$canonical");
            my $expl = 'Variable names should be specific, not vague';
            return $self->violation( $desc, $expl, $elem );
        }
    }
    return;
}

1;