NAME

Perl::Critic::Policy::ControlStructures::ProhibitMultipleSubscripts - forbid using the same subscript multiple times in a loop

AFFILIATION

This policy as a part of the Perl::Critic::PolicyBundle::SNEZ distribution.

DESCRIPTION

Conway suggests only extracting specific values of arrays and hashes in loops exactly once and assigning them to variables for later access. Not only does it make the code less cluttered with repeated lookups, it is also more efficient in many cases.

# Not ok
for my $n (0..$#clients) {
    $clients[$n]->tally_hours();
    $clients[$n]->bill_hours();
    $clients[$n]->reset_hours();
}

# Ok
for my $client (@clients) {
    $client->tally_hours();
    $client->bill_hours();
    $client->reset_hours();
}

# Not ok
for my $agent_num (0..$#operatives) {                        # Iterate indices
    print "Checking agent $agent_num\n";                     # Use index
    if ($on_disavowed_list{$operatives[$agent_num]}) {       # Extract value
        print "\t...$operatives[$agent_num] disavowed!\n";   # Extract value again
    }
}

# Ok
for my $agent_num (0 .. $#operatives) {
    print "Checking agent $agent_num\n";
    my $agent = $operatives[$agent_num];
    if ($on_disavowed_list{$agent}) {
        print "\t...$agent disavowed!\n";
    }
}

# Not ok
foreach my $elem_ref (@stuff) {
    Some::Util::foo($elem_ref->{data});
    Some::Util::bar($elem_ref->{data});
}

# Ok
foreach my $elem_ref (@stuff) {
    my $data = $elem_ref->{data};
    Some::Util::foo($data);
    Some::Util::bar($data);
}

CONFIGURATION

This Policy is not configurable except for the standard options.

COPYRIGHT

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.