SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Is duplicate PHP code bad practice?

    To further clarify, I am wanting to find out if it is bad practice to duplicate a method / function, say it has a different while loop or slightly different variables etc.

    See below, these are two different functions, should I create one giant multi pupose function or keep them as two seperate, which is best practice?

    Sometimes you can fit everything within 1 function but not sure if that should always be the case or if there is a best practice for minimizing copy / paste scenarios or it doesnt really matter?


    PHP Code:
    function method1($data) {

        
    $mydate $data;
        
    $test $this->load->view('testpage',$mydata);

    }

    function 
    method2($data) {

        
    $otherdata$data;
        
    $test $this->load->view('anotherpage',$otherdata);



  2. #2
    SitePoint Wizard bronze trophy devbanana's Avatar
    Join Date
    Apr 2006
    Location
    Pennsylvania
    Posts
    1,736
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In general, I'd extract the common parts and create a private/protected method (if this is in a class, as hinted at by the use of $this).

    But I'm not really seeing duplication in your code. One line doesn't make duplication.
    Laudetur Iesus Christus!
    Christ's Little Flock
    Jesus is the Good Shepherd

  3. #3
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,819
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    Try this:

    PHP Code:

    function method$testpage=NULL$my_data=array() ) 
    {
        
    $this->load->view$testpage$my_data );
    }


    /* NOT REQUIRED
    function method1($data) 
    {
        $mydate = $data;
        $test = $this->load->view('testpage',$mydata);

    }
    function method2($data) {

        $otherdata= $data;
        $test = $this->load->view('anotherpage',$otherdata);


    */ 
    Last edited by John_Betong; Feb 19, 2012 at 02:46. Reason: removed $test since it was never used and changed order of parameters

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    In your short example, it is better to merge them into one function.

    However, and this is the big part, if the function needs to start doing more, and you have to pass in even more parameters, start considering ways to break it apart into easier maintainable groups. Last thing you want is a megalithic function that can both cook and clean at the same time.

  5. #5
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To me this looks like you gave an example from a conroller-class. In that case I don't think it would be smart to put several different functions from a controller-class into 1 function.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •