Coding style question; function calling

Discussion corner for Developers of Serendipity.
Post Reply
FishNiX
Regular
Posts: 40
Joined: Sun Sep 02, 2007 6:32 pm

Coding style question; function calling

Post 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);
}

garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Coding style question; function calling

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
FishNiX
Regular
Posts: 40
Joined: Sun Sep 02, 2007 6:32 pm

Re: Coding style question; function calling

Post by FishNiX »

Garvin - Thanks as always!
Post Reply