IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 2332


Ignore:
Timestamp:
Nov 10, 2004, 3:55:58 PM (22 years ago)
Author:
jhoblitt
Message:

converted the comments and declaration sections
various formatting fixes and terminology changes

File:
1 edited

Legend:

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

    r2315 r2332  
    1 %%% $Id: perlCodeConventions.tex,v 1.1 2004-11-09 03:55:35 jhoblitt Exp $
     1%%% $Id: perlCodeConventions.tex,v 1.2 2004-11-11 01:55:58 jhoblitt Exp $
    22\documentclass[panstarrs]{panstarrs}
    33
     
    155155# Copyright (C) 2004  Joshua Hoblitt
    156156#
    157 # $Id: perlCodeConventions.tex,v 1.1 2004-11-09 03:55:35 jhoblitt Exp $
    158 
    159 use 5.008005;   #optional
     157# $Id: perlCodeConventions.tex,v 1.2 2004-11-11 01:55:58 jhoblitt Exp $
     158
     159use 5.008005;   # optional
    160160
    161161use strict;     # not optional
     
    212212# Copyright (C) 2004  Joshua Hoblitt
    213213#
    214 # $Id: perlCodeConventions.tex,v 1.1 2004-11-09 03:55:35 jhoblitt Exp $
     214# $Id: perlCodeConventions.tex,v 1.2 2004-11-11 01:55:58 jhoblitt Exp $
    215215
    216216package Foo;
    217217
    218 use 5.008005;   #optional
     218use 5.008005;   # optional
    219219
    220220use strict;     # not optional
     
    279279squished up against the right margin, just indent 8 spaces
    280280instead.\footnote{This should be \emph{very} rare! Consider whether
    281 you should be e.g. factoring code into a function.}
     281you should be e.g. factoring code into a subroutine.}
    282282
    283283\end{itemize}
    284284
    285 Here are some examples of breaking function calls:
    286 
    287 \begin{verbatim}
    288 someFunc(longExpression1, longExpression2, longExpression3,
     285Here are some examples of breaking subroutine calls:
     286
     287\begin{verbatim}
     288someSub(longExpression1, longExpression2, longExpression3,
    289289         longExpression4, longExpression5);
    290290 
    291 $var = someFunc1(longExpression1,
    292                  someFunc2(longExpression2,
     291$var = someSub(longExpression1,
     292                 someSub(longExpression2,
    293293                          longExpression3));
    294294\end{verbatim}
     
    322322\section{Comments}
    323323
    324 Comments should not be used to `comment out code'; use \code{#if 0} instead:
    325 \begin{verbatim}
    326 #if 0
    327     code that is not wanted just at present
    328 #endif
    329 \end{verbatim}
    330 This has two advantages:
    331 \begin{itemize}
    332 \item  The code can easily be reinstated.
    333 \item  Related blocks of code can be removed with
    334 \begin{verbatim}
    335 #define IGNORE_RHL 1
    336 #if !IGNORE_RHL
    337     ...
    338 #endif
    339 ...
    340 #if !IGNORE_RHL
    341     ...
    342 #endif
    343 \end{verbatim}
    344 This type of \code{#define} may appear within the main body of the file.
    345 \end{itemize}
     324\subsection{Deactivating Code}
     325
     326Comments should not be used to comment out large sections of code.  When
     327deactivating 2 or fewer lines of code a \code{#} should be used at the very
     328beginning of the line(s).
     329
     330Example of deactivating a single line code.
     331\begin{verbatim}
     332    if ($foo) {
     333#        print( "foo" );
     334    }
     335\end{verbatim}
     336
     337When deactivating 3 or more lines of code an \code{if} statement should be used
     338with a false boolean value.  The opening and closing of the \code{if} statement
     339must be at the very beginning of the line.
     340
     341Example of deactivating multiple lines of code.
     342\begin{verbatim}
     343if (0) {
     344    if ($foo) {
     345        print( "foo" );
     346    }
     347}
     348\end{verbatim}
    346349
    347350Comments should be used to give overviews of code and provide
     
    370373\subsection{Implementation Comment Formats}
    371374
    372 Programs can have four styles of implementation comments: block,
    373 single-line, trailing, and end-of-line.
    374 
    375 \subsubsection{Block Comments }
    376 \label{ImplBlockComments}
    377 
    378 Block comments are used to provide descriptions of files, functions,
    379 data structures and algorithms. Block comments may be used at the
    380 beginning of each file and before each function. They can also be used
    381 in other places, such as within functions. Block comments inside a
    382 function should be indented to the same level as the code they
    383 describe.
    384 
    385 See also Documentation Comments (\S\ref{DocComments}).
     375Programs can have three styles of implementation comments: single-line,
     376multi-line and trailing.
    386377
    387378\subsubsection{Single-Line Comments}
    388 \label{SingleLineComment}
    389 
    390 Short comments can appear on a single line indented to the level of
    391 the code that follows. If a comment can't be written in a single line,
    392 it should follow the block comment format (\S\ref{ImplBlockComments}).
     379
     380Short comments can appear on a single line indented to the level of the code
     381that follows.  The comment should appear above the code that it is describe and
     382must have a space between the \code{#} and the first text.
     383
     384Example of a single-line comment.
     385\begin{verbatim}
     386# some words
     387\end{verbatim}
     388
     389\subsubsection{Multi-Line Comments}
     390
     391Merely a repitition of single-line comments.
    393392
    394393\subsubsection{Trailing Comments}
    395 \label{col40}
     394\label{col41}
    396395
    397396Very short comments can appear on the same line as the code they
    398 describe, and should be indented to column 40\footnote{In emacs, this
     397describe, and should be indented to column 41\footnote{In emacs, this
    399398may be achieved with \code{ESC;}}. If the code extends beyond this
    400399column, the comment should be separated from the closing semi-colon by
    401400a single space.
    402401
    403 Here's an example of a trailing comment in C code:
    404 \begin{verbatim}
    405 if (a == 2) {
    406     return TRUE;                          /* special case */
     402Here's an example of a trailing comment in Perl code:
     403\begin{verbatim}
     404if ($a == 2) {
     405    return TRUE;                        # special case
    407406} else {
    408     return isPrime(a);                    /* works only for odd a */
    409 }
    410 \end{verbatim}
    411 
    412 \subsubsection{End-Of-Line Comments}
    413 
    414 The \code{//} comment delimiter can comment out a complete line
    415 or only a partial line; as for trailing comments, the comment should
    416 start in column 40.  It shouldn't be used on consecutive multiple
    417 lines for text comments.  Examples of both styles follow:
    418 
    419 \begin{verbatim}
    420 if (foo > 1) {
    421 
    422     // Do a double-flip.
    423     ...
    424 } else {
    425     return false;          // Explain why here.
     407    return isPrime($a);                 # works only for odd a
    426408}
    427409\end{verbatim}
     
    430412\label{DocComments}
    431413
    432 Doxygen\footnote{{\tt  www.doxygen.org}}  will   be  used  to  produce
    433 documentation of the types,  functions and variables without requiring
    434 much  extra  effort  for   the  programmer.   Comments  starting  with
    435 particular characters  (``tags'') are used by Doxygen  to identify the
    436 relevant code to be documented.
    437 
    438 Functions shall be tagged for Doxygenation by pre-pending them with a
    439 block comment (\S\ref{ImplBlockComments}) which starts with a
    440 \code{/**} instead of the usual \code{/*}.
    441 
    442 Variables shall be tagged for Doxygenation by appending their
    443 declaration with an end-of-line comment which starts with a
    444 \code{///<} instead of the usual \code{//}.
    445 
    446 An example of a function definition employing Doxygen-compatible
    447 comments follows:
    448 
    449 \begin{verbatim}
    450 /** This is a really cool function.
    451  *  It does many really cool things.
    452  */
    453 int reallyCoolFunction(int aNumber,     ///< This is a number
    454                        float aRealNumber ///< This is a real number
    455                        )
     414Plain Old Documentation format or Pod will be used to produce documentation of
     415the subroutines and variables in the exposed API.  Pod sections will appear
     416above the section of code that they describe.
     417
     418An example of a Pod section defining the API of a subroutine.
     419
     420\begin{verbatim}
     421=item * reallyCoolSubroutine
     422
     423Accepts a real number and a string.  Does some really cool stuff with it and
     424returns a really cool thing.
     425
     426=cut
     427
     428sub reallyCoolSubroutine
    456429{
    457     char *aString;                      ///< This is a string used to do stuff.
    458 
    459     /* Do really cool stuff */
    460     ...
    461 }
    462 \end{verbatim}
    463 
    464 If you need to give information about a type, interface, or variable
    465 that isn't appropriate for doxygenation, use a normal implementation
    466 block comment (\S\ref{ImplBlockComments}) or single-line comment
    467 (\S\ref{SingleLineComment}) immediately \textit{before} the code (as
    468 exampled above). For example, details about the implementation of a
    469 type should go in in such an implementation block comment
    470 \textit{before} the start of the implementation, not in the doxygen
    471 comment.
     430    my ($aNumber, $aString) = @_;
     431
     432    # Do really cool stuff
     433
     434    return $aThing;
     435}
     436\end{verbatim}
     437
     438If you need to give information about a interface, or variable that isn't
     439appropriate for Podification, use a normal implementation single-line or
     440multi-line comment  immediately \textit{before} the code (as exampled above).
    472441
    473442%------------------------------------------------------------------------------
     
    479448files, but rather in separate, private header files.
    480449
    481 \subsection{Number Per Line}
    482 
    483 One declaration per line is recommended since it encourages
    484 commenting. In other words,
    485 \begin{verbatim}
    486 int level;                              // indentation level
    487 int size;                               // size of table
    488 \end{verbatim}
     450\subsection{Lexical Variables}
     451
     452One declaration per line with a single place between 'my' and the identifier.
     453
     454\begin{verbatim}
     455my $level;                              # indentation level
     456my $size;                               # size of table
     457\end{verbatim}
     458
    489459is preferred over
    490 \begin{verbatim}
    491 int level, size;
    492 \end{verbatim}
     460
     461\begin{verbatim}
     462my ($level, $size);
     463\end{verbatim}
     464
     465In almost all situations with the exception of when parameters are being passed
     466into a subrutine.
     467
    493468Do not put different types on the same line. Example:
    494469\begin{verbatim}
    495 int foo,  fooarray[];                   // WRONG!
    496 \end{verbatim}
    497 
    498 Note: The examples above use one space between the type and the identifier. Another acceptable alternative is to use tabs, e.g.:
    499 \begin{verbatim}
    500 int    level;                           // indentation level
    501 int    size;                            // size of table
    502 Object    currentEntry;                 // currently selected table entry
    503 \end{verbatim}
    504 
    505 (Note that the comments are indented to column 40, as per section~\ref{col40}).
     470my ($foo,  @fooArray);                  # WRONG!
     471\end{verbatim}
    506472
    507473\subsection{Initialization}
     
    521487associates their declaration with their use.
    522488
    523 For example, declarations are naturally mixed with assertions:
    524 \begin{verbatim}
    525 void function(REGION *reg)                      // image of the sky
     489
     490Indexes of loops should usually be declared on the line above loop statement.
     491
     492\begin{verbatim}
     493my $i = 0;
     494for (i < $maxLoops) {
     495    # do stuff
     496
     497    $i++;
     498}
     499\end{verbatim}
     500
     501Avoid \code{local} declarations that hide user declared variables from higher
     502level scopes. For example, do not declare the same variable name in an inner
     503block:
     504
     505\begin{verbatim}
     506my $count;
     507...
     508sub mySubroutine
    526509{
    527     assert (reg != NULL);
    528     const int nrow = reg->nrow;
    529     assert (reg->type == psType_U16);
    530     const psTypeU16 *rows = reg->rows;
     510    local $count;
    531511    ...
    532512}
    533513\end{verbatim}
    534514
    535 Indexes of \code{for} loops should usually
    536 be declared in the \code{for} statement (the declarations appear
    537 in the scope of the braces):
    538 \begin{verbatim}
    539 for (int i = 0; i < maxLoops; i++) {
     515\subsection{Subroutine Declarations}
     516
     517When coding Perl subroutines the following formatting rules should be
     518followed:
     519
     520\begin{itemize}
     521\item
     522Subroutine declarations should be preceeded by a short comment describing what
     523the subroutine does.  These documentation comments should include a brief
     524desription as well as other warnings, bugs, etc. as needed.
     525
     526\item
     527The subroutine body's open brace \CODE.{. should be in column 1 of the next
     528line.
     529
     530\item
     531The first line after the opening brace should declare a list of lexical
     532variables for input parameters and assign \code{@\_} to it.
     533
     534\item
     535Closing brace \CODE.}. starts a line by itself indented to match its
     536corresponding opening statement, except when it is a null statement the
     537\CODE.}. should appear immediately after the \CODE.{..
     538
     539\item
     540The last line before the closing brace must explicity declare a return value.
     541\end{itemize}
     542
     543\begin{verbatim}
     544sub subName
     545{
     546    my ($param1, $param2, ...) = @_;
    540547    ...
    541 }
    542 \end{verbatim}
    543 
    544 Avoid local declarations that hide declarations at higher levels. For
    545 example, do not declare the same variable name in an inner block:
    546 \begin{verbatim}
    547 static int count;
    548 ...
    549 void myFunction(void)
    550 {
    551     if (condition) {
    552     int count = 0;                      // AVOID!
    553     ...
    554     }
    555     ...
    556 }
    557 \end{verbatim}
    558 
    559 \subsection{Function Declarations}
    560 
    561 When coding C functions the following formatting rules should be
    562 followed:
    563 
    564 \begin{itemize}
    565 \item
    566 Function declarations should be preceeded by a short comment
    567 describing what the function does.  These comment blocks should
    568 include doxygen-style comments to provide a brief desription as well
    569 as other warnings, bugs, etc as needed.
    570 
    571 \item The function's type should appear on the same line as the
    572 function declaration.
    573 
    574 \item All arguments should be declared with their types; i.e. no
    575 classic-C declarations like:
    576 \begin{verbatim}
    577 int main(ac, av)
    578          int ac;
    579          char **av;
    580 \end{verbatim}
    581 
    582 \item No space between a function name and the parenthesis \code{(} starting
    583 its parameter list \matchBracket{)}
    584 
    585 \item The function body's open brace \CODE.{. should be in column 1 of the next line:
    586 \begin{verbatim}
    587 void function(void)
    588 {
    589     ...
    590 }
    591 \end{verbatim}
    592 
    593 \item Closing brace \CODE.}. starts a line by itself indented to match its
    594 corresponding opening statement, except when it is a null statement
    595 the \CODE.}. should appear immediately after the \CODE.{..
    596 
    597 \begin{verbatim}
    598 void find_neos(const REGION *sky, const char *descrip)
    599 {
    600     struct {
    601         int x, y;
    602     } work[10];
    603     int ivar;
    604 
    605     while(isspace(*descrip++)) {}         // skip white space
    606     descrip--;                            // one too far. Not great code...
    607 
    608     ...
    609 }
    610 \end{verbatim}
    611 \end{itemize}
     548
     549    return $val;
     550}
     551\end{verbatim}
    612552
    613553%------------------------------------------------------------------------------
     
    799739\begin{itemize}
    800740
    801 \item Between the local variables in a function and its first statement
    802 
    803 \item Between logical sections inside a function to improve readability
     741\item Between the local variables in a subroutine and its first statement
     742
     743\item Between logical sections inside a subroutine to improve readability
    804744\end{itemize}
    805745
     
    816756\end{verbatim}
    817757
    818 Note that a blank space should not be used between a function name and
     758Note that a blank space should not be used between a subroutine name and
    819759its opening parenthesis. This helps to distinguish keywords from
    820 function calls.
     760subroutine calls.
    821761
    822762\item A blank space should appear after commas in argument lists.
     
    854794
    855795Naming conventions make programs more understandable by making them
    856 easier to read. They can also give information about the function of
    857 the identifier -- for example, whether it's a constant, a function, or
     796easier to read. They can also give information about the subroutine of
     797the identifier -- for example, whether it's a constant, a subroutine, or
    858798a type -- which can be helpful in understanding the code.  Remember
    859799these are guidelines for improving readability; clarity should trump
     
    885825\\
    886826
    887 Functions &
    888 
    889 The names of all externally visible functions (i.e. all those that are
     827Subroutines &
     828
     829The names of all externally visible subroutines (i.e. all those that are
    890830not declared \code{static}) should be verbal phrases, in mixed case.
    891831
    892832Namespaces should be protected by using special naming prefixes to
    893833restrict the name space in particular libraries.  For example, the
    894 PSLib functions are all prefixed with \code{ps}.
     834PSLib subroutines are all prefixed with \code{ps}.
    895835
    896836&
     
    954894\subsection{When to Make Symbols Global}
    955895
    956 Declare all functions and top-level variables \code{static} within a
     896Declare all subroutines and top-level variables \code{static} within a
    957897file if they are not needed outside of the file.  {\bf Note:} do not
    958898confuse this use of \code{static} with its usage to make
    959 auto-variables within a function persistent.
    960 
    961 A name (whether of a variable, a function, or a type) shall start
     899auto-variables within a subroutine persistent.
     900
     901A name (whether of a variable, a subroutine, or a type) shall start
    962902\code{ps} (or \code{p_ps}) if and only if it is visible at global
    963903scope.  The distinction is that \code{p_ps} names are not part of the
     
    11031043\end{verbatim}
    11041044
    1105 \subsubsection{Functions that Take No Parameters}
    1106 
    1107 When a function takes no arguments, it should be explicitly declared
     1045\subsubsection{Subroutines that Take No Parameters}
     1046
     1047When a subroutine takes no arguments, it should be explicitly declared
    11081048as \code{void}:
    11091049\begin{verbatim}
    1110 static type myFunction(void)
     1050static type mySubroutine(void)
    11111051{
    11121052    ...
     
    11501090
    11511091\item
    1152 Use \code{\notused} to indicate unused arguments to a function:
     1092Use \code{\notused} to indicate unused arguments to a subroutine:
    11531093e.g.
    11541094\begin{verbatim}
     
    13271267\item
    13281268  Added naming conventions for constructors, destructors, and conversion
    1329   functions.
     1269  subroutines.
    13301270
    13311271\item
     
    13371277
    13381278\item
    1339   Specified that functions taking no arguments should be explicitly
     1279  Specified that subroutines taking no arguments should be explicitly
    13401280  specified as \code{(void)} (avoiding complaints on some compilers,
    13411281  e.g. on SGIs).
     
    14181358(add-hook 'panstarrs-c-mode-hook
    14191359          '(lambda ()
    1420              (set (make-variable-buffer-local 'comment-indent-function)
     1360             (set (make-variable-buffer-local 'comment-indent-subroutine)
    14211361                  'panstarrs-comment-indent)))
    14221362\end{verbatim}
Note: See TracChangeset for help on using the changeset viewer.