Section 2.12. Blocks


2.12. Blocks

Never place two statements on the same line.

If two or more statements share one line, each of them becomes harder to comprehend:

     RECORD:     while (my $record = <$inventory_file>) {         chomp $record; next RECORD if $record eq $EMPTY_STR;         my @fields = split $FIELD_SEPARATOR, $record; update_sales(\@fields);$count++;     }

You're already saving vertical space by using K&R bracketing; use that space to improve the code's readability, by giving each statement its own line:

      RECORD:     while (my $record = <$inventory_file>) {         chomp $record;         next RECORD if $record eq $EMPTY_STR;         my @fields = split $FIELD_SEPARATOR, $record;         update_sales(\@fields);         $count++;     } 

Note that this guideline applies even to map and grep blocks that contain more than one statement. You should write:

      my @clean_words         = map {               my $word = $_;               $word =~ s/$EXPLETIVE/[DELETED]/gxms;               $word;           } @raw_words; 

not:

     my @clean_words         = map { my $word = $_; $word =~ s/$EXPLETIVE/[DELETED]/gxms; $word } @raw_words;



Perl Best Practices
Perl Best Practices
ISBN: 0596001738
EAN: 2147483647
Year: 2004
Pages: 350
Authors: Damian Conway

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net