IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3090


Ignore:
Timestamp:
Jan 25, 2005, 11:44:01 AM (21 years ago)
Author:
jhoblitt
Message:

prepared for DR02
incorporated feedback from Paul Price
minor section re-ordering/nesting
misc. grammatical changes/clarifications

File:
1 edited

Legend:

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

    r3042 r3090  
    1 %%% $Id: perlCodeConventions.tex,v 1.44 2005-01-18 23:25:08 jhoblitt Exp $
     1%%% $Id: perlCodeConventions.tex,v 1.45 2005-01-25 21:44:01 jhoblitt Exp $
    22\documentclass[panstarrs]{panstarrs}
    33
     
    1313\project{Pan-STARRS Image Processing Pipeline}
    1414\organization{Institute for Astronomy}
    15 \version{DR01}
     15\version{DR02}
    1616\docnumber{PSDC-430-010}
    1717
     
    2626\hline
    2727DR01& 2004-12-01 & Second Draft\\
     28\hline
     29DR02& 2005-01-24 & Third Draft\\
    2830\RevisionsEnd
    2931
     
    5456This document is derived from the Pan-STARRS IPP C Code Standards document
    5557(PSDC 430-004), which is derived from the Sun Microsystems Java language coding
    56 standards presented in the Java Language Specification\footnote{The Java
    57 Language Specification - http://java.sun.com/docs/books/jls/index.html}.
     58standards presented in the \emph{Java Language Specification}\footnote{The
     59Java Language Specification -
     60\code{http://java.sun.com/docs/books/jls/index.html}}.
    5861
    5962\begin{verbatim}
     
    6467\end{verbatim}
    6568
    66 See the Java Code Conventions Web site
    67 (\code{http://java.sun.com/docs/codeconv/}) for more details.
     69See the \emph{Java Code Conventions} web site\footnote{The Java Code
     70Conventions - \code{http://java.sun.com/docs/codeconv/}} for more details.
    6871
    6972Such an adaption is explicitly permitted by the Sun Microsystems
     
    7982common with Perl programmers.  Instead, it is an attempt to make the C and Perl
    8083coding styles as consistent as is reasonably feasible within the Pan-STARRS
    81 project.  This document contains only a brief discussion of Perl's
    82 Object-Oriented features and none of it's more advanced features.
     84project.  Further limiting the possible applicability of this document, only
     85language features that are deemed likely to be used by the Pan-STARRS project
     86are discussed.  This has resulted in only a brief discussion of Perl's
     87Object-Oriented features and none of it's more advanced features (e.g.
     88AUTOLOAD).
    8389
    8490\subsection{Applicability}
    8591
    86 This document is intended to be relevant to Perl 5.8.0 or later.
     92This document is intended to be relevant to Perl5, version 5.8.0 or later.
    8793
    8894\subsection{Conformance}
    8995
    9096If any of the conventions established in this document conflict with source
    91 code that has been formated by \code{perltidy} (see \S\ref{perltidy}), the
     97code that has been formatted by \code{perltidy} (see \S\ref{perltidy}), the
    9298formatting as output by \code{perltidy} will be considered to be in compliance
    9399with the Perl Code Standards.
     
    109115\subsection{File Suffixes}
    110116
    111 Software uses the following file suffixes:
     117Software uses the following file suffixes (in lowercase):
    112118\begin{center}
    113119\begin{tabular}{ll}
     
    130136The preferred name for a file that invokes a ``build'' process.  Typically this
    131137calls \code{ExtUtils::MakeMaker} or \code{Module::Build} setup the the
    132 necessary files to build and install a module or compile Swig generated C
    133 code.\\
     138necessary files to build and install a module or compile Swig generated C code.
     139\emph{The capitalized \code{.PL} suffix is reserved for build and configuration
     140scripts}\\
    134141\end{tabular}
    135142\end{center}
     
    152159\item
    153160For an example of a properly formatted program, see
    154 Complete Program Example (\S\ref{SourceExample}).
     161\emph{Complete Program Example} (\S\ref{SourceExample}).
    155162
    156163\item
     
    164171\subsubsection{Perl Scripts}
    165172
    166 Script files should have the following order:
     173Script files should have, in the following order:
    167174
    168175\begin{itemize}
    169176\item
    170 A Bourne shell ``she-bang'' for \code{perl}.  \code{#!/usr/bin/perl}.
     177A Bourne shell ``she-bang'' for \code{perl}.  e.g.  \code{#!/usr/bin/perl}.
    171178
    172179\item
     
    194201
    195202\item
    196 Any script level "globals" declared as lexical variables.  e.g. \code{my $foo}
     203Any script level ``globals'' declared as lexical variables.  e.g. \code{my $foo}
     204
     205\item
     206The functional ``body'' of the script.  This where command line parameter
     207parsing, subroutine calls, etc. are placed.
    197208
    198209\item
     
    207218\end{itemize}
    208219
    209 For an example of a properly organized script, see Script File Example
     220For an example of a properly organized script, see \emph{Script File Example}
    210221(\S\ref{foo.pl}).
    211222
    212223\subsubsection{Perl Modules}
    213224
    214 Module files should have the following order:
     225Module files should have, in the following order:
    215226
    216227\begin{itemize}
     
    262273\end{itemize}
    263274
    264 For an example of a properly organized module, see Module File Example
     275For an example of a properly organized module, see \emph{Module File Example}
    265276(\S\ref{Foo.pm}).
    266277
     
    308319\end{itemize}
    309320
    310 For an example of a properly organized Pod, see Pod File Example
     321For an example of a properly organized Pod, see \emph{Pod File Example}
    311322(\S\ref{Foo.pod}).
    312323
     
    345356\item Prefer higher-level breaks to lower-level breaks.
    346357
    347 \item Indent new lines 4 spaces deeper than the nesting depth of the line on
    348 which the expression began.
     358\item Indent new lines 1 indentation unit deeper than the nesting depth of the
     359line on which the expression began.
    349360
    350361\item If it is convenient to list one statement per line (similar to a hash
     
    378389\end{verbatim}
    379390
    380 Here are three acceptable ways to format ternary expressions:
     391Below are three acceptable ways to format ternary expressions.
     392\emph{Expression fragments should be indented such that \code{?} and \code{:}
     393are aligned vertically (as opposed to being indented a fixed number of
     394indentation units).}
    381395
    382396\begin{verbatim}
     
    399413
    400414Comments should not be used to comment out large sections of code.  When
    401 deactivating 2 or fewer consecutive lines of code a double hash (\code{##})
     415deactivating 3 or fewer consecutive lines of code a double hash (\code{##})
    402416should be used at the very beginning of the line(s).
    403417
     
    410424\end{verbatim}
    411425
    412 When deactivating 3 or more lines of code an embedded Pod statement should be
     426When deactivating 4 or more lines of code an embedded Pod statement should be
    413427used.  The opening statement should be \code{=for off} and the closing
    414428statement should be \code{=cut}.  The deactivated section should remain nested
     
    478492\begin{verbatim}
    479493    # this is the start of a multi-line comment that continues across a couple of
    480     # lines then has a logical separation before another comment
     494    # lines, then has a logical separation before another comment
    481495    #
    482496    # this is an adjoined comment
     
    491505separated from the closing semi-colon by a single space.
    492506
    493 Here's an example of a trailing comment in Perl code:
     507Here's an example trailing comments:
    494508
    495509\begin{verbatim}
     
    512526
    513527Plain Old Documentation format or Pod will be used to produce documentation of
    514 the subroutines and variables in the exposed API.  See Plain Old Documentation
    515 (\S\ref{pod}).
    516 
    517 If you need to give information about a interface, or variable that isn't
     528the subroutines and variables in the exposed API.  See \emph{Plain Old
     529Documentation} (\S\ref{pod}).
     530
     531If you need to give information about an interface, or variable that isn't
    518532appropriate for Podification, use a normal implementation single-line or
    519 multi-line comment immediately \textit{before} the code (as exampled above).
     533multi-line comment immediately \emph{before} the code (as exampled above).
    520534
    521535
     
    536550
    537551\begin{verbatim}
    538     my ($level, $size);
     552    my ($level, $size);                     # AVOID!
    539553\end{verbatim}
    540554
     
    588602\end{verbatim}
    589603
    590 Consider using the \code{qw} operator when assigning strings to an array.
     604Consider using the \code{qw} operator to avoid repetitive string quoting when
     605assigning to an array.
    591606
    592607\begin{verbatim}
     
    643658\end{verbatim}
    644659
    645 \emph{This restriction does not apply to Perl's built in variables.}
     660\emph{This restriction does not apply to Perl's built-in variables or the
     661package variable used by some modules to control global behavior.}
    646662
    647663\subsection{Package Variables}
    648664
    649 See When to Make Symbols Global (\S\ref{globals}).
     665See \emph{When to Make Symbols Global} (\S\ref{globals}).
    650666
    651667\subsection{Initialization}
     
    658674
    659675\item
    660 Do not initialize variables with an empty string or list - this is pointless.
     676Do not initialize variables with an empty string or list; this is pointless.
    661677
    662678\item
     
    675691This allows them to be initialized as they are created, and naturally
    676692associates their declaration with their use.
     693
     694Practical examples of when \emph{not} to place variable declarations at the top
     695of a block include loop iterators, intermediate value variables, and
     696variables used for returning values from nested scopes.
    677697
    678698\subsection{Subroutine Declarations}
     
    752772where the two actions are intimately related.
    753773
     774\subsubsection{return Statements}
     775
     776A \code{return} statement should not use parentheses unless they make the
     777return value more obvious in some way.  All subroutines must have an explicit,
     778non-null, return statement unless they are returning an error condition, in
     779which case a bare \code{return} is acceptable.   Example:
     780
     781\begin{verbatim}
     782    return $a;                              # Correct
     783    return;                                 # Correct only if indicating an error
     784    return $myDisk->size;                   # Correct
     785    return ($size ? $size : $defaultSize);  # Correct
     786    return($a);                             # AVOID!
     787    return ($a);                            # AVOID!
     788\end{verbatim}
     789
     790\emph{In the absence of an explicit return statement Perl will return the
     791results of the last statement executed.  Relying on this behavior is likely to
     792confuse maintenance programmers and therefore should be avoided.}
     793
     794Try to make the structure of your program match the intent. Example:
     795
     796\begin{verbatim}
     797    if (booleanExpression) {
     798        return true;
     799    } else {
     800        return false;
     801    }
     802\end{verbatim}
     803
     804should instead be written as
     805
     806\begin{verbatim}
     807    return booleanExpression;
     808\end{verbatim}
     809
     810If you're concerned that the reader may not know that \code{booleanExpression}
     811is boolean, use:
     812
     813\begin{verbatim}
     814    return (booleanExpression ? true : false);
     815\end{verbatim}
     816
     817Similarly,
     818
     819\begin{verbatim}
     820    if (condition) {
     821        return $x;
     822    }
     823    return $y;
     824\end{verbatim}
     825
     826should be written as
     827
     828\begin{verbatim}
     829    return (condition ? x : y);
     830\end{verbatim}
     831
    754832\subsection{Compound Statements}
    755833
     
    765843\end{itemize}
    766844
    767 \subsection{return Statements}
    768 
    769 A \code{return} statement with a value should not use parentheses unless they
    770 make the return value more obvious in some way.  All subroutines must have an
    771 explicit, non-null, return statement.   Example:
    772 
    773 \begin{verbatim}
    774     return undef;                           # Correct
    775     return;                                 # AVOID!
    776     return $myDisk->size;                   # Correct
    777     return ($size ? $size : $defaultSize);  # Correct
    778 \end{verbatim}
    779 
    780 \emph{In the absence of an explicit return statement Perl will return the
    781 results of the last statement executed.  A null \code{return} statement also
    782 causes the result of the last statement executed to be returned.  Relying on
    783 this behavior is likely to confuse maintenance programmers and therefore should
    784 be avoided.}
    785 
    786 \subsection{if, if-else, if-elsif-else Statements}
    787 
    788 \code{if} statements must always use parentheses \code{()} around conditionals.
     845\subsubsection{if, if-else, if-elsif-else Statements}
    789846
    790847The \code{if-else} class of statements should have the following form:
     
    810867\end{verbatim}
    811868
    812 \subsection{C style for Statements}
     869\emph{The same formatting applies to \code{unless}, \code{unless-else}, \&
     870\code{unless-elsif-else} statements.}
     871
     872\subsubsection{C style for Statements}
    813873
    814874A \code{for} statement should have the following form:
     
    829889
    830890An empty \code{for} statement (one in which all the work is done in the
    831 initialization, condition, and update clauses) should have the following forms:
     891initialization, condition, and update clauses) should have the following form:
    832892
    833893\begin{verbatim}
     
    841901the loop (for the update clause).
    842902
    843 \subsection{Perl style for, foreach Statements}
     903\emph{Generally, ``Perlish'' style loops are preferred over the C form.}
     904
     905\subsubsection{Perl style for, foreach Statements}
    844906
    845907A Perlish \code{for} or \code{foreach} statement should have the following
     
    848910\begin{verbatim}
    849911    foreach my $item (@workingList) {
    850         # do stuff with $item
     912        statements;
     913    }
     914
     915    foreach my $item (@workingList) {
     916        statements;
     917    } continue {
     918        statements;
    851919    }
    852920\end{verbatim}
    853921
    854922The current working item should be interacted with through a declared lexical
    855 variable and  not via \code{$_}.
     923variable and not via \code{$_}.
    856924
    857925\begin{verbatim}
     
    861929\end{verbatim}
    862930
    863 \subsection{while Statements}
     931\subsubsection{while Statements}
    864932
    865933A \code{while} statement should have the following form:
     
    869937        statements;
    870938    }
     939
     940    while (condition) {
     941        statements;
     942    } continue {
     943        statements;
     944    }
     945\end{verbatim}
     946
     947Do \emph{not} use \code{$_} when iterating through a filehandle.  Instead, place
     948each ``line'' of the filehandle into a named lexical variable.
     949
     950\begin{verbatim}
     951    my $line;
     952    while (defined($line = <FILE>)) {
     953        statements;
     954    }
    871955\end{verbatim}
    872956
     
    877961\end{verbatim}
    878962
    879 \subsection{do-while Statements}
    880 
    881 A \code{do-while} statement should have the following form:
     963\emph{Consider using the \code{map} subroutine instead of a loop for list
     964generation.}
     965
     966\subsubsection{do Statements}
     967
     968In Perl \code{do} is a built-in subroutine.  Traditional \code{do-while} like
     969statements may be constructed with it by using Statement Modifiers (see
     970\S\ref{statement-modifiers}) in the following form:
    882971
    883972\begin{verbatim}
    884973    do {
    885974        statements;
    886     } while (condition);
    887 \end{verbatim}
    888 
    889 \subsection{Label Statements}
    890 
    891 Code labels should be indented to align with the previous level of
    892 indentation.
     975    } statement-modifier (condition);
     976\end{verbatim}
     977
     978\subsubsection{Label Statements}
     979
     980Code labels should be indented to align with the previous level of indentation.
     981Do \emph{not} label a loop unless you are going to use the label.
    893982
    894983\begin{verbatim}
     
    900989\end{verbatim}
    901990
    902 \subsection{Blocks}
    903 
    904 The contense of a block should be idented one level.
     991\subsubsection{Blocks}
     992
     993The contents of a block should be indented one level.
    905994
    906995\begin{verbatim}
     
    9121001\end{verbatim}
    9131002
     1003\subsection{Statement Modifiers}
     1004\label{statement-modifiers}
     1005
     1006These keywords are considered acceptable for use as ``Statement Modifiers'':
     1007\code{if}, \code{unless} \& \code{until}.  These keywords should generally be
     1008avoided for that purpose: \code{while} \& \code{foreach}.
     1009
     1010Statement Modifiers should only be used \emph{only} when it makes the intent
     1011for the code more obvious.  e.g.
     1012
     1013\begin{verbatim}
     1014    die "you forgot to handle some case" unless $flag;  # Correct
     1015    $a = 1 if (($b == $c) && defined($d);               # AVOID!
     1016\end{verbatim}
     1017
     1018Parenthesis should be used with complex expressions.
     1019
     1020\begin{verbatim}
     1021    someSubCall($a, $b, $c) if ($foo eq 'bar' || $baz > 10);    # Correct
     1022    someSubCall($a, $b, $c) if $foo eq 'bar' || $baz > 10;      # AVOID!
     1023\end{verbatim}
     1024
    9141025
    9151026%------------------------------------------------------------------------------
    9161027
    9171028\section{White Space}
     1029
     1030In general, be very liberal in the use of whitespace, both horizontal and
     1031vertical, if it will improve the clarity and readability of your code.
     1032
    9181033\subsection{Blank Lines}
    9191034
     
    9211036are logically related.
    9221037
    923 Two blank lines should always be used in the following circumstances:
    924 \begin{itemize}
    925 \item
    926 Between sections of a source file
    927 \end{itemize}
     1038Two blank lines should always be used between sections of a source file.
    9281039
    9291040One blank line should always be used in the following circumstances:
     
    9921103\end{itemize}
    9931104
    994 Blank spaces should \textit{not} be used in the following circumstances:
     1105Blank spaces should \emph{not} be used in the following circumstances:
    9951106
    9961107\begin{itemize}
     
    10271138
    10281139The filenames of all scripts should be verbal phrases, in mixed case.  The
    1029 capitalized \code{.PL} suffix is reserved for ``build'' scripts.
     1140capitalized \code{.PL} suffix is reserved for build and configuration scripts.
    10301141
    10311142&
     
    10831194of a variable name should be mnemonic- that is, designed to indicate to the
    10841195casual observer the intent of its use. One-character variable names should be
    1085 avoided except for temporary \textit{throwaway} variables. Common names for
     1196avoided except for temporary \emph{throwaway} variables. Common names for
    10861197temporary variables are \code{$i}, \code{$j}, \code{$k}, \code{$m}, and \code{$n}
    10871198for integers; \code{$c}, \code{$d}, and \code{$e} for characters.
     
    12381349\end{verbatim}
    12391350
    1240 \subsubsection{Returning Values}
    1241 
    1242 Try to make the structure of your program match the intent. Example:
    1243 
    1244 \begin{verbatim}
    1245     if (booleanExpression) {
    1246         return true;
    1247     } else {
    1248         return false;
    1249     }
    1250 \end{verbatim}
    1251 
    1252 should instead be written as
    1253 
    1254 \begin{verbatim}
    1255     return booleanExpression;
    1256 \end{verbatim}
    1257 
    1258 If you're concerned that the reader may not know that \code{booleanExpression}
    1259 is boolean, use:
    1260 
    1261 \begin{verbatim}
    1262     return (booleanExpression ? true : false);
    1263 \end{verbatim}
    1264 
    1265 Similarly,
    1266 
    1267 \begin{verbatim}
    1268     if (condition) {
    1269         return $x;
    1270     }
    1271     return $y;
    1272 \end{verbatim}
    1273 
    1274 should be written as
    1275 
    1276 \begin{verbatim}
    1277     return (condition ? x : y);
    1278 \end{verbatim}
    1279 
    12801351\subsubsection{Expressions before `?' in the Conditional Operator }
    12811352
     
    12981369\end{verbatim}
    12991370
    1300 \subsubsection{FileHandles}
     1371\subsubsection{Filehandles}
    13011372
    13021373Do not use ``bare-words'' as filehandles except for \code{STDIN}, \code{STDOUT},
     
    13831454\subsubsection{Inheritance}
    13841455
    1385 Do not use the \code{@ISA} package variable for subclassing.  Instead, use the
     1456Do not use the \code{@ISA} package variable for sub-classing.  Instead, use the
    13861457\code{base} pragma.
    13871458
     
    14391510
    14401511\item
    1441 Consider using the \code{q} and \code{qq} operators instead of backslashes
    1442 (\code{\}) when quoting strings that contain other quotes or blackslases.
     1512 Consider using the \code{q} and \code{qq} operators instead of backslashes
     1513(\code{\}) when quoting strings that contain other quotes or backslashes.
    14431514
    14441515\item
    14451516Consider using the \code{quotemeta} subroutine when handling strings that
    1446 contain meta-characters.
    1447 \end{itemize}
     1517contain meta-characters.  \end{itemize}
    14481518
    14491519\subsubsection{String Concatenation}
     
    14831553
    14841554\begin{verbatim}
    1485     sub myopen (*;$)                    # AVOID!
     1555    sub myopen (*;$);                   # AVOID!
     1556    sub myopen (*;$) { }                # AVOID!
    14861557\end{verbatim}
    14871558
     
    15431614http://search.cpan.org/~drolsky/Params-Validate/} is a Perl module that allows
    15441615you to perform rigorous subroutine parameter validation.  It is highly
    1545 configurable and validation may be deactivated at runtime
     1616configurable and validation may be deactivated at runtime.
    15461617
    15471618The basic premise is that a validation specification is passed to one of
Note: See TracChangeset for help on using the changeset viewer.