The Nightstar Zoo

Nightstar IRC Network - irc.nightstar.net
It is currently Mon Nov 20, 2017 7:58 pm

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: What's clearer?
PostPosted: Mon Jun 06, 2005 11:13 am 
Offline
Nightstar Graveyard Daemon
User avatar

Joined: Mon Jun 03, 2002 8:30 pm
Posts: 1071
Location: Wouldn't you rather observe my Velocity?
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?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 06, 2005 10:29 pm 
Offline
Entertainment
Entertainment
User avatar

Joined: Sat May 11, 2002 7:55 pm
Posts: 867
Location: 59°20'N 18°03'E
I'd go with the first version in case the "ending month format equals single month format" part of the specification ever changes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 07, 2005 10:20 am 
Offline
Nightstar Graveyard Daemon
User avatar

Joined: Mon Jun 03, 2002 8:30 pm
Posts: 1071
Location: Wouldn't you rather observe my Velocity?
Do you think that's likely?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 09, 2005 12:43 am 
Offline
Entertainment
Entertainment
User avatar

Joined: Sat May 11, 2002 7:55 pm
Posts: 867
Location: 59°20'N 18°03'E
No idea. There are hundreds of human languages whose date formats I know nothing about. 8-)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group