Changeset 2349
- Timestamp:
- Nov 12, 2004, 11:14:27 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/doc/misc/perlCodeConventions.tex (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/misc/perlCodeConventions.tex
r2336 r2349 1 %%% $Id: perlCodeConventions.tex,v 1. 5 2004-11-11 04:02:34jhoblitt Exp $1 %%% $Id: perlCodeConventions.tex,v 1.6 2004-11-12 21:14:27 jhoblitt Exp $ 2 2 \documentclass[panstarrs]{panstarrs} 3 3 … … 64 64 Such an adaption is explicitly permitted by the Sun Microsystems 65 65 copyright notice (Copyright.doc.html). 66 67 \subsection{Limitations} 68 69 Although Perl's syntax is in many ways similar to that of C or Java, there are 70 many signification deviations. This has lead to a coding style within the Perl 71 community that is generally significantly different then that used for C or 72 Java (See the \code{perlstyle} Pod in the standard Perl documentation for an 73 example of this). This document does not attempt to define the coding style 74 common with Perl programmers. Instead, it is an attempt to make the C and Perl 75 coding styles as consistent as is reasonably feasible within the Pan-STARRS 76 project. 66 77 67 78 \subsection{To whom should I Complain?} … … 155 166 # Copyright (C) 2004 Joshua Hoblitt 156 167 # 157 # $Id: perlCodeConventions.tex,v 1. 5 2004-11-11 04:02:34jhoblitt Exp $168 # $Id: perlCodeConventions.tex,v 1.6 2004-11-12 21:14:27 jhoblitt Exp $ 158 169 159 170 use 5.008005; # optional … … 212 223 # Copyright (C) 2004 Joshua Hoblitt 213 224 # 214 # $Id: perlCodeConventions.tex,v 1. 5 2004-11-11 04:02:34jhoblitt Exp $225 # $Id: perlCodeConventions.tex,v 1.6 2004-11-12 21:14:27 jhoblitt Exp $ 215 226 216 227 package Foo; … … 811 822 Perl's \code{constant} pragma should not be used as it merely generates 812 823 subroutines with null prototypes. 'Constants' created this way can not be 813 interpolated instead of strings. Instead, use Lexical vari bles but with all824 interpolated instead of strings. Instead, use Lexical variables but with all 814 825 capital letters. 815 826 … … 841 852 \section{Programming Practices} 842 853 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 856 Only declare variables as package variables (\code{our}) if they are going to 857 be exported into another namespace. All other variables within a namespace 858 should be declared as lexical. 859 860 \begin{verbatim} 861 package someModule; 862 863 use base qw( Exporter ); 864 865 our @EXPORT_OK = qw( $foo $bar ); 866 867 \end{verbatim} 854 868 855 869 \subsection{Constants} … … 889 903 performance. This is the job of the compiler. Example: 890 904 \begin{verbatim} 891 d = (a = b + c) + r; //AVOID!905 $d = ($a = $b + $c) + $r; # AVOID! 892 906 \end{verbatim} 893 907 894 908 should be written as 895 909 \begin{verbatim} 896 a = b +c;897 d = a +r;910 $a = $b + $c; 911 $d = $a + $r; 898 912 \end{verbatim} 899 913 … … 909 923 910 924 \begin{verbatim} 911 if ( a & b || c & d) //AVOID!912 if (( a & b) || (c & d)) //RIGHT925 if ($a & $b || $c & $d) # AVOID! 926 if (($a & $b) || ($c & $d)) # RIGHT 913 927 \end{verbatim} 914 928 915 929 There 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) +k923 ( i & b) |c930 expressions involving the combinations of \code{||} and \code{&&} should be 931 fully parenthesized, as should all expressions containing bitwise operators: 932 933 \begin{verbatim} 934 if (($a == $b && $c == $d) || $e == $f) 935 $l << ($j + $k) 936 ($l << $j) + $k 937 ($i & $b) | $c 924 938 \end{verbatim} 925 939 … … 927 941 928 942 Try to make the structure of your program match the intent. Example: 943 929 944 \begin{verbatim} 930 945 if (booleanExpression) { … … 934 949 } 935 950 \end{verbatim} 951 936 952 should instead be written as 953 937 954 \begin{verbatim} 938 955 return booleanExpression; 939 956 \end{verbatim} 940 If you're concerned that the reader may not know that 941 \code{booleanExpression} is boolean, use: 957 958 If you're concerned that the reader may not know that \code{booleanExpression} 959 is boolean, use: 960 942 961 \begin{verbatim} 943 962 return (booleanExpression ? true : false); … … 945 964 946 965 Similarly, 966 947 967 \begin{verbatim} 948 968 if (condition) { 949 return x;950 } 951 return y;969 return $x; 970 } 971 return $y; 952 972 \end{verbatim} 953 973 should be written as … … 958 978 \subsubsection{Expressions before `?' in the Conditional Operator } 959 979 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} 980 If an expression containing a binary operator appears before the \code{?} in 981 the ternary \code{?: }operator, it should be parenthesized. Example: 982 983 \begin{verbatim} 984 ($x >= 0) ? $x : -$x; 985 \end{verbatim} 1024 986 1025 987 %------------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.
