Perl coding standard

From VyOS Wiki
Jump to: navigation, search

Definitions:

  • Shall rules are mandatory requirements. They must be followed and they require

verification (either automatic or manual).

  • Will rules are intended to be mandatory requirements. It is expected that they will be

followed, but they do not require verification. They are limited to non-safety-critical requirements that cannot be easily verified (e.g., naming conventions).

  • Should rules are advisory rules. They strongly suggest the recommended way of

doing things.


General rules

Function size

Any one function (or method) will contain no more than 200 logical source lines of code.

Rationale: Long functions are hard to test and comprehend.

Line length

Source lines will be kept to a length of 120 characters or less.

Rationale: Very long source lines can be difficult to read and understand.

Self-modifying code

There shall not be any self-modifying code.

Rationale: Self-modifying code is error-prone as well as difficult to read, test, and maintain.

Character set

Only ASCII characters shall be used in both code and messages.

Rationale: Avoid compatibility issues.

Commented out code

There should not be commented out code.

Rationale: Makes it harder to comprehend the code, also modern SCM systems make it unnecessary.

Formatting

Whitespace

Tabs should be avoided.

Rationale: Tabs are interpreted differently across various editors and printers. Configure your editor to expand tabs.

Indentation

All indentations will be at least four spaces per level and be consistent within the same source file.

Rationale: Readability.

Identifiers

Identifiers should use underscore as word separator. Examples: $some_variable, some_function.

Constant identifiers should be written in all capitals. Examples: DEFAULT_FILE_NAME.

Blocks

The statements forming the body of an if, elsif, else, while, for, or foreach statement shall always be enclosed in braces, even if the braces form an empty block.

Braces will be placed on a separate line.

Examples:

sub is_even($)
{
    if( $_ % 2 == 0 )
    {
         return(1);
    }
    else
    {
         return(0);
    }
}

Programming guidelines

strict and warnings

Modules strict and warnings shall be used.

use strict;
use warnings;

Rationale: Helps to eliminate a lot of mistakes early.

Subroutine prototypes

Subroutine prototype should have correct argument lists.

sub is_even($)
{
}

sub sum($$)
{
}