IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 12, 2004, 11:14:27 AM (22 years ago)
Author:
jhoblitt
Message:

add limitations blurb
converted Programming Practices

File:
1 edited

Legend:

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

    r2336 r2349  
    1 %%% $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $
     1%%% $Id: perlCodeConventions.tex,v 1.6 2004-11-12 21:14:27 jhoblitt Exp $
    22\documentclass[panstarrs]{panstarrs}
    33
     
    6464Such an adaption is explicitly permitted by the Sun Microsystems
    6565copyright notice (Copyright.doc.html).
     66
     67\subsection{Limitations}
     68
     69Although Perl's syntax is in many ways similar to that of C or Java, there are
     70many signification deviations.  This has lead to a coding style within the Perl
     71community that is generally significantly different then that used for C or
     72Java (See the \code{perlstyle} Pod in the standard Perl documentation for an
     73example of this).  This document does not attempt to define the coding style
     74common with Perl programmers.  Instead, it is an attempt to make the C and Perl
     75coding styles as consistent as is reasonably feasible within the Pan-STARRS
     76project.
    6677
    6778\subsection{To whom should I Complain?}
     
    155166# Copyright (C) 2004  Joshua Hoblitt
    156167#
    157 # $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $
     168# $Id: perlCodeConventions.tex,v 1.6 2004-11-12 21:14:27 jhoblitt Exp $
    158169
    159170use 5.008005;   # optional
     
    212223# Copyright (C) 2004  Joshua Hoblitt
    213224#
    214 # $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $
     225# $Id: perlCodeConventions.tex,v 1.6 2004-11-12 21:14:27 jhoblitt Exp $
    215226
    216227package Foo;
     
    811822Perl's \code{constant} pragma should not be used as it merely generates
    812823subroutines with null prototypes.  'Constants' created this way can not be
    813 interpolated instead of strings.  Instead, use Lexical varibles but with all
     824interpolated instead of strings.  Instead, use Lexical variables but with all
    814825capital letters.
    815826
     
    841852\section{Programming Practices}
    842853
    843 %\subsection{When to Make Symbols Global}
    844 
    845 %Declare all subroutines and top-level variables \code{static} within a
    846 %file if they are not needed outside of the file.  {\bf Note:} do not
    847 %confuse this use of \code{static} with its usage to make
    848 %auto-variables within a subroutine persistent.
    849 
    850 %A name (whether of a variable, a subroutine, or a type) shall start
    851 %\code{ps} (or \code{p_ps}) if and only if it is visible at global
    852 %scope.  The distinction is that \code{p_ps} names are not part of the
    853 %documented APIs, but need to be exposed for some reason.
     854\subsection{When to Make Symbols Global}
     855
     856Only declare variables as package variables (\code{our}) if they are going to
     857be exported into another namespace.  All other variables within a namespace
     858should be declared as lexical.
     859
     860\begin{verbatim}
     861package someModule;
     862
     863use base qw( Exporter );
     864
     865our @EXPORT_OK = qw( $foo $bar );
     866
     867\end{verbatim}
    854868
    855869\subsection{Constants}
     
    889903performance. This is the job of the compiler. Example:
    890904\begin{verbatim}
    891 d = (a = b + c) + r;                    // AVOID!
     905$d = ($a = $b + $c) + $r;               # AVOID!
    892906\end{verbatim}
    893907
    894908should be written as
    895909\begin{verbatim}
    896 a = b + c;
    897 d = a + r;
     910$a = $b + $c;
     911$d = $a + $r;
    898912\end{verbatim}
    899913
     
    909923
    910924\begin{verbatim}
    911 if (a & b || c & d)                     // AVOID!
    912 if ((a & b) || (c & d))                 // RIGHT
     925if ($a & $b || $c & $d)                 # AVOID!
     926if (($a & $b) || ($c & $d))             # RIGHT
    913927\end{verbatim}
    914928
    915929There are some famous problems with precedence in C.  In particular,
    916 expressions involving the combinations of \code{||} and
    917 \code{&&} should be fully parenthesised, as should all
    918 expressions containing bitwise operators:
    919 \begin{verbatim}
    920 if ((a == b && c == d) || e == f)
    921 l << (j + k)
    922 (l << j) + k
    923 (i & b) | c
     930expressions involving the combinations of \code{||} and \code{&&} should be
     931fully parenthesized, as should all expressions containing bitwise operators:
     932
     933\begin{verbatim}
     934if (($a == $b && $c == $d) || $e == $f)
     935$l << ($j + $k)
     936($l << $j) + $k
     937($i & $b) | $c
    924938\end{verbatim}
    925939
     
    927941
    928942Try to make the structure of your program match the intent. Example:
     943
    929944\begin{verbatim}
    930945if (booleanExpression) {
     
    934949}
    935950\end{verbatim}
     951
    936952should instead be written as
     953
    937954\begin{verbatim}
    938955return booleanExpression;
    939956\end{verbatim}
    940 If you're concerned that the reader may not know that
    941 \code{booleanExpression} is boolean, use:
     957
     958If you're concerned that the reader may not know that \code{booleanExpression}
     959is boolean, use:
     960
    942961\begin{verbatim}
    943962return (booleanExpression ? true : false);
     
    945964
    946965Similarly,
     966
    947967\begin{verbatim}
    948968if (condition) {
    949     return x;
    950 }
    951 return y;
     969    return $x;
     970}
     971return $y;
    952972\end{verbatim}
    953973should be written as
     
    958978\subsubsection{Expressions before `?' in the Conditional Operator }
    959979
    960 If an expression containing a binary operator appears before the \code{?} in the ternary \code{?: }operator, it should be parenthesized. Example:
    961 \begin{verbatim}
    962 (x >= 0) ? x : -x;
    963 \end{verbatim}
    964 
    965 \subsubsection{Subroutines that Take No Parameters}
    966 
    967 When a subroutine takes no arguments, it should be explicitly declared
    968 as \code{void}:
    969 \begin{verbatim}
    970 static type mySubroutine(void)
    971 {
    972     ...
    973 }
    974 \end{verbatim}
    975 
    976 
    977 \subsubsection{Special Comments}
    978 
    979 Doxygen has special comments which are used to provide specific notes
    980 in the code.  These are added in as special entries and sections in
    981 the Doxygen-generated documentation.  These special comments should be
    982 used in addition to the Doxygen usage to make these types of
    983 conditions easily searchable.
    984 
    985 \begin{itemize}
    986 \item
    987 Use \code{\warning} in a comment to flag something that is bogus but works.
    988 
    989 \item
    990 Use \code{\bug} to flag something that is bogus and broken.
    991 
    992 \item
    993 Use \code{\todo} to note additional work to be done
    994 
    995 \item
    996 Use \code{\note} to make other general notes
    997 
    998 \item
    999 Use \code{\test} to list test cases
    1000 
    1001 \item
    1002 Use \code{\notreached} to indicate a line of code that cannot be reached,
    1003 e.g.
    1004 \begin{verbatim}
    1005 if (sqrt(x) < 0) {
    1006     psAbort();                            // never returns
    1007     exit(1);                              // NOTREACHED
    1008 }
    1009 \end{verbatim}
    1010 
    1011 \item
    1012 Use \code{\notused} to indicate unused arguments to a subroutine:
    1013 e.g.
    1014 \begin{verbatim}
    1015 type psWorkHard(const Region *restrict reg, // Region to operate on
    1016                 int myConst,            // magic value
    1017                 int magicNumber         // NOTUSED; reserve for next version
    1018                 )
    1019 {
    1020    ...
    1021 }
    1022 \end{verbatim}
    1023 \end{itemize}
     980If an expression containing a binary operator appears before the \code{?} in
     981the ternary \code{?: }operator, it should be parenthesized. Example:
     982
     983\begin{verbatim}
     984($x >= 0) ? $x : -$x;
     985\end{verbatim}
    1024986
    1025987%------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.