Page 1 of 1

Managing functions used in many places

Posted: Tue Jul 20, 2010 12:45 pm
by eruna
Lets say there is a function that displays product information. In the beginning, there is only a category variable that differentiates the output, and everything works great.

Over time, the site evolves and more and more distinct requirements for the different areas emerge. What is a good strategy for evolving the function to minimize bugs in areas of the site that share the same function.

Thanks E

Re: Managing functions used in many places

Posted: Tue Jul 20, 2010 1:34 pm
by Alex-V
Maybe this

Code: Select all

// original funcion
function display($category)
{
    // generate data

    return $data;
}

// some time after
function display($category, $option2 = false)
{
    //generate data
    if($option2)
    {
        // generate more data
    }

    return $data;
}

// even more time after
function display($category, $option2 = false, $option3 = false)
{
    //generate data
    if($option2)
    {
        // generate more data
    }

    if($option3)
    {
        // generate even more data
    }

    return $data;
}

Re: Managing functions used in many places

Posted: Wed Jul 21, 2010 9:59 am
by eruna
Right, I do that. I was asking for broader strategy approaches. Sometimes a function is used in a dozen places and altering it for one purpose can effect it in a way that causes a problem somewhere else.

Re: Managing functions used in many places

Posted: Thu Jul 22, 2010 6:10 am
by josh
Automated testing. Think PHPunit http://www.phpunit.de

Alex-V signatures cannot be overridden in PHP - that would not work. Edit: - Oh I see you're saying the function would be changed over time - thats the very problem hes facing. Monadic functions (one parameter) are hard enough to understand. By the time you've got tons of parameters you've created a really unmaintainable function. It needs to be broken out into MULTIPLE functions (with different names). But "how to do so without breaking it" was the question.

Ideally after you have the code in an automated test "harness" (think of it like rock climbing, you can climb but not fall to your peril) - then you can go use things like objects & classes to better organize your code. so Automated tests pin down current functionality, and then due to that it also allows you to become more fearless in refactoring or cleaning up messy code. A TDD developer like me will write it nice & messy, but since I write the tests before creating the mess - I can easily stop and clean it up every 20 minutes or so to keep things top notch throughout the project instead of boiling myself alive in bugs or being one of those developers who is afraid to edit his own code and is always debugging. Automated tests have many other benefits. Its a whole life style change. Debugging makes me sad & depressed and has serious psychological impacts if done all the time. Writing tests can be a little monotonous but I'll take that over depression.

Read the phpunit documentation & lurk on our testing forums here. Try it out for 30+ days before deciding if you like it or are dismissing it. It takes some time to "get it". Also check out the videos Jenk shared in the testing forum. They aren't in PHP but it shows the idea really good! viewtopic.php?f=39&t=112787