William Valdar's Perl coding conventions


This document, which is intended to help other people read my code, reflects the way I write perl at the moment. I am not suggesting this is the best way to write perl or any other language; any set of conventions has it's good and bad points and this is no exception.These conventions have evolved with time and will probably undergo further revisions as I encounter new problems and explore new practices in my work.
 

Naming

Follow java conventions where possible. Avoid abbreviations or contractions unless they are standard: scrSeq() is bad, scoreSeq() is okay, scoreSequence() is better provided it doesn't make other names unweildy, eg, scoreMultipleSequenceAlignment() is probably worth contracting to scoreMsa(). If you have to include an abbreviation, treat it as a single word: avoid PDBFile and ReadPDBFile; prefer pdbFile and ReadPdbFile. Except for constants, use '_' sparingly.

Variables


Perl is not type-safe and this can cause confusion and errors. Use a limited prefix notation for such common basic types as array, hash, FileHandle.

Functions

Modules (packages that are not classes)

Classes

As for modules but with 'C' prefix: CStopwatch, CWindowPanel, Pdb::CResidue

Instance methods

Layout conventions

Bracket using the Allman style. Indent at four spaces. Avoid hardware tabs. Put a single space between while/if/for and their conditionals to distinguish them from functions. Indent overflowing lines by twice the normal indent (ie, eight spaces). Eg,

while (false != $condition)
{
    if ($debug)
    {
        SomeFunction($argOne, $argTwo);
        SomeOtherFunction($fred, $barney, $betty, $wilma, $bambam, $bedrock,
                $dinosaur, $otherGuy);
    }
}

Comment long argument lists like this:

FooFunction($name,  # person's name
        $address,   # where they live
        $zScore);   # their z-score

Comment extended regular expressions similarly.
 

Documentation

 
Notation Meaning
CRE caught run-time exception
URE uncaught run-time exception
<foo> argument or return value is of type foo
<const foo> foo is assumed to be constant (ie, not modified directly)
boolean zero is false, nonzero is true
char SCALAR variable of one character
CONSTANT a class constant
int SCALAR variable of integer value
real SCALAR variable of floating-point value
string SCALAR variable holding a string
Pdb::CResidue reference to an object of type Pdb::CResidue
@ ARRAY
@ real ARRAY of reals
\@ reference to an ARRAY
\@ real reference to an ARRAY of reals
\@@ real reference to a 2-D ARRAY of reals
% HASH
\% reference to a HASH
\sub reference to a subroutine
ist intput stream object
ost output stream object
stream stream object
this a reference to the current object
SCALAR anything as long as it's scalar (not a reference)
? SCALAR or a reference

eg:
 

  • ColsFromStream
  •  Function:
       Returns arrays representing the specified columns from a file
     ARGUMENTS:
       1. <ist> stream
       2. <\@ int> list of column numbers (starting 0..)
     RETURN:
       if scalar
           1. <\@ string> first column specified
       elsif array
           <@@ string> array of arrays of specified columns

    Programming conventions

    my @arrayType = (1..100);
    ProcessArray(\@arrayType);

    sub ProcessArray($)
    {
        my $arrayRef = shift;
        # array spends most of its life accessed
        # as a ref anyway
       Process($arrayRef);
        # etc...
    }