Version 3 (modified by juruen@…, 6 years ago) (diff)

--

Coding style

A few coding style rules. Some of them make sense, some are just there to provide consistency across everybody's code.

KISS

  • code should be kept simple and readable. This should be a major priority.

Some examples of this:

        less readable: if (!condition) {
        more readable: unless (condition) {

        less readable: $foo->doSomeStuff(someArg, anotherArga) if (condition)
        more readable: if (condition) { 
                                $foo->doSomeStuff(...);
                        }

the shorter versions of if constructs are good when the code is small:

        checkData($arg) or die "wrong data"; # this is perfectly readable

but, the ones that put the condition before are more readable:

        die "Wrong data" unless checkData($arg); # this is less readable

Format

  • Lines should be 80 characters wide unless there is a very good reason for doing so.
  • Tabs are tabs, spaces may only be used for alignment purposes, when the space to fill is smaller than a tab. Tabs are 8 spaces wide.

Braces

Opening brace starts at the end of the line:

  • Ending brace goes on a line by itself. Aligned with the opening line.
                    if () {
                            do stuff
                    }
    
    • else and elsif clauses go on the same line as the closing brace of the if:
                      if () {
                              do stuff
                      } else {
                              do other stuff
                      }
      

Spacing

Put spaces before and after parethesis, and braces they make everything more readable:

                wrong: if(someCondition){
                                do stuff
                       }else{
                                do other stuff
                       }

                right: if (someCondition) {
                                do stuff
                       } else {
                                do other stuff
                       }

Put spaces around operators:

                wrong: my $foo=$bar+$foobar;
                right: my $foo = $bar + $foobar;

Parameters

Method signature

  • In case of having fewer than three parameters use positional parameters, otherwise named parameters.
  • Avoid using flags in function taking more than one parameter. If necessary add a new method.

Fetching parameters

  • If a method receives positional parameters, fetch all them at once, and do not use shift
    This is wrong:
    sub foo() {
       my $a = shift;
       my $b = shift;
    }
    
    This is right:
    sub foo() {
       my ($a, $b) = @_;
    }
    

Naming public methods

This section has been copied from  this great article of the Qt Guys on APIs.

  • Do not abbreviate otherwise names will be more difficult to remember
  • Parameter names are an important source of information
  • Verbs have no prefix and don't use third person