Journal(2005) | Blog(2006) | RandomLink | WhoAmI | LiveBookmark | HomePage

<<Previous: yes yes yes, i'm back  >>Next: Tips from Perl Best Practices

一种代码风格

Category: BookNotes   Keywords: Code Layout

刚拿起盗版电子书《Perl Best Practices》在读。没机会买正版(没 Visa/MasterCard + 穷),也只好捧着盗版先读下了。
有机会还是挺想买正版书支持下 Damain Conway 先生的。

既然读着,也就随手翻译点,写点东西。不保证任何东西。

Mr Conway 在 Chapter 2 Code Layout 中讲了下代码布局风格,我看着挺好的,也值得推广下。

    # 推荐
    my @names = (
        'Damian',    # Primary key
        'Matthew',   # Disambiguator
        'Conway',    # General class or category
    );

    for my $name (@names) {
        for my $word ( anagrams_of(lc $name) ) {
            print "$word\n";
        }
    }
    # 不推荐这两种

    # Don't use BSD style...
    my @names =
    (
        'Damian',    # Primary key
        'Matthew',   # Disambiguator
        'Conway',    # General class or category
    );

    for my $name (@names)
    {
        for my $word (anagrams_of(lc $name))
        {
            print "$word\n";
        }
    }

    # And don't use GNU style either...

    for my $name (@names)
      {
        for my $word (anagrams_of(lc $name))
          {
            print "$word\n";
          }
      }
# 推荐
for my $result (@results) {
while ($min < $max) {
if ($value[$try] < $target) {

# 不推荐
for(@results) {
while($min < $max) {
if($value[$try] < $target) {
# Do
get_candidates($marker);
if open_region($i);
$candidates[$i]{region}

# DONOT
get_candidates ($marker);
if open_region ($i);
$candidates [$i] {region}
# Do
$candidates[$i] = $incumbent{ $candidates[$i]{ get_region( ) } };
print $incumbent{ $largest_gerrymandered_constituency };

# Do NOT
$candidates[$i] = $incumbent{$candidates[$i]{get_region( )}};
print $incumbent{$largest_gerrymandered_constituency};
 # Do
my $displacement = $initial_velocity * $time  +  0.5 * $acceleration * $time**2;
my $price = $coupon_paid * $exp_rate  +  ($face_val + $coupon_paid) * $exp_rate**2;

# Do NOT
my $displacement=$initial_velocity*$time+0.5*$acceleration*$time**2;
my $price=$coupon_paid*$exp_rate+(($face_val+$coupon_val)*$exp_rate**2);
# Do
if ( $line =~ s{\A (\s*) -- (.*)}{$1#$2}xms ) {
    push @comments, $2;
}
my @sqrt_results = map { sqrt $_ } @results;

# Do NOT
if ( $line =~ s{\A (\s*) -- (.*)}{$1#$2}xms ) {
    push @comments, $2
}
my @sqrt_results = map { sqrt $_; } @results;
 # Do
chomp $record;
next RECORD if $record eq $EMPTY_STR;

# Do NOT
chomp $record; next RECORD if $record eq $EMPTY_STR;
# Do
}
else {
}
elsif ($sigil eq '@' && $subsigil eq '?') {

# NOT
} else {
} elsif ($sigil eq '@' && $subsigil eq '?') {
# Do
my %expansion_of = (
    q{it's}    => q{it is},
    q{we're}   => q{we are},
    q{didn't}  => q{did not},
    q{must've} => q{must have},
    q{I'll}    => q{I will},
);
$name   = standardize_name($name);
$age    = time - $birth_date;
$status = 'active';


# Do NOT
my %expansion_of = (
    q{it's} => q{it is}, q{we're} => q{we are}, q{didn't} => q{did not},
    q{must've} => q{must have}, q{I'll} => q{I will},
);
$name = standardize_name($name);
$age = time - $birth_date;
$status = 'active';
# Do
push @steps, $steps[-1]
             + $radial_velocity * $elapsed_time
             + $orbital_velocity * ($phase + $phase_shift)
             - $DRAG_COEFF * $altitude
             ;

# NOT
push @steps, $steps[-1] +
     $radial_velocity * $elapsed_time +
     $orbital_velocity * ($phase + $phase_shift) -
     $DRAG_COEFF * $altitude;
# Do
my $next_step = $steps[-1]
                + $radial_velocity * $elapsed_time
                + $orbital_velocity * ($phase + $phase_shift)
                - $DRAG_COEFF * $altitude
                ;
add_step( \@steps, $next_step, $elapsed_time);

# Do NOT
add_step( \@steps, $steps[-1]
                   + $radial_velocity * $elapsed_time
                   + $orbital_velocity * ($phase + $phase_shift)
                   - $DRAG_COEFF * $altitude
                   , $elapsed_time);
# Do
push @steps, $steps[-1]
             + $radial_velocity * $elapsed_time
             + $orbital_velocity
                 * ($phase + $phase_shift)
             - $DRAG_COEFF * $altitude
             ;

# NOT
push @steps, $steps[-1] + $radial_velocity
             * $elapsed_time + $orbital_velocity
             * ($phase + $phase_shift) - $DRAG_COEFF
             * $altitude
             ;
# Do
$predicted_val
    = $average + $predicted_change * $fudge_factor;

# NOT
$predicted_val = $average
                 + $predicted_change * $fudge_factor
                 ;
 # Do
my $salute = $name eq $EMPTY_STR                      ? 'Customer'
           : $name =~ m/\A((?:Sir|Dame) \s+ \S+) /xms ? $1
           : $name =~ m/(.*), \s+ Ph[.]?D \z     /xms ? "Dr $1"
           :                                            $name
           ;

# NOT
my $salute = $name eq $EMPTY_STR ? 'Customer'
           : $name =~ m/\A((?:Sir|Dame) \s+ \S+)/xms ? $1
           : $name =~ m/(.*), \s+ Ph[.]?D \z/xms ? "Dr $1" : $name;
# Do
push @items, (
    "A brand new $item",
    "A fully refurbished $item",
    "A ratty old $item",
);

# NOT
push @items, "A brand new $item"
           , "A fully refurbished $item"
           , "A ratty old $item"
           ;

当然风格是自己选的,并非强制。 Perl 世界是自由的世界。

<<Previous: yes yes yes, i'm back  >>Next: Tips from Perl Best Practices

Options: +Del.icio.us

Related items Created on 2005-09-13 13:02:31, Last modified on 2005-09-13 13:40:58
Copyright 2004-2005 All Rights Reserved. Powered by Eplanet && Catalyst 5.62.