IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Nov 10, 2004, 6:02:34 PM (22 years ago)
Author:
jhoblitt
Message:

converted white space and naming conventions

File:
1 edited

Legend:

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

    r2334 r2336  
    1 %%% $Id: perlCodeConventions.tex,v 1.4 2004-11-11 02:27:56 jhoblitt Exp $
     1%%% $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $
    22\documentclass[panstarrs]{panstarrs}
    33
     
    155155# Copyright (C) 2004  Joshua Hoblitt
    156156#
    157 # $Id: perlCodeConventions.tex,v 1.4 2004-11-11 02:27:56 jhoblitt Exp $
     157# $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $
    158158
    159159use 5.008005;   # optional
     
    212212# Copyright (C) 2004  Joshua Hoblitt
    213213#
    214 # $Id: perlCodeConventions.tex,v 1.4 2004-11-11 02:27:56 jhoblitt Exp $
     214# $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $
    215215
    216216package Foo;
     
    698698
    699699\begin{itemize}
    700 
    701700\item Between the local variables in a subroutine and its first statement
    702701
     
    708707Blank spaces should be used in the following circumstances:
    709708\begin{itemize}
    710 
    711 \item A keyword followed by a parenthesis should be separated by a space. Example:
     709\item
     710A keyword followed by a parenthesis should be separated by a space. Example:
     711
    712712\begin{verbatim}
    713713while (true) {
     
    716716\end{verbatim}
    717717
    718 Note that a blank space should not be used between a subroutine name and
    719 its opening parenthesis. This helps to distinguish keywords from
    720 subroutine calls.
    721 
    722 \item A blank space should appear after commas in argument lists.
    723 
    724 \item
    725 Binary operators should be separated from their operands by spaces.
    726 Blank spaces should never separate unary operators such as a type
    727 cast, unary minus, increment (\code{++}), and decrement (\code{--})
    728 from their operands. Examples:
    729 \begin{verbatim}
    730     a += c + d;
    731     a = (a + b) / (c * d);
     718Note that a blank space should not be used between a subroutine name and its
     719opening parenthesis. This helps to distinguish keywords from subroutine calls.
     720
     721\item
     722A blank space should appear after commas in argument lists.
     723
     724\item
     725Binary operators should be separated from their operands by spaces.  Blank
     726spaces should never separate unary operators such as a type cast, unary minus,
     727increment (\code{++}), and decrement (\code{--}) from their operands. Examples:
     728
     729\begin{verbatim}
     730    $a += $c + $d;
     731    $a = ($a + $b) / ($c * $d);
    732732   
    733     while (*d++ = *s++) {                 // Tricky way of copying until '\0'
    734         n++;
     733    while ($d++ = $s++) {
     734        $n++;
    735735    }
    736     printf ("size is %d\n", foo);
    737 \end{verbatim}
    738 
    739 \item The expressions in a \code{for} statement should be
    740 separated by blank spaces. Example:
     736    printf("size is %d\n", $foo);
     737\end{verbatim}
     738
     739\item
     740The expressions in a \code{for} statement should be separated by blank spaces.
     741Example:
    741742
    742743\begin{verbatim}
     
    766767\textbf{Examples} \\
    767768
    768 Typedefs &
    769 
    770 Type names should be nouns, in mixed case with the first letter of
    771 each internal word capitalized.  Little words, such as 'for', 'to',
    772 'at', etc, may be written in all lower-case or separated with
    773 underscores if it makes the name clearer.
    774 
    775 If and only if the type is visible at global scope, the
    776 type name should start with \code{ps}.
    777 
    778 Try to keep your type names simple and descriptive. Use whole
    779 words-avoid acronyms and abbreviations (unless the abbreviation is
    780 much more widely used than the long form, such as URL or HTML).
    781 
    782  &
    783 typedef struct {...} psRaster;\hfil\break
    784 typedef struct {...} psImage;
     769Subroutines &
     770
     771The names of all public subroutines (and methods) should be verbal phrases, in
     772mixed case.
     773
     774Private subroutines (and methods) should follow the same rules as public
     775subroutines but be prefixed with an underscore.
     776
     777&
     778
     779\code{run();}\hfil\break
     780\code{runFast();}\hfil\break
     781\code{getBackground();}\hfil\break
     782
    785783\\
    786784
    787 Subroutines &
    788 
    789 The names of all externally visible subroutines (i.e. all those that are
    790 not declared \code{static}) should be verbal phrases, in mixed case.
    791 
    792 Namespaces should be protected by using special naming prefixes to
    793 restrict the name space in particular libraries.  For example, the
    794 PSLib subroutines are all prefixed with \code{ps}.
     785Variables &
     786
     787Except for variables, all instance, class, and class constants are in mixed
     788case with Variable names should not start with underscore \code{_} as these
     789names are, under some circumstances, reserved by Perl community convention.
     790
     791Variable names should be short yet meaningful. The choice of a variable name
     792should be mnemonic- that is, designed to indicate to the casual observer the
     793intent of its use. One-character variable names should be avoided except for
     794temporary \textit{throwaway} variables. Common names for temporary variables
     795are \code{i}, \code{j}, \code{k}, \code{m}, and \code{n} for integers;
     796\code{c}, \code{d}, and \code{e} for characters.
    795797
    796798&
    797799
    798 \code{psRun(int ID);}\hfil\break
    799 \code{psRunFast(float velocity);}\hfil\break
    800 \code{psGetBackground(void);}\hfil\break
     800\code{$i;}\hfil\break
     801\code{$c;}\hfil\break
     802\code{$myWidth;}\hfil\break
    801803\hfil\break
    802 \code{p_psForgeSignature(const char *name);}
    803 
    804 \\
    805 
    806 Variables &
    807 
    808 Except for variables, all instance, class, and class constants are in
    809 mixed case with
    810 
    811 Variable names should not start with underscore \code{_} as these
    812 names are, under some circumstances, reserved by the C
    813 standard. Non-globally visible words should start with a lowercase
    814 first letter; Internal words start with capital letters.
    815 
    816 Variable names should be short yet meaningful. The choice of a
    817 variable name should be mnemonic- that is, designed to indicate to the
    818 casual observer the intent of its use. One-character variable names
    819 should be avoided except for temporary \textit{throwaway} variables. Common
    820 names for temporary variables are \code{i}, \code{j},
    821 \code{k}, \code{m}, and \code{n} for integers;
    822 \code{c}, \code{d}, and \code{e} for characters.
    823 
    824 &
    825 \code{int i;}\hfil\break
    826 \code{char c;}\hfil\break
    827 \code{float myWidth;}\hfil\break
    828 \hfil\break
    829 \code{int pseudo;}\hfil\break
    830 \code{int p_psMyFiddleFactor;}\hfil\break
     804\code{$pseudo;}\hfil\break
     805\code{$myFiddleFactor;}\hfil\break
    831806
    832807\\
    833808 
    834 Constants
     809Constants &
     810
     811Perl's \code{constant} pragma should not be used as it merely generates
     812subroutines with null prototypes.  'Constants' created this way can not be
     813interpolated instead of strings.  Instead, use Lexical varibles but with all
     814capital letters.
    835815
    836816&
    837 
    838 Constants used to e.g. dimension arrays should be set using the
    839 \code{#define}; \code{const} variables should not be used, especially
    840 in header files.  Symbolic values should usually be declared using
    841 enumerated types.
     817\code{my $MAXLEN = 40;}\hfil\break
     818\code{my @LENGTH = (2, 4, 8);}\hfil\break
     819\code{my %CLASS = (raw => 1, processed => 2);}\hfil\break
     820
     821\\
     822
     823Namespaces &
     824
     825Must start with a capitalized letter but otherwise follow the same naming
     826conventions as variables.
    842827
    843828&
    844 \code{#define PS_MAXLEN 40}\hfil\break
    845 \CODE|enum { PS_ONE = 1, PS_TWO = 2 };|\hfil\break
     829
     830\code{Class::Data::Inheritable}\hfil\break
     831\code{Time::HiRes}\hfil\break
     832\code{XML::SimpleObject}\hfil\break
     833
     834\\
    846835
    847836\end{tabular}
     
    852841\section{Programming Practices}
    853842
    854 \subsection{When to Make Symbols Global}
    855 
    856 Declare all subroutines and top-level variables \code{static} within a
    857 file if they are not needed outside of the file.  {\bf Note:} do not
    858 confuse this use of \code{static} with its usage to make
    859 auto-variables within a subroutine persistent.
    860 
    861 A name (whether of a variable, a subroutine, or a type) shall start
    862 \code{ps} (or \code{p_ps}) if and only if it is visible at global
    863 scope.  The distinction is that \code{p_ps} names are not part of the
    864 documented APIs, but need to be exposed for some reason.
     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.
    865854
    866855\subsection{Constants}
     
    870859in a \code{for} loop as counter values. 
    871860
    872 \subsection{Type Qualifiers (const and restrict)}
    873 
    874 All interfaces and type definitions should use \code{const} and
    875 \code{restrict} wherever appropriate. For example,
    876 \begin{verbatim}
    877 typedef struct {
    878     int n;
    879     float *restrict vec;
    880 } psVec;
    881 
    882 psVec *psVecAdd(const restrict* psVec s1, const restrict* psVec s2);
    883 \end{verbatim}
    884 
    885 \textit{{\bf Note:} compilers are free to ignore the \code{restrict}
    886 keyword, so all code should be written to explicitly handle aliasing}.
    887 
    888 \subsection{structs and typedefs}
    889 
    890 All structs should be defined as \code{typedef}s.
    891 
    892 A struct should not include a struct
    893 tag unless it's self-referential; E.g.
    894 \begin{verbatim}
    895 typedef struct myStruct {               // Omit "myStruct"
    896     int x;
    897 } myStruct;
    898 
    899 typedef struct yourStruct {             // OK
    900     struct yourStruct *next;
    901     int x;
    902 } yourStruct;
    903 \end{verbatim}
    904 
    905861\subsection{Variable Assignments}
    906862
     
    909865
    910866\begin{verbatim}
    911 row0 = col0 = 0;
    912 sum = sumx = sumy = 0;
    913 \end{verbatim}
    914 
    915 Do not use the assignment operator in a place where it can be easily confused with the equality operator. Example:
    916 \begin{verbatim}
    917 if (c++ = d++) {                        // AVOID!
     867$row0 = $col0 = 0;
     868$sum = $sumx = $sumy = 0;
     869\end{verbatim}
     870
     871Do not use the assignment operator in a place where it can be easily confused
     872with the equality operator. Example:
     873
     874\begin{verbatim}
     875if ($c++ = $d++) {                      # AVOID!
    918876    ...
    919877}
    920878\end{verbatim}
     879
    921880should be written as
    922 \begin{verbatim}
    923 if ((c++ = d++) != 0) {
     881
     882\begin{verbatim}
     883if (($c++ = $d++) != 0) {
    924884    ...
    925885}
Note: See TracChangeset for help on using the changeset viewer.