SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Would this be a violation of the dry principle ?

    I am building a task manager with projects and departments. Each project has somedepartments. In the admin interface one can add a department. Of course you have to assign it to some projects (yes, more than one is possible).

    So this would make up something like this to check the checkboxes and insert the department to the projects:

    PHP Code:
                //-------------------------------------------
                // Insert the Department into the DB
                //-------------------------------------------
                        
                
    if( isset( $SITE->input['projects'] ) )
                {                    
                    foreach( 
    $SITE->input['projects'] as $projId )
                    {    
                        
    $DB->query"INSERT INTO intr_departments (name, proj_id) VALUES ('{$SITE->input['deptName']}', '{$projId}')" );
                    }
                }
                
                
    $this->content .= 'Department successfully added.'
    So far so good. But I have another feature in the admin interface: You can add assign a user to some projects, to some departments, etc. All you have to do is pick a name and click some checkboxes to assign him to certain projects and departments. Looks similar to this:

    PHP Code:
                    //-------------------------------------------
                    // Departments
                    //-------------------------------------------
                    
                    
    if( isset( $SITE->input['departments'] ) )
                    {
                          
    $DB->query"DELETE FROM intr_user2department WHERE user_id = '{$SITE->input['userId']}'" );
                          
                        foreach( 
    $SITE->input['departments'] as $deptId )
                        {                      
                            
    //-------------------------------------------
                            // Did we determine user should be a dept
                            // leader? :)
                            //-------------------------------------------
                                
                            
    $isDeptLead 0;
                                
                            if( isset( 
    $SITE->input["departments_Leader::{$deptId}"] ) ) 
                                
    $isDeptLead $SITE->input["departments_Leader::{$deptId}"];
                                
                                
                            
    //-------------------------------------------
                            // Insert the department into the collection 
                            // table
                            //-------------------------------------------
                                
                            
    $DB->query"INSERT INTO intr_user2department (user_id, dept_id, is_dept_lead) 
                                            VALUES ('
    {$SITE->input['userId']}', '{$deptId}', '{$isDeptLead}')" );
                        }
                    } 
    Hmm. Some of the code looks exactly like the one from the first code box. Is this refactorable ? Must this be refactored ? Is this a violation of the DRY (Don't-Repeat-Yourself)-Principle?

    What do you think ?

  2. #2
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    Quote Originally Posted by DarkAngelBGE
    What do you think ?
    Are departments and projects just different types of teams? A team being anything you can add people to.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  3. #3
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, basically. A project has some departments that each have members. A member can be on more than one department, scattered over different projects et al.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    PHP Code:
    ...$SITE->input["departments_Leader::{$deptId}"] ... 
    Like how you've worked this part

    Paying attention to how it all works internally, as using { ...string... } appears to be useful.

    You seam to be executing a class based on an ID ?? Might be wrong though

  5. #5
    SitePoint Wizard
    Join Date
    May 2003
    Location
    Berlin, Germany
    Posts
    1,829
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You know there is the ability to be a leader of a department or just a member. Now for each checkbox (that represents a department) there is a radio button saying "Leader?".

    I just put the radio buttons into one name "department leaders" and append the department id after a ::. In the markup interpretation I explode() the thing using '::' as the delimiter and can check which $deptId's the fella shall be a leader of.

    Quote Originally Posted by Widow Maker
    Off Topic:


    PHP Code:
    ...$SITE->input["departments_Leader::{$deptId}"] ... 
    Like how you've worked this part

    Paying attention to how it all works internally, as using { ...string... } appears to be useful.

    You seam to be executing a class based on an ID ?? Might be wrong though

    There's a big spectrum between exact repetition on one hand, and pattern repetition on the other hand, but both are repetition. According to DRY, you shouldn't repeat yourself. However, I believe you must always take the cost into account for such a principle.

    Some patterns can be so hard to factor out that it won't be worth your time. This depends very much on your abstract thinking skills.

    I like to compare refactoring with walking through a tunnel. Only start refactoring if you see light (the 'solution' and it's advantages) at the end of the tunnel.
    Thanks, Meryn, very useful post. I think I won't refactor this keeping the flexibility on my side.

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    Netherlands
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's a big spectrum between exact repetition on one hand, and pattern repetition on the other hand, but both are repetition. According to DRY, you shouldn't repeat yourself. However, I believe you must always take the cost into account for such a principle.

    Some patterns can be so hard to factor out that it won't be worth your time. This depends very much on your abstract thinking skills.

    I like to compare refactoring with walking through a tunnel. Only start refactoring if you see light (the 'solution' and it's advantages) at the end of the tunnel.


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
  •