Changeset 2332
- Timestamp:
- Nov 10, 2004, 3:55:58 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/doc/misc/perlCodeConventions.tex (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/misc/perlCodeConventions.tex
r2315 r2332 1 %%% $Id: perlCodeConventions.tex,v 1. 1 2004-11-09 03:55:35jhoblitt Exp $1 %%% $Id: perlCodeConventions.tex,v 1.2 2004-11-11 01:55:58 jhoblitt Exp $ 2 2 \documentclass[panstarrs]{panstarrs} 3 3 … … 155 155 # Copyright (C) 2004 Joshua Hoblitt 156 156 # 157 # $Id: perlCodeConventions.tex,v 1. 1 2004-11-09 03:55:35jhoblitt Exp $158 159 use 5.008005; # optional157 # $Id: perlCodeConventions.tex,v 1.2 2004-11-11 01:55:58 jhoblitt Exp $ 158 159 use 5.008005; # optional 160 160 161 161 use strict; # not optional … … 212 212 # Copyright (C) 2004 Joshua Hoblitt 213 213 # 214 # $Id: perlCodeConventions.tex,v 1. 1 2004-11-09 03:55:35jhoblitt Exp $214 # $Id: perlCodeConventions.tex,v 1.2 2004-11-11 01:55:58 jhoblitt Exp $ 215 215 216 216 package Foo; 217 217 218 use 5.008005; # optional218 use 5.008005; # optional 219 219 220 220 use strict; # not optional … … 279 279 squished up against the right margin, just indent 8 spaces 280 280 instead.\footnote{This should be \emph{very} rare! Consider whether 281 you should be e.g. factoring code into a function.}281 you should be e.g. factoring code into a subroutine.} 282 282 283 283 \end{itemize} 284 284 285 Here are some examples of breaking functioncalls:286 287 \begin{verbatim} 288 some Func(longExpression1, longExpression2, longExpression3,285 Here are some examples of breaking subroutine calls: 286 287 \begin{verbatim} 288 someSub(longExpression1, longExpression2, longExpression3, 289 289 longExpression4, longExpression5); 290 290 291 $var = some Func1(longExpression1,292 some Func2(longExpression2,291 $var = someSub(longExpression1, 292 someSub(longExpression2, 293 293 longExpression3)); 294 294 \end{verbatim} … … 322 322 \section{Comments} 323 323 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 326 Comments should not be used to comment out large sections of code. When 327 deactivating 2 or fewer lines of code a \code{#} should be used at the very 328 beginning of the line(s). 329 330 Example of deactivating a single line code. 331 \begin{verbatim} 332 if ($foo) { 333 # print( "foo" ); 334 } 335 \end{verbatim} 336 337 When deactivating 3 or more lines of code an \code{if} statement should be used 338 with a false boolean value. The opening and closing of the \code{if} statement 339 must be at the very beginning of the line. 340 341 Example of deactivating multiple lines of code. 342 \begin{verbatim} 343 if (0) { 344 if ($foo) { 345 print( "foo" ); 346 } 347 } 348 \end{verbatim} 346 349 347 350 Comments should be used to give overviews of code and provide … … 370 373 \subsection{Implementation Comment Formats} 371 374 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}). 375 Programs can have three styles of implementation comments: single-line, 376 multi-line and trailing. 386 377 387 378 \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 380 Short comments can appear on a single line indented to the level of the code 381 that follows. The comment should appear above the code that it is describe and 382 must have a space between the \code{#} and the first text. 383 384 Example of a single-line comment. 385 \begin{verbatim} 386 # some words 387 \end{verbatim} 388 389 \subsubsection{Multi-Line Comments} 390 391 Merely a repitition of single-line comments. 393 392 394 393 \subsubsection{Trailing Comments} 395 \label{col4 0}394 \label{col41} 396 395 397 396 Very short comments can appear on the same line as the code they 398 describe, and should be indented to column 4 0\footnote{In emacs, this397 describe, and should be indented to column 41\footnote{In emacs, this 399 398 may be achieved with \code{ESC;}}. If the code extends beyond this 400 399 column, the comment should be separated from the closing semi-colon by 401 400 a single space. 402 401 403 Here's an example of a trailing comment in Ccode:404 \begin{verbatim} 405 if ( a == 2) {406 return TRUE; /* special case */402 Here's an example of a trailing comment in Perl code: 403 \begin{verbatim} 404 if ($a == 2) { 405 return TRUE; # special case 407 406 } 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 426 408 } 427 409 \end{verbatim} … … 430 412 \label{DocComments} 431 413 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 ) 414 Plain Old Documentation format or Pod will be used to produce documentation of 415 the subroutines and variables in the exposed API. Pod sections will appear 416 above the section of code that they describe. 417 418 An example of a Pod section defining the API of a subroutine. 419 420 \begin{verbatim} 421 =item * reallyCoolSubroutine 422 423 Accepts a real number and a string. Does some really cool stuff with it and 424 returns a really cool thing. 425 426 =cut 427 428 sub reallyCoolSubroutine 456 429 { 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 438 If you need to give information about a interface, or variable that isn't 439 appropriate for Podification, use a normal implementation single-line or 440 multi-line comment immediately \textit{before} the code (as exampled above). 472 441 473 442 %------------------------------------------------------------------------------ … … 479 448 files, but rather in separate, private header files. 480 449 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 452 One declaration per line with a single place between 'my' and the identifier. 453 454 \begin{verbatim} 455 my $level; # indentation level 456 my $size; # size of table 457 \end{verbatim} 458 489 459 is preferred over 490 \begin{verbatim} 491 int level, size; 492 \end{verbatim} 460 461 \begin{verbatim} 462 my ($level, $size); 463 \end{verbatim} 464 465 In almost all situations with the exception of when parameters are being passed 466 into a subrutine. 467 493 468 Do not put different types on the same line. Example: 494 469 \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}). 470 my ($foo, @fooArray); # WRONG! 471 \end{verbatim} 506 472 507 473 \subsection{Initialization} … … 521 487 associates their declaration with their use. 522 488 523 For example, declarations are naturally mixed with assertions: 524 \begin{verbatim} 525 void function(REGION *reg) // image of the sky 489 490 Indexes of loops should usually be declared on the line above loop statement. 491 492 \begin{verbatim} 493 my $i = 0; 494 for (i < $maxLoops) { 495 # do stuff 496 497 $i++; 498 } 499 \end{verbatim} 500 501 Avoid \code{local} declarations that hide user declared variables from higher 502 level scopes. For example, do not declare the same variable name in an inner 503 block: 504 505 \begin{verbatim} 506 my $count; 507 ... 508 sub mySubroutine 526 509 { 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; 531 511 ... 532 512 } 533 513 \end{verbatim} 534 514 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 517 When coding Perl subroutines the following formatting rules should be 518 followed: 519 520 \begin{itemize} 521 \item 522 Subroutine declarations should be preceeded by a short comment describing what 523 the subroutine does. These documentation comments should include a brief 524 desription as well as other warnings, bugs, etc. as needed. 525 526 \item 527 The subroutine body's open brace \CODE.{. should be in column 1 of the next 528 line. 529 530 \item 531 The first line after the opening brace should declare a list of lexical 532 variables for input parameters and assign \code{@\_} to it. 533 534 \item 535 Closing brace \CODE.}. starts a line by itself indented to match its 536 corresponding opening statement, except when it is a null statement the 537 \CODE.}. should appear immediately after the \CODE.{.. 538 539 \item 540 The last line before the closing brace must explicity declare a return value. 541 \end{itemize} 542 543 \begin{verbatim} 544 sub subName 545 { 546 my ($param1, $param2, ...) = @_; 540 547 ... 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} 612 552 613 553 %------------------------------------------------------------------------------ … … 799 739 \begin{itemize} 800 740 801 \item Between the local variables in a functionand its first statement802 803 \item Between logical sections inside a functionto improve readability741 \item Between the local variables in a subroutine and its first statement 742 743 \item Between logical sections inside a subroutine to improve readability 804 744 \end{itemize} 805 745 … … 816 756 \end{verbatim} 817 757 818 Note that a blank space should not be used between a functionname and758 Note that a blank space should not be used between a subroutine name and 819 759 its opening parenthesis. This helps to distinguish keywords from 820 functioncalls.760 subroutine calls. 821 761 822 762 \item A blank space should appear after commas in argument lists. … … 854 794 855 795 Naming conventions make programs more understandable by making them 856 easier to read. They can also give information about the functionof857 the identifier -- for example, whether it's a constant, a function, or796 easier to read. They can also give information about the subroutine of 797 the identifier -- for example, whether it's a constant, a subroutine, or 858 798 a type -- which can be helpful in understanding the code. Remember 859 799 these are guidelines for improving readability; clarity should trump … … 885 825 \\ 886 826 887 Functions &888 889 The names of all externally visible functions (i.e. all those that are827 Subroutines & 828 829 The names of all externally visible subroutines (i.e. all those that are 890 830 not declared \code{static}) should be verbal phrases, in mixed case. 891 831 892 832 Namespaces should be protected by using special naming prefixes to 893 833 restrict the name space in particular libraries. For example, the 894 PSLib functions are all prefixed with \code{ps}.834 PSLib subroutines are all prefixed with \code{ps}. 895 835 896 836 & … … 954 894 \subsection{When to Make Symbols Global} 955 895 956 Declare all functions and top-level variables \code{static} within a896 Declare all subroutines and top-level variables \code{static} within a 957 897 file if they are not needed outside of the file. {\bf Note:} do not 958 898 confuse this use of \code{static} with its usage to make 959 auto-variables within a functionpersistent.960 961 A name (whether of a variable, a function, or a type) shall start899 auto-variables within a subroutine persistent. 900 901 A name (whether of a variable, a subroutine, or a type) shall start 962 902 \code{ps} (or \code{p_ps}) if and only if it is visible at global 963 903 scope. The distinction is that \code{p_ps} names are not part of the … … 1103 1043 \end{verbatim} 1104 1044 1105 \subsubsection{ Functions that Take No Parameters}1106 1107 When a functiontakes no arguments, it should be explicitly declared1045 \subsubsection{Subroutines that Take No Parameters} 1046 1047 When a subroutine takes no arguments, it should be explicitly declared 1108 1048 as \code{void}: 1109 1049 \begin{verbatim} 1110 static type my Function(void)1050 static type mySubroutine(void) 1111 1051 { 1112 1052 ... … … 1150 1090 1151 1091 \item 1152 Use \code{\notused} to indicate unused arguments to a function:1092 Use \code{\notused} to indicate unused arguments to a subroutine: 1153 1093 e.g. 1154 1094 \begin{verbatim} … … 1327 1267 \item 1328 1268 Added naming conventions for constructors, destructors, and conversion 1329 functions.1269 subroutines. 1330 1270 1331 1271 \item … … 1337 1277 1338 1278 \item 1339 Specified that functions taking no arguments should be explicitly1279 Specified that subroutines taking no arguments should be explicitly 1340 1280 specified as \code{(void)} (avoiding complaints on some compilers, 1341 1281 e.g. on SGIs). … … 1418 1358 (add-hook 'panstarrs-c-mode-hook 1419 1359 '(lambda () 1420 (set (make-variable-buffer-local 'comment-indent- function)1360 (set (make-variable-buffer-local 'comment-indent-subroutine) 1421 1361 'panstarrs-comment-indent))) 1422 1362 \end{verbatim}
Note:
See TracChangeset
for help on using the changeset viewer.
