previousArray formatting (Previous) (Next) Notesnext

View this page in Last updated: Sun, 18 Oct 2009
English | Brazilian Portuguese | Chinese | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Turkish

Recommendations

Readability of code blocks

Related lines of code should be grouped into blocks, seperated from each other to keep readability as high as possible. The definition of "related" depends on the code :)

For example:

<?php

if ($foo) {
    
$bar 1;
}
if (
$spam) {
    
$ham 1;
}
if (
$pinky) {
    
$brain 1;
}
?>

is a lot easier to read when seperated:

<?php

if ($foo) {
    
$bar 1;
}

if (
$spam) {
    
$ham 1;
}

if (
$pinky) {
    
$brain 1;
}
?>

Return early

To keep readability in functions and methods, it is wise to return early if simple conditions apply that can be checked at the beginning of a method:

<?php

function foo($bar$baz)
{
    if (
$foo) {
        
//assume
        //that
        //here
        //is
        //the
        //whole
        //logic
        //of
        //this
        //method
        
return $calculated_value;
    } else {
        return 
null;
    }
}
?>

It's better to return early, keeping indentation and brain power needed to follow the code low.

<?php

function foo($bar$baz)
{
    if (!
$foo) {
        return 
null;
    }

    
//assume
    //that
    //here
    //is
    //the
    //whole
    //logic
    //of
    //this
    //method
    
return $calculated_value;
}
?>
previousArray formatting (Previous) (Next) Notesnext

Download Documentation Last updated: Sun, 18 Oct 2009
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
There are no user contributed notes for this page.