CIT 042 Index > Programming Standards

CIT042–Perl Programming Standards

  1. For all of our class programs, you will provide the following comments at the beginning of your files:
    # Your Name
    # The course and Program Assignment Number
    # The purpose of the program
    
    For example:
    # Fred J. Doakes
    # CIT042A Assignment 1
    # Get information from users and assign it to scalars
    
  2. We have installed Textpad on the lab computers to use to create and edit your Perl programs. You are NOT required to use Textpad; you may use any text editor you choose. However, Textpad “remembers” your level of indenting and also displays line numbers, which are quite useful when trying to decipher error messages. Textpad can be started with the textpad icon icon in the quick start tray (bottom left of the screen).
  3. You may have only ONE statement per line.
  4. Statements exceeding 80 characters must be broken into multiple lines. These continuation statements are indented one tab from the initial continued statement.
  5. Follow the indentation style as used in the text book.
  6. Use Tabs for indentation, not spaces.
  7. In the book, the beginning brace for a block of code is on the same line as the statetment preceding it. You may also put the brace on a line all by itself; thus either:
    if ($n == 3) {
    or
    if ($n == 3)
    {
  8. Data Declarations
    1. All declarations are at the top of the block of code they are used in. (For now, that means top of the subroutine or top of the file.)
    2. Only ONE variable or constant declared per statement.
    3. Declare variables in the proper scope, using my
  9. Names are descriptive.
    1. Names for variables and subroutines begin with a lower case letter.
    2. Names for packages and modules begin with a capital letter
    3. Names for constants are in all capital letters
    4. Names that contain more than one word use an underscore to separate words.
  10. Spacing
    1. Separate blocks of algorithms and blocks of code with ONE blank line.
    2. Separate binary operators from their operands with one blank space. Example: $a = 2 + ($b - 3) * 4;
    3. Do not separate unary operators from their operand with any blank spaces, but separate the pair from other expressions with a space. Example: $a = $b++ - ++$c; (This is a good example of the spacing, but poor example of decent code. The result of this statement may not be what you think. Write some code to try and see if you are correct in predicting the final value of $a.)
    4. Follow commas with a space.
  11. Literal Usage:
    1. You may use string literals.
    2. Numeric literals are not to be used except
      1. To initialize declared constants
      2. When they are self defining (2 to double or halve a value, 100 to convert from and to percentages, 0 or 1 to initialize counters or accumulators).
      3. When they are given in a mathematical equation.
  12. Input Prompts should be descriptive and informative.
  13. Comments are for programmers, not students, and should not be redundant to the code. (Our textbook contains, as it should, comments for the students.) Example:
    my $count;           # Count temperatures: valid
    $count = $count + 1; # Add 1 to count: NOT valid
    
  14. Constraints:
    1. There will be only ONE exit point in a subroutine or loop. This means only ONE return statement in a subroutine and no usage of next or last in loops, unless required in an assignment (to make sure you know what they are).
      1. We will have only ONE return statement in a subroutine, and it will be the last line in the subroutine.
      2. We will not use the redo statement.
  15. All subroutines will be preceded with a comment containing the purpose of the subroutine.
  16. All subroutines will include comments describing the parameters.
  17. Decision structures will have separate lines for the condition and the true statement. Never put the true (or the false) on the same line as the condition. Never do anything like this:
    if ($x > $y){ $isGreater = 1;} else {$isGreater = 0;}
    Besides the standards violation, the above is bad practice. Preferred would be:
    $is_greater = $x > $y;