Difference between revisions of "Perl Coding Standards"

From Apache OpenOffice Wiki
Jump to: navigation, search
m
 
(One intermediate revision by the same user not shown)
Line 48: Line 48:
 
* filehandles in capital letters: <code>MYFILE</code>   
 
* filehandles in capital letters: <code>MYFILE</code>   
 
* start class names with a capital: <code>MySelfmadeModule</code>
 
* start class names with a capital: <code>MySelfmadeModule</code>
* prefere underscores instead of mixed cases for variable names  
+
* prefere underscores instead of mixed cases for variable names: <code>multipart_name</code>
  
 
Recommendation: use <code>ref</code> postfix for references
 
Recommendation: use <code>ref</code> postfix for references
Line 58: Line 58:
 
===== Use Parentheses(Paren) =====
 
===== Use Parentheses(Paren) =====
 
Use parentheses for all function calls.  
 
Use parentheses for all function calls.  
<br>Possible exceptions: standard perl idioms
+
 
 +
Possible exceptions: standard perl idioms.
  
 
===== Close Handles Soon (Close) =====
 
===== Close Handles Soon (Close) =====
Line 64: Line 65:
  
 
===== Near Backinserters (BackIn) =====
 
===== Near Backinserters (BackIn) =====
Use and assign RE backinserters (like <code>($1), ($2)</code>) as close to the originating regular expression as possible, never more than five lines away.
+
Use and assign regular expression backinserters (like <code>($1), ($2)</code>) as close to the originating expression as possible, never more than five lines away.
  
 
===== Check System Calls (System) =====
 
===== Check System Calls (System) =====
Line 73: Line 74:
  
 
===== Pathseparator is Slash (Path) =====
 
===== Pathseparator is Slash (Path) =====
Use the forward slash as path separator in all cases.<br>
+
Use the forward slash as path separator in all cases.
The singular exception is, when a system program needs a path in local convention as argument.
+
 
 +
Exception: When a system program needs a path in local convention as argument.
  
  
Line 86: Line 88:
 
===== Document Return (DocReturn) =====
 
===== Document Return (DocReturn) =====
 
If a function has a non-obvious return semantic, document type and semantic of the possible return values.
 
If a function has a non-obvious return semantic, document type and semantic of the possible return values.
 +
----
 +
[[Category:Coding Standards]]

Latest revision as of 11:04, 15 June 2007

Purpose

See the C++ Coding Standards for the general purpose of Standards.

After doing Code Reviews with C++ for a while, there occurred the wish among Perl developers to do code reviews as well and to set up a rule set for that purpose.

The following is a first draft of those.

Rules for Perl

General

Strict Settings (Strict)

Apply use strict and use warnings to all scripts and modules.


Design

Make Functions Short (Short)

Make functions short. A comment line or large control structures are hints to extract functions, making the containing function more concise and readable.

Avoid Duplicate Code (Dupli)

Avoid and remove duplicate code. Put common code into an extra function.
Duplicate code may be okay, if otherwise dependencies among so far unrelated modules would be created.

Use Framework Classes (Reuse)

Use the modules provided in a standard Perl 5.8 installation instead of reinventing the wheel.
However, don't create new dependencies to uncommon Perl modules, if there is no major need.

Use Perl Functions (PerlFs)

Prefer perl functionality over calls to system() where possible.

Optimize Sensibly (Opti)

Optimize by algorithm complexity.
Don't do smaller optimizations, if they make the code less readable and there is no proven reason.

Declarations: Scoping and Names

Use my (My)

Use my to scope all variables local to a block.

Avoid Global Data (NoGlobal)

Use as few global data as possible.

Assign Parameters at Top (Params)

Assign all parameters of a function to local, sensibly named variables on top of the function body.

Descriptive Names (Descr)

Use descriptive names.

Naming Conventions (NameConv)

Adhere to naming comventions:

  • filehandles in capital letters: MYFILE
  • start class names with a capital: MySelfmadeModule
  • prefere underscores instead of mixed cases for variable names: multipart_name

Recommendation: use ref postfix for references

Implementation Details

Explicit Returns (Return)

Use an explicit return statement for every return.

Use Parentheses(Paren)

Use parentheses for all function calls.

Possible exceptions: standard perl idioms.

Close Handles Soon (Close)

Don't forget to close all filehandles and connections. Close them as soon as possible.

Near Backinserters (BackIn)

Use and assign regular expression backinserters (like ($1), ($2)) as close to the originating expression as possible, never more than five lines away.

Check System Calls (System)

Check the return values of all system calls.

Use the Correct Comparison Operators (Compare)

Make sure, eq etc. are used for strings and == etc. for numbers.

Pathseparator is Slash (Path)

Use the forward slash as path separator in all cases.

Exception: When a system program needs a path in local convention as argument.


Comments

Comments (Comments)

Use enough comments in the code to make it understandable.

Document Function Behaviour (ClearBehave)

As far as the behaviour of a function is not clear by its name and parameters, document that behaviour.

Document Return (DocReturn)

If a function has a non-obvious return semantic, document type and semantic of the possible return values.


Personal tools