Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Get DoB Bounds from Age Range

by wrog (Friar)
on May 07, 2015 at 17:57 UTC ( [id://1126022]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Get DoB Bounds from Age Range
in thread Get DoB Bounds from Age Range

Re
suitable for use in SQL queries, e.g., WHERE dob < ? AND dob > ?
Just as a matter of general programming style, I've found that life is improved if you get into the habit of always, always thinking in terms of half-open intervals and then write your code/libraries accordingly, e.g., here you expect that what people will actually want is to do queries of the form:
suitable for use in SQL queries, e.g., WHERE ? <= dob AND dob < ?
(note also that this way the parameters have the lower bound first, which is what people generally expect; also this phrasing makes it more immediately visually obvious that dob is supposed to be between the two limits ... anything you can do to help the reader is a win).

That way, if you're doing two separate queries on "0 to 10" (which includes 0 but not 10) and "10 to 20" (which includes 10 but not 20) that gets you exactly "0 to 20" and you're not forgetting to include 10 or including it twice. Lots of fencepost errors go away without your having to think about it, and if you need to know how many things are in a range, you just subtract ("20 to 30" has 10 things in it because 30-20=10).

And then you get rid of the inclusive/exclusive parameter entirely (because if everybody else using the library follows this convention, then they can just add 1 day to either of the upper or lower bound dates they get back as they need to, and in their own code it'll be obvious what they're doing; but, chances are, they won't be needing to do that at all because the half-open interval will have been the Right Thing in the first place).

Also with respect to your defaults, I'm pretty sure the original respondent meant

my $max = shift || $min;
except in my world (with half-open intervals) it's
my $max = shift || ($min + 1);
which then makes it obvious that if you leave off the second parameter, you're going to get back a 1-year interval (which will have exactly 365 days in it because it will include, say, 2014-May-17 and not 2015-May-17).

Replies are listed 'Best First'.
Re^4: Get DoB Bounds from Age Range
by over2sd (Beadle) on May 08, 2015 at 01:39 UTC

    I have updated the function with an attempt to put your comments into action.

    Thank you for your comments.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1126022]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2024-04-19 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found