I'm printing summary reports. If a store is in a region that is shopped quarterly, the title is "Mar 2005", "Jun 2005", etc--the last month in the shop cycle. If the store is in a region that is shopped bimonthly, the title is "Jan-Feb 2005", "Mar-Apr 2005", etc.
There's a dozen ways to do this, of course. A very basic if/else statement covers it nicely. (For non-PHP folks, date($format, $timestamp) formats a date string. M is the three-letter abbreviation of the month name and Y is the year.):
One Way:
Code:
$title = '';
if($this->region_shop_cycle == REGION_SHOP_CYCLE_BIMONTHLY) {
$title = date('M', $begin) . '-' . date('M Y', $end);
} else {
$title = date('M Y', $end);
}
Then I realized that the bimonthly title is the same as the quarterly title, but with the begin month prepended. Here's what I ended up writing:
Code:
$title = '';
if($this->region_shop_cycle == REGION_SHOP_CYCLE_BIMONTHLY) {
$title .= date('M', $begin) . '-';
}
$title .= date('M Y', $end);
Too much optimization? Not enough? Certainly stopping to forumize about it is expensive timewise. But I'm trying to learn something about my coding style, here: I spent the extra time rewriting the first version into the second one, but then I realized that both are sufficiently clear. If the date format changes, both logic paths still have to be changed, so I don't think I gained anything in terms of maintenance.
Doing partial transforms of data instead of explicit branching has elegant uses, but in this particular case I think I made the code more subtle, not more elegant.
Thoughts?