IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 16, 2004, 1:40:04 PM (22 years ago)
Author:
jhoblitt
Message:

consistently indent verbatim blocks

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/misc/perlCodeConventions.tex

    r2377 r2378  
    1 %%% $Id: perlCodeConventions.tex,v 1.19 2004-11-16 22:15:07 jhoblitt Exp $
     1%%% $Id: perlCodeConventions.tex,v 1.20 2004-11-16 23:40:04 jhoblitt Exp $
    22\documentclass[panstarrs]{panstarrs}
    33
     
    5656
    5757\begin{verbatim}
    58       Adapted with permission from
    59       CODE CONVENTIONS FOR THE JAVA^TM PROGRAMMING LANGUAGE.
    60       Copyright 1995-1999 Sun
    61       Microsysytems, Inc.  All rights reserved.
     58    Adapted with permission from
     59    CODE CONVENTIONS FOR THE JAVA^TM PROGRAMMING LANGUAGE.
     60    Copyright 1995-1999 Sun
     61    Microsysytems, Inc.  All rights reserved.
    6262\end{verbatim}
    6363
     
    193193\normalsize
    194194\begin{verbatim}
    195 use 5.008005;                           # optional
    196 
    197 use strict;                             # not optional
    198 use warnings;                           # not optional
    199 
    200 use Foo qw( $bar );                     # import $bar into our namespace
    201 
    202 use constant MAXFOO => 3;               # maximum number of foos
    203 
    204 my $baz;                                # is a lexical variable in the top level scope
    205 
    206 sub fuu
    207 {
    208 
    209 }
    210 
    211 __END__
    212 
    213 ...
     195    use 5.008005;                           # optional
     196
     197    use strict;                             # not optional
     198    use warnings;                           # not optional
     199
     200    use Foo qw( $bar );                     # import $bar into our namespace
     201
     202    use constant MAXFOO => 3;               # maximum number of foos
     203
     204    my $baz;                                # is a lexical variable in the top level scope
     205
     206    sub fuu
     207    {
     208
     209    }
     210
     211    __END__
     212
     213    ...
    214214\end{verbatim}
    215215
     
    267267\normalsize
    268268\begin{verbatim}
    269 package Foo;
    270 
    271 use 5.008005;                           # optional
    272 
    273 use strict;                             # not optional
    274 use warnings;                           # not optional
    275 
    276 use base qw( Baz );                     # become a subclass of Baz
    277 
    278 use Foo qw( $bar );                     # import $bar into our namespace
    279 
    280 use constant MAXFOO => 3;               # maximum number of foos
    281 
    282 my $baz;                                # is a lexical variable in the top level scope
    283 
    284 sub fuu
    285 {
    286 
    287 }
    288 
    289 1;
     269    package Foo;
     270
     271    use 5.008005;                           # optional
     272
     273    use strict;                             # not optional
     274    use warnings;                           # not optional
     275
     276    use base qw( Baz );                     # become a subclass of Baz
     277
     278    use Foo qw( $bar );                     # import $bar into our namespace
     279
     280    use constant MAXFOO => 3;               # maximum number of foos
     281
     282    my $baz;                                # is a lexical variable in the top level scope
     283
     284    sub fuu
     285    {
     286
     287    }
     288
     289    1;
    290290
    291291__END__
     
    301301
    302302\begin{verbatim}
    303 =pod
    304 
    305 =head1 NAME
    306 
    307 Foo - Does something with Baz.
    308 
    309 =head1 SYNOPSIS
    310 
    311     use Foo;
     303    =pod
     304
     305    =head1 NAME
     306
     307    Foo - Does something with Baz.
     308
     309    =head1 SYNOPSIS
     310
     311        use Foo;
     312
     313        ...
     314
     315    =head1 DESCRIPTION
     316
     317    More details about what this modules does with Baz.
     318
     319    =head1 USAGE
     320
     321    =head2 Import Parameters
     322
     323    This module accepts no arguments to it's C<import> method and exports no
     324    I<symbols>.
     325
     326    =head2 Methods
     327
     328    =head3 Class Methods
     329
     330    =over 4
     331
     332    =item * methodOne
     333
     334    details of methodOne
     335
     336    =item * methodTwo
     337
     338    details of methodTwo
    312339
    313340    ...
    314341
    315 =head1 DESCRIPTION
    316 
    317 More details about what this modules does with Baz.
    318 
    319 =head1 USAGE
    320 
    321 =head2 Import Parameters
    322 
    323 This module accepts no arguments to it's C<import> method and exports no
    324 I<symbols>.
    325 
    326 =head2 Methods
    327 
    328 =head3 Class Methods
    329 
    330 =over 4
    331 
    332 =item * methodOne
    333 
    334 details of methodOne
    335 
    336 =item * methodTwo
    337 
    338 details of methodTwo
    339 
    340 ...
    341 
    342 =back
    343 
    344 =head1 SUPPORT
    345 
    346 Where to get help.
    347 
    348 =head1 AUTHOR
    349 
    350 Principle authors and contact info.
    351 
    352 =head1 COPYRIGHT
    353 
    354 Copyright (C) 2004  Robert Lupton.  All rights reserved.
    355 
    356 This program is free software; you can redistribute it and/or modify it under
    357 the terms of the GNU General Public License as published by the Free Software
    358 Foundation; either version 2 of the License, or (at your option) any later
    359 version.
    360 
    361 This program is distributed in the hope that it will be useful, but WITHOUT ANY
    362 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
    363 PARTICULAR PURPOSE.  See the GNU General Public License for more details.
    364 
    365 You should have received a copy of the GNU General Public License along with
    366 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    367 Place - Suite 330, Boston, MA  02111-1307, USA.
    368 
    369 The full text of the license can be found in the LICENSE file included with
    370 this module, or in the L<perlgpl> Pod as supplied with Perl 5.8.1 and later.
    371 
    372 =head1 SEE ALSO
    373 
    374 L<Bar::Baz>
    375 
    376 =cut
     342    =back
     343
     344    =head1 SUPPORT
     345
     346    Where to get help.
     347
     348    =head1 AUTHOR
     349
     350    Principle authors and contact info.
     351
     352    =head1 COPYRIGHT
     353
     354    Copyright (C) 2004  Robert Lupton.  All rights reserved.
     355
     356    This program is free software; you can redistribute it and/or modify it under
     357    the terms of the GNU General Public License as published by the Free Software
     358    Foundation; either version 2 of the License, or (at your option) any later
     359    version.
     360
     361    This program is distributed in the hope that it will be useful, but WITHOUT ANY
     362    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
     363    PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     364
     365    You should have received a copy of the GNU General Public License along with
     366    this program; if not, write to the Free Software Foundation, Inc., 59 Temple
     367    Place - Suite 330, Boston, MA  02111-1307, USA.
     368
     369    The full text of the license can be found in the LICENSE file included with
     370    this module, or in the L<perlgpl> Pod as supplied with Perl 5.8.1 and later.
     371
     372    =head1 SEE ALSO
     373
     374    L<Bar::Baz>
     375
     376    =cut
    377377\end{verbatim}
    378378
     
    404404
    405405\begin{verbatim}
    406 if ($a < $b ||
    407     $a > 2*$b) {
    408     $x = ($a + $b + $c) +
    409     sin($z);
    410 }
     406    if ($a < $b ||
     407        $a > 2*$b) {
     408        $x = ($a + $b + $c) +
     409        sin($z);
     410    }
    411411\end{verbatim}
    412412
     
    427427
    428428\begin{verbatim}
    429 someSub(longExpression1, longExpression2, longExpression3,
    430          longExpression4, longExpression5);
    431  
    432 $var = someSub(longExpression1,
    433                  someSub(longExpression2,
    434                           longExpression3));
     429    someSub(longExpression1, longExpression2, longExpression3,
     430             longExpression4, longExpression5);
     431    
     432    $var = someSub(longExpression1,
     433                     someSub(longExpression2,
     434                  longExpression3));
    435435\end{verbatim}
    436436
     
    451451
    452452\begin{verbatim}
    453 $alpha = (aLongBooleanExpression) ? $beta : $gamma; 
    454 
    455 $alpha = (aLongBooleanExpression) ? $beta
    456                                  : $gamma; 
    457 
    458 $alpha = (aLongBooleanExpression)
    459         ? $beta
    460         : $gamma; 
     453    $alpha = (aLongBooleanExpression) ? $beta : $gamma; 
     454
     455    $alpha = (aLongBooleanExpression) ? $beta
     456                                     : $gamma; 
     457
     458    $alpha = (aLongBooleanExpression)
     459            ? $beta
     460            : $gamma; 
    461461\end{verbatim}
    462462
     
    475475
    476476\begin{verbatim}
    477     if ($foo) {
    478 #        print("foo");
    479     }
     477        if ($foo) {
     478    #        print("foo");
     479        }
    480480\end{verbatim}
    481481
     
    487487
    488488\begin{verbatim}
    489 if (0) {
    490     if ($foo) {
    491         print("foo");
    492     }
    493 }
     489    if (0) {
     490        if ($foo) {
     491            print("foo");
     492        }
     493    }
    494494\end{verbatim}
    495495
     
    531531
    532532\begin{verbatim}
    533 # some words
     533    # some words
    534534\end{verbatim}
    535535
     
    540540
    541541\begin{verbatim}
    542 # this is the start of a multi-line comment that continues across a couple of
    543 # lines then has a logical separation before another comment
    544 #
    545 # this is an adjoined comment
     542    # this is the start of a multi-line comment that continues across a couple of
     543    # lines then has a logical separation before another comment
     544    #
     545    # this is an adjoined comment
    546546\end{verbatim}
    547547
     
    557557
    558558\begin{verbatim}
    559 if ($a == 2) {
    560     return TRUE;                        # special case
    561 } else {
    562     return isPrime($a);                 # works only for odd a
    563 }
     559    if ($a == 2) {
     560        return TRUE;                        # special case
     561    } else {
     562        return isPrime($a);                 # works only for odd a
     563    }
    564564\end{verbatim}
    565565
     
    574574
    575575\begin{verbatim}
    576 =item * reallyCoolSubroutine
    577 
    578 Accepts a real number and a string.  Does some really cool stuff with it and
    579 returns a really cool thing.
    580 
    581 =cut
    582 
    583 sub reallyCoolSubroutine
    584 {
    585     my ($aNumber, $aString) = @_;
    586 
    587     # Do really cool stuff
    588 
    589     return $aThing;
    590 }
     576    =item * reallyCoolSubroutine
     577
     578    Accepts a real number and a string.  Does some really cool stuff with it and
     579    returns a really cool thing.
     580
     581    =cut
     582
     583    sub reallyCoolSubroutine
     584    {
     585        my ($aNumber, $aString) = @_;
     586
     587        # Do really cool stuff
     588
     589        return $aThing;
     590    }
    591591\end{verbatim}
    592592
     
    605605
    606606\begin{verbatim}
    607 my $level;                              # indentation level
    608 my $size;                               # size of table
     607    my $level;                              # indentation level
     608    my $size;                               # size of table
    609609\end{verbatim}
    610610
     
    612612
    613613\begin{verbatim}
    614 my ($level, $size);
     614    my ($level, $size);
    615615\end{verbatim}
    616616
     
    620620Do not put different types on the same line. Example:
    621621\begin{verbatim}
    622 my ($foo, @fooArray);                  # AVOID!
     622    my ($foo, @fooArray);                  # AVOID!
    623623\end{verbatim}
    624624
     
    630630
    631631\begin{verbatim}
    632 my $count;
    633 ...
    634 sub mySubroutine
    635 {
    636     local $count;                       # AVOID!
     632    my $count;
    637633    ...
    638 }
     634    sub mySubroutine
     635    {
     636        local $count;                       # AVOID!
     637        ...
     638    }
    639639\end{verbatim}
    640640
     
    689689
    690690\begin{verbatim}
    691 sub subName
    692 {
    693     my ($param1, $param2, ...) = @_;
    694     ...
    695 
    696     return $val;
    697 }
     691    sub subName
     692    {
     693        my ($param1, $param2, ...) = @_;
     694        ...
     695
     696        return $val;
     697    }
    698698\end{verbatim}
    699699
     
    705705
    706706Each line should usually contain only one statement. Example:
    707 \begin{verbatim}
    708 $x = sqrt($x2);                         # Correct
    709 $i++;                                   # Correct 
    710 $x = sqrt($x2); $i++;                   # Avoid!
     707
     708\begin{verbatim}
     709    $x = sqrt($x2);                         # Correct
     710    $i++;                                   # Correct 
     711    $x = sqrt($x2); $i++;                   # Avoid!
    711712\end{verbatim}
    712713
    713714An example of a reasonable two-statement line is:
    714 \begin{verbatim}
    715 $foo++; $bar--;
    716 \end{verbatim}
     715
     716\begin{verbatim}
     717    $foo++; $bar--;
     718\end{verbatim}
     719
    717720where the two actions are intimately related.
    718721
     
    737740
    738741\begin{verbatim}
    739 return undef;                           # Correct
    740 return;                                 # Avoid!
    741 
    742 return $myDisk->size;
    743 
    744 return ($size ? $size : $defaultSize);
     742    return undef;                           # Correct
     743    return;                                 # Avoid!
     744
     745    return $myDisk->size;
     746
     747    return ($size ? $size : $defaultSize);
    745748\end{verbatim}
    746749
     
    757760
    758761The \code{if-else} class of statements should have the following form:
    759 \begin{verbatim}
    760 if (condition) {
    761     statements;
    762 }
    763 
    764 if (condition) {
    765     statements;
    766 } else {
    767     statements;
    768 }
    769 
    770 if (condition) {
    771     statements;
    772 } elsif (condition) {
    773     statements;
    774 } else {
    775     statements;
    776 }
     762
     763\begin{verbatim}
     764    if (condition) {
     765        statements;
     766    }
     767
     768    if (condition) {
     769        statements;
     770    } else {
     771        statements;
     772    }
     773
     774    if (condition) {
     775        statements;
     776    } elsif (condition) {
     777        statements;
     778    } else {
     779        statements;
     780    }
    777781\end{verbatim}
    778782
     
    782786
    783787\begin{verbatim}
    784 for (initialization; condition; update) {
    785     statements;
    786 }
     788    for (initialization; condition; update) {
     789        statements;
     790    }
    787791\end{verbatim}
    788792
     
    790794
    791795\begin{verbatim}
    792 for (my $i = 0; $i < 100; $i++) {
    793     print "$i\n";
    794 }
     796    for (my $i = 0; $i < 100; $i++) {
     797        print "$i\n";
     798    }
    795799\end{verbatim}
    796800
     
    799803
    800804\begin{verbatim}
    801 for (initialization; condition; update) {}
     805    for (initialization; condition; update) {}
    802806\end{verbatim}
    803807
     
    814818
    815819\begin{verbatim}
    816 foreach my $item (@workingList) {
    817     # do stuff with $item
    818 }
     820    foreach my $item (@workingList) {
     821        # do stuff with $item
     822    }
    819823\end{verbatim}
    820824
     
    823827
    824828\begin{verbatim}
    825 foreach (@workingList) {                # Avoid!
    826     # do stuff with $_
    827 }
     829    foreach (@workingList) {                # Avoid!
     830        # do stuff with $_
     831    }
    828832\end{verbatim}
    829833
     
    833837
    834838\begin{verbatim}
    835 while (condition) {
    836     statements;
    837 }
     839    while (condition) {
     840        statements;
     841    }
    838842\end{verbatim}
    839843
     
    841845
    842846\begin{verbatim}
    843 while (condition) {}
     847    while (condition) {}
    844848\end{verbatim}
    845849
     
    849853
    850854\begin{verbatim}
    851 do {
    852     statements;
    853 } while (condition);
     855    do {
     856        statements;
     857    } while (condition);
    854858\end{verbatim}
    855859
     
    860864
    861865\begin{verbatim}
    862 MYLABEL: while (condition) {
    863     if (condition) {
    864         next MYLABEL;
    865     }
    866 }
     866    MYLABEL: while (condition) {
     867        if (condition) {
     868            next MYLABEL;
     869        }
     870    }
    867871\end{verbatim}
    868872
     
    897901
    898902\begin{verbatim}
    899 while (true) {
    900     ...
    901 }
     903    while (true) {
     904        ...
     905    }
    902906\end{verbatim}
    903907
     
    916920    $a += $c + $d;
    917921    $a = ($a + $b) / ($c * $d);
    918    
     922
    919923    while ($d++ = $s++) {
    920924        $n++;
     
    928932
    929933\begin{verbatim}
    930     for (expr1; expr2; expr3)
     934    for (expr1; expr2; expr3) {}
    931935\end{verbatim}
    932936
     
    936940
    937941\begin{verbatim}
    938 my @things = qw( foo bar baz );
     942    my @things = qw( foo bar baz );
    939943\end{verbatim}
    940944\end{itemize}
     
    10471051
    10481052\begin{verbatim}
    1049 package someModule;
    1050 
    1051 use base qw( Exporter );
    1052 
    1053 our @EXPORT_OK = qw( $foo $bar );
    1054 
     1053    use base qw( Exporter );
     1054
     1055    our @EXPORT_OK = qw( $foo $bar );
    10551056\end{verbatim}
    10561057
     
    10671068
    10681069\begin{verbatim}
    1069 $row0 = $col0 = 0;
    1070 $sum = $sumx = $sumy = 0;
     1070    $row0 = $col0 = 0;
     1071    $sum = $sumx = $sumy = 0;
    10711072\end{verbatim}
    10721073
     
    10751076
    10761077\begin{verbatim}
    1077 if ($c++ = $d++) {                      # AVOID!
    1078     ...
    1079 }
     1078    if ($c++ = $d++) {                      # AVOID!
     1079        ...
     1080    }
    10801081\end{verbatim}
    10811082
     
    10831084
    10841085\begin{verbatim}
    1085 if (($c++ = $d++) != 0) {
    1086     ...
    1087 }
     1086    if (($c++ = $d++) != 0) {
     1087        ...
     1088    }
    10881089\end{verbatim}
    10891090
    10901091Do not use embedded assignments in an attempt to improve run-time
    10911092performance. This is the job of the compiler. Example:
    1092 \begin{verbatim}
    1093 $d = ($a = $b + $c) + $r;               # AVOID!
     1093
     1094\begin{verbatim}
     1095    $d = ($a = $b + $c) + $r;               # AVOID!
    10941096\end{verbatim}
    10951097
    10961098should be written as
    1097 \begin{verbatim}
    1098 $a = $b + $c;
    1099 $d = $a + $r;
     1099
     1100\begin{verbatim}
     1101    $a = $b + $c;
     1102    $d = $a + $r;
    11001103\end{verbatim}
    11011104
     
    11111114
    11121115\begin{verbatim}
    1113 if ($a & $b || $c & $d) {}              # AVOID!
    1114 if (($a & $b) || ($c & $d)) {}          # RIGHT
     1116    if ($a & $b || $c & $d) {}              # AVOID!
     1117    if (($a & $b) || ($c & $d)) {}          # RIGHT
    11151118\end{verbatim}
    11161119
     
    11201123
    11211124\begin{verbatim}
    1122 if (($a == $b && $c == $d) || $e == $f) {}
    1123 $l << ($j + $k)
    1124 ($l << $j) + $k
    1125 ($i & $b) | $c
     1125    if (($a == $b && $c == $d) || $e == $f) {}
     1126    $l << ($j + $k)
     1127    ($l << $j) + $k
     1128    ($i & $b) | $c
    11261129\end{verbatim}
    11271130
     
    11311134
    11321135\begin{verbatim}
    1133 if (booleanExpression) {
    1134     return true;
    1135 } else {
    1136     return false;
    1137 }
     1136    if (booleanExpression) {
     1137        return true;
     1138    } else {
     1139        return false;
     1140    }
    11381141\end{verbatim}
    11391142
     
    11411144
    11421145\begin{verbatim}
    1143 return booleanExpression;
     1146    return booleanExpression;
    11441147\end{verbatim}
    11451148
     
    11481151
    11491152\begin{verbatim}
    1150 return (booleanExpression ? true : false);
     1153    return (booleanExpression ? true : false);
    11511154\end{verbatim}
    11521155
     
    11541157
    11551158\begin{verbatim}
    1156 if (condition) {
    1157     return $x;
    1158 }
    1159 return $y;
    1160 \end{verbatim}
     1159    if (condition) {
     1160        return $x;
     1161    }
     1162    return $y;
     1163\end{verbatim}
     1164
    11611165should be written as
    1162 \begin{verbatim}
    1163 return (condition ? x : y);
     1166
     1167\begin{verbatim}
     1168    return (condition ? x : y);
    11641169\end{verbatim}
    11651170
     
    11701175
    11711176\begin{verbatim}
    1172 ($x >= 0) ? $x : -$x;
     1177    ($x >= 0) ? $x : -$x;
    11731178\end{verbatim}
    11741179
Note: See TracChangeset for help on using the changeset viewer.