Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is completely optimization question, I have a pagination query like that

$this->paginate    = array(
        'fields'    =>  array(
                        'DISTINCT Contact.contact_id',
                        'Contact.first_name',
                        'Contact.last_name',
                        'Contact.email',
                        'Contact.created',
                        'ContactGroup.name',
                      ),  
        'conditions' => array(
                        $this->conditions,
                        'ContactsContactGroup.contact_group_id'=>$viewList,                        
                        isset($keywordQuery)?$keywordQuery:"",
                      ),      
        'limit'      => 5,
         'group'     => array('Contact.contact_id')
                      );
$data = $this->paginate('ContactsContactGroup');
$data = $this->paginate('ContactsContactGroup');

this query is called in every if and else statement, I have four conditions of if and else, and in all conditions the above piece of code is written.

I want to optimize, and avoid the big line of code in every condition, how can I optimize it, any answer will be appreciated.

share|improve this question
    
you can half the cost of that code by calling paginate only once =) – AD7six May 1 '14 at 17:11
up vote 2 down vote accepted

Perhaps you could wrap it all in a function:

function pagination($this,$viewList,$keywordQuery)
{
    $this->paginate    = array(
        'fields'    =>  array(
                        'DISTINCT Contact.contact_id',
                        'Contact.first_name',
                        'Contact.last_name',
                        'Contact.email',
                        'Contact.created',
                        'ContactGroup.name',
                      ),  
        'conditions' => array(
                        $this->conditions,
                        'ContactsContactGroup.contact_group_id'=>$viewList,                        
                        isset($keywordQuery)?$keywordQuery:"",
                      ),      
        'limit'      => 5,
         'group'     => array('Contact.contact_id')
                      );

   return $this->paginate('ContactsContactGroup');

}

if(something())
{
    //something
    $data=pagination($this,$viewList,$keywordQuery);
}
else
{
    //other
    $data=pagination($that,$viewList,$keywordQuery);
}

etc..

share|improve this answer
    
I'm sorry, but really, what sort of code review comes up with an answer that includes global? – Letharion Mar 14 '13 at 19:51
    
just a quick way to clean up his duplicate code. either that, or they can just delete it all, start over. – phouse Mar 14 '13 at 20:34
    
I get you though. @usii, maybe make the function return $data; – phouse Mar 14 '13 at 20:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.