Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2004-04-16 13:46:32


From: David Abrahams <dave_at_[hidden]>
> David Abrahams <dave_at_[hidden]> writes:
> > "Powell, Gary" <powellg_at_[hidden]> writes:
> >> Rob Stewart <stewart_at_[hidden]> writes:
> >
> >>>> I think readability is more important. Nobody but pedants have
> >>>> problems with string + string. Likewise I think:
> >>>>
> >>>> d + 2*months
> >>>>
> >>>> or whatever is better than the functional-looking alternatives.
> >>
> >>>If there was universal acceptance of what it means to add two
> >>>months to a date, I might agree. Given the imprecise notion of
> >>>what it means to add two months, however, I disagree.
> >>>"next_month" or "increment_month" versus "add_month" better
> >>>distinguish the imprecision.
> >>
> >> Here I totally agree with Rob, adding months is a non-intuitive operation and
> >> calling out the precision helps the readability of the program.
> >>
> >> It makes perfect sense to do
> >>
> >> d + (30 * days)
> >>
> >> but not with Months.
> >
> > It makes perfect sense if you know the semantics the library assigns
> > to months.

A maintenance programmer, may infer any meaning that seems
personally meaningful to such expressions. Sure, that programmer
would be wrong to make assumptions, but given that adding 2 *
months could mean any of a number of things, for which there
isn't complete agreement, it makes such syntactic sugar
dangerous.

> > Does the library have a concept of a month with no day, e.g. January

I don't know.

> > of 2004? If so, I'd be happy to say
> >
> > date(d.month() + 2*months, d.day)
> >
> > That, too, calls out the fact that adding months to dates has a
> > dubious meaning.

That makes the result of the expression quite explicit. I can't
see any problems with that approach. I'd expect an exception if
the resulting month doesn't have the requested day. OTOH, if
date's ctor were to take an extra argument, or if there was a
date factory function supplying the different behavior, you could
specify that you want clipping to the last day of the designated
month:

   date(d.month() + 2 * months, d.day(), clip_to_eom)
   make_date_clipped_to_end_of_month(d.month() + 2 * months,
      d.day())

> No comment?

I read it before, but you made me take another look. I guess I
had something to say after all.

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk