in reply to Get DoB Bounds from Age Range

Oh right, one other thing:
Do not call now() at all.
Make the caller pass in a date; have it be the first argument.

It's bad enough that you're calling now() twice, which could introduce really subtle bugs that only get triggered very intermittently (imagine someone using this function right at midnight).

Also, consulting the system clock is a system call and all system calls are potentially expensive in certain contexts; perhaps not this one, but you still want to get into the habit of thinking of the system clock as something you don't look at unless you need to.

And whoever is calling your function will need to be thinking about which "now()" they actually want to be using. E.g., in a webserver context there's a request time and, chances are, the framework has already made the system call for you, and life gets simpler if you take the position that everything w.r.t. processing that request is happening in a single instant.

Replies are listed 'Best First'.
Re^2: Get DoB Bounds from Age Range
by over2sd (Beadle) on May 11, 2015 at 19:18 UTC

    That is a good point. Forcing^H^H^H^H^H^H^H Allowing the user to pass a reference date in also makes the function more flexible, letting them calculate ages at an arbitrary point in time.

    Thank you.