Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have these few lines of code (written inside a Smarty .tpl file).

{if $event.venueName == 'Multiple Venues'}
    Multiple Venues
{else}
    <a href="http://www.example.com/venue/{$event.venueUrlname}">{$event.venueName}</a>
{/if}

Would it be better to echo out the HTML "Multiple Venues", or the variable "$event.venueName"?

My gut feeling is the HTML is better, as Smarty/PHP wouldn't have to look up the value of the variable, then display it, but that's just my guess.

I realize that the answer to this question wont make any visible, maybe even measurable difference, but I'm just curious if I had this situation say, 100,000 times on a page, which (if any) would be quicker to execute.

share|improve this question

2 Answers 2

What you're doing is called premature optimization, which is often a poor use of time and effort.

You would be right in assuming that the static HTML will be faster. However, the degree to which this is true is minimal — you can probably expect gains of a few microseconds a call, since you're just doing a lookup on a variable.. Over 100 000 calls, this would result in less than a second of improvement (though I suspect you may have other things to worry about if you're calling it 100 000 times on a single page).

The correct approach to this is to profile your application when you've finished developing it. That way, you can identify the real bottlenecks and fix those, instead of spending time on minor things like this.


Aside: You should consider renaming $event.venueUrlname. For example, .venueURL looks good to me.

share|improve this answer
    
I agree with what you're saying, I was just curious when I thought about it :P. Ideally I'd like to call it Url too, but the standard when I was hired here is that urls stored in the database are called Urlname, so I'm stuck with that for now. –  Tom Hart Jul 24 at 15:24

It already has to look up the value of the variable $event.venueName for the if statement, so you can make your question irrelevant by storing it in a temporary variable.

Additionally, by storing it in a temporary variable and then printing it, you prevent duplication which could hamper maintenance (what if it was called something different than a venue)?

share|improve this answer
    
So using {$event.venueName} wouldn't require an extra look up? That's good to know. Thanks :) –  Tom Hart Jul 24 at 13:27
    
... err, that sounds a lot different from what I read. Gimme a sec. –  Pimgd Jul 24 at 13:28
    
Sorry, I didn't mean event, I meant extra. Had to type event too many times today... –  Tom Hart Jul 24 at 13:30

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.