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

I have written a function for switching advert image based on $template. Function is working completely fine without any problem and getting everything dynamically with no issue.

Concerning performance, I want some feedback and suggestion from you experts to optimize code in better way.

Since below code is dynamically getting option key using $template so I am not sure is it good to repeat same code for each case or there is some better way.

$this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
$this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
$this->render('</a>');

Please find below is my full function code

function page_advert()
{

    $template = get_request() == '' ? 'home' : get_request_part(0);
    $advert = option('ops_'.$template.'_advert_image_url');

    if((option('ops_'.$template.'_enable_adverts')) && (!empty($advert))) {

        $this->render('<!-- Start page advert -->','<div class="ops-page-advert '.$template.'">'); 

        switch ($template) {
            case 'home':

                $this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
                $this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
                $this->render('</a>');

                break;

            case 'archive':

                $this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
                $this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
                $this->render('</a>');

                break;

            case 'page':

                $this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
                $this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
                $this->render('</a>');

                break;

            case 'contact':

                $this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
                $this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
                $this->render('</a>');

                break;

            case 'tags':

                $this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
                $this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
                $this->render('</a>');

                break;

            case 'categories':

                $this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
                $this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
                $this->render('</a>');

                break;

            case 'users':

                $this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
                $this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
                $this->render('</a>');

                break;

            case 'admin':

                $this->render('<a href="'.option('ops_'.$template.'_advert_destination_link').'" >');
                $this->render('<img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-'.$template.'-advert" />');
                $this->render('</a>');

                break;

            default:                    
                return false;
                break;
        }

        $this->render('</div>', '<!-- End page advert -->');

    } //endif

}
share|improve this question
up vote 3 down vote accepted

Ah! what a stupid mistake I have done.. :P Didn't realized that I don't have to use Switch Case either. Here is the final optimized code

function page_advert()
{

    $template = get_request() == '' ? 'home' : get_request_part(0);
    $advert = option('ops_'.$template.'_advert_image_url');

    if((option('ops_'.$template.'_enable_adverts')) && (!empty($advert))) {

        $html = '
        <!-- Start page advert -->
        <div class="q2am-page-advert '.$template.'">
            <a href="'.option('ops_'.$template.'_advert_destination_link').'" >
                <img src="'.option('ops_'.$template.'_advert_image_url').'" alt="adv-market-'.$template.'-advert" />
            </a>
        </div>
        <!-- End page advert -->
        ';

        $this->output($html);

    } //endif

}
share|improve this answer
    
Why not concatenate all of those lines of HTML and just make 1 render call? – jsanc623 Jan 10 '14 at 15:45
    
Nice idea.. let me try that too.. thanks – pixelngrain Jan 10 '14 at 16:01
    
@jsanc623 how about this now? – pixelngrain Jan 10 '14 at 16:12
    
looks much better now and only 1 function call vs 5 :) – jsanc623 Jan 10 '14 at 18:04
1  
Agree! and thank for the tip :) – pixelngrain Jan 10 '14 at 18:07

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.