Coding conventions

This page list the coding conventions that applies to GCstar’s code source. You should conform to them when modifying some code or creating new plugins. You may be used to other conventions. None of them are perfect. But using always the same ones in a project make work easier for all of the developers when maintenance or evolution are required. Using PerlTidy is a painless way to reformat your code according to these rules before check in.

Indentation

| Top |

Always indent your code using spaces (ASCII code 32) and not tabulations. Use 4 spaces for each level of indentation.

Brackets

| Top |

For all functions, loops or any block, the opening curly bracket should be placed alone on one line, at the same indent level as the previous line. The closing bracket should be aligned with the first one. Here is an example:

sub myMethod
{
    # Some code here
    while ($condition)
    {
        # Loop code
    }
    if ($anotherCondition)
    {
        # if code
    }
    else
    {
        # else code
    }
}

You may notice also that the else keyword should be placed on its own line.

Specific Perl features

| Top |

All the code should be done using strict code. So all of the Perl files, including the plugins, should contain this line on top of them:

use strict;

using PerlTidy

| Top |

PerlTidy is a very useful program to automatically reformat existing Perl source code according the above coding rules. Using this as the perltidy configuration file ~/.perltidyrc reformats your sources with no afford at all:

-l=90  # allow slightly too long line
-i=4   # indent by 4 space per level, no tabs
-pt=2  # parentheses tight
-bt=2  # curly braces tight
-isbc  # do not indent comments starting in col. 1
-nsfs  # no space before a semicolon in a for(;;) statement
-bl    # opening curly brace on new line
-nbbc  # no blank lines before comment lines

This simple call saves the reformatted source as your_source.pm.tdy:

perltidy your_source.pm

Using the additional parameter “-b” reformats in place and moves the unformatted original to your_source.pm.bak. To avoid reformatting in some certain areas of the file use these special marker sequences (see perltidy man page for more details):

#<<<  do not let perltidy touch this
    my @list = (1,
                1, 1,
                1, 2, 1,
                1, 3, 3, 1,
                1, 4, 6, 4, 1,);
#>>>
 
en/coding_conventions.txt · Last modified: 01/07/2010 03:47 by tenbaht



Should you have a problem using GCstar, you can open a bug report or request some support on GCstar forums.