From: Daniel Frey (daniel.frey_at_[hidden])
Date: 2003-10-19 05:33:23
Thorsten Ottosen wrote:
> "Daniel Frey" <daniel.frey_at_[hidden]> wrote in message
>>s.trim() is what IMHO would be an intuitive interface to changes s's
>>state. Sadly, it's not possible to provide it. It's the old question
>>whether or not a function should modify its arguments. Of course you are
>>right that the STL uses a different notion, but that's not the one and
>>only argument as you seem to suggest. From my experience (and I would
>>like to hear whether others share this view), users don't expect x to
>>change when they write f(x).
> they should not expect that to be the only way to read f(x). Think of
> several C library functions; think of Pascal procedures. It's beyond me how
> you can state that.
I just state that I observed this. I don't want people to do this, I
don't teach them to do this, I just observed that they actually do it.
Shouldn't I state that? And especially your examples are a common source
of trouble because C library functions, etc. do modify their parameters.
In theory people read the docs so it shouldn't be a problem, but in
practice, well, ... :)
>>The most problematic portion of the
>>current interface is in fact, that it allows you to write:
>>std::string s = "Hello, world!";
>>std::string t = trim( s ); // Ouch!
> well, in this case, nothing is trimmed. If the user has read the "Usage"
> section he will easily
> know that trim etc changes the argument.
>>It's IMHO very hard to find these kinds of errors...
> I really don't know. I think the hardest errors I had to debug was index
> beyond array bounds from some C library.
> I and why is it an error? What about this error:
> trim( s ); // bug: s is not changed
There is a difference: If you write
trim( s );
[copying version assumed] you expect that 's' is now trimmed. And you
usually check this as when writing code, you run it to see if it works.
If it's not trimmed, you look for the reason and it's IMHO not hard to
find this bug now. But if you write
std::string t = trim( s );
[modifying version assumed], this has the effect you expect, namely 't'
contains a trimmed version of 's'. The problem is, that this has a side
effect on 's'. In a larger code base, these side effects sometimes show
up very late and some of them are really hard to find. Well, of course
this is just my personal experience.
-- Daniel Frey aixigo AG - financial training, research and technology Schloß-Rahe-Straße 15, 52072 Aachen, Germany fon: +49 (0)241 936737-42, fax: +49 (0)241 936737-99 eMail: daniel.frey_at_[hidden], web: http://www.aixigo.de
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk