Changeset 2336
- Timestamp:
- Nov 10, 2004, 6:02:34 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/doc/misc/perlCodeConventions.tex (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/misc/perlCodeConventions.tex
r2334 r2336 1 %%% $Id: perlCodeConventions.tex,v 1. 4 2004-11-11 02:27:56jhoblitt Exp $1 %%% $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $ 2 2 \documentclass[panstarrs]{panstarrs} 3 3 … … 155 155 # Copyright (C) 2004 Joshua Hoblitt 156 156 # 157 # $Id: perlCodeConventions.tex,v 1. 4 2004-11-11 02:27:56jhoblitt Exp $157 # $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $ 158 158 159 159 use 5.008005; # optional … … 212 212 # Copyright (C) 2004 Joshua Hoblitt 213 213 # 214 # $Id: perlCodeConventions.tex,v 1. 4 2004-11-11 02:27:56jhoblitt Exp $214 # $Id: perlCodeConventions.tex,v 1.5 2004-11-11 04:02:34 jhoblitt Exp $ 215 215 216 216 package Foo; … … 698 698 699 699 \begin{itemize} 700 701 700 \item Between the local variables in a subroutine and its first statement 702 701 … … 708 707 Blank spaces should be used in the following circumstances: 709 708 \begin{itemize} 710 711 \item A keyword followed by a parenthesis should be separated by a space. Example: 709 \item 710 A keyword followed by a parenthesis should be separated by a space. Example: 711 712 712 \begin{verbatim} 713 713 while (true) { … … 716 716 \end{verbatim} 717 717 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 \itemA 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);718 Note that a blank space should not be used between a subroutine name and its 719 opening parenthesis. This helps to distinguish keywords from subroutine calls. 720 721 \item 722 A blank space should appear after commas in argument lists. 723 724 \item 725 Binary operators should be separated from their operands by spaces. Blank 726 spaces should never separate unary operators such as a type cast, unary minus, 727 increment (\code{++}), and decrement (\code{--}) from their operands. Examples: 728 729 \begin{verbatim} 730 $a += $c + $d; 731 $a = ($a + $b) / ($c * $d); 732 732 733 while ( *d++ = *s++) { // Tricky way of copying until '\0'734 n++;733 while ($d++ = $s++) { 734 $n++; 735 735 } 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 740 The expressions in a \code{for} statement should be separated by blank spaces. 741 Example: 741 742 742 743 \begin{verbatim} … … 766 767 \textbf{Examples} \\ 767 768 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; 769 Subroutines & 770 771 The names of all public subroutines (and methods) should be verbal phrases, in 772 mixed case. 773 774 Private subroutines (and methods) should follow the same rules as public 775 subroutines 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 785 783 \\ 786 784 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}. 785 Variables & 786 787 Except for variables, all instance, class, and class constants are in mixed 788 case with Variable names should not start with underscore \code{_} as these 789 names are, under some circumstances, reserved by Perl community convention. 790 791 Variable names should be short yet meaningful. The choice of a variable name 792 should be mnemonic- that is, designed to indicate to the casual observer the 793 intent of its use. One-character variable names should be avoided except for 794 temporary \textit{throwaway} variables. Common names for temporary variables 795 are \code{i}, \code{j}, \code{k}, \code{m}, and \code{n} for integers; 796 \code{c}, \code{d}, and \code{e} for characters. 795 797 796 798 & 797 799 798 \code{ psRun(int ID);}\hfil\break799 \code{ psRunFast(float velocity);}\hfil\break800 \code{ psGetBackground(void);}\hfil\break800 \code{$i;}\hfil\break 801 \code{$c;}\hfil\break 802 \code{$myWidth;}\hfil\break 801 803 \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 831 806 832 807 \\ 833 808 834 Constants 809 Constants & 810 811 Perl's \code{constant} pragma should not be used as it merely generates 812 subroutines with null prototypes. 'Constants' created this way can not be 813 interpolated instead of strings. Instead, use Lexical varibles but with all 814 capital letters. 835 815 836 816 & 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 823 Namespaces & 824 825 Must start with a capitalized letter but otherwise follow the same naming 826 conventions as variables. 842 827 843 828 & 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 \\ 846 835 847 836 \end{tabular} … … 852 841 \section{Programming Practices} 853 842 854 \subsection{When to Make Symbols Global}855 856 Declare all subroutines and top-level variables \code{static} within a857 file if they are not needed outside of the file. {\bf Note:} do not858 confuse this use of \code{static} with its usage to make859 auto-variables within a subroutine persistent.860 861 A name (whether of a variable, a subroutine, or a type) shall start862 \code{ps} (or \code{p_ps}) if and only if it is visible at global863 scope. The distinction is that \code{p_ps} names are not part of the864 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. 865 854 866 855 \subsection{Constants} … … 870 859 in a \code{for} loop as counter values. 871 860 872 \subsection{Type Qualifiers (const and restrict)}873 874 All interfaces and type definitions should use \code{const} and875 \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 struct893 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 { // OK900 struct yourStruct *next;901 int x;902 } yourStruct;903 \end{verbatim}904 905 861 \subsection{Variable Assignments} 906 862 … … 909 865 910 866 \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 871 Do not use the assignment operator in a place where it can be easily confused 872 with the equality operator. Example: 873 874 \begin{verbatim} 875 if ($c++ = $d++) { # AVOID! 918 876 ... 919 877 } 920 878 \end{verbatim} 879 921 880 should be written as 922 \begin{verbatim} 923 if ((c++ = d++) != 0) { 881 882 \begin{verbatim} 883 if (($c++ = $d++) != 0) { 924 884 ... 925 885 }
Note:
See TracChangeset
for help on using the changeset viewer.
