Page 1 of 1

Coding style question; function calling

Posted: Fri May 27, 2011 1:16 am
by FishNiX
When I have a function called in a hook which needs config items is it better to do this:

Code: Select all

switch($event) {
    case 'somehook':
        $foocfg = $this->get_config('foo');
        $barcfg = $this->get_config('bar');
        $foo = doWork($foocfg, $barcfg);
        ...
        ...
    break;
...
...

function doWork($foocfg, $barcfg) {
    ....
    ....
    return($thingy);
}

or this

Code: Select all

switch($event) {
    case 'somehook':
        $foo = doWork();
        ...
        ...
    break;
...
...

function doWork() {
    global serendipity;

    $foocfg = $this->get_config('foo');
    $barcfg = $this->get_config('bar');
    ....
    ....
    return($thingy);
}



or this (which is almost like #1)

Code: Select all

switch($event) {
    case 'somehook':
        $foo = doWork($this->get_config('foo'), $this->get_config('bar'));
        ...
        ...
    break;
...
...

function doWork($foocfg,$barcfg) {
    global serendipity;
    ....
    ....
    return($thingy);
}


Re: Coding style question; function calling

Posted: Fri May 27, 2011 1:17 pm
by garvinhicking
Hi!

Personally, I prefer to use get_config calls only in the method (doWork()), UNLESS $foocfg etc. is also used in other event hooks. In that case I use a static declaration and store the config variable, so that when multiple event hooks are executed, the cfg does not need to be fetched multiple times.

HTH,
Garvin

Re: Coding style question; function calling

Posted: Fri May 27, 2011 3:09 pm
by FishNiX
Garvin - Thanks as always!