From: Patricia Shanahan on
Lothar Behrens wrote:
> Hi,
>
> I have implemented a trim function and documented it as follows:
>
> /** \brief Trim trailing spaces.
> *
> * Removes the trailing spaces in the string.
> */
> void trim();
>
> But actually I have written a test to ensure it's correctness and
> assumed it would remove leading and trailing whitespaces.
> I know there are rtrim and ltrim in some languages or implementations.
> So I think I am wrong with my implementation as
> I have expected other behavior than I have initially implemented.
>
> What would you expect from this function?

Do you have a use-case or equivalent that motivates creation of this
function? If you do have a use-case, what behavior does it need? If not,
why does the function exist?

After you have decided exactly what the function needs to do, you can
pick a name that reflects that functionality.

Patricia
From: Lothar Behrens on


Thad Smith schrieb:
> Lothar Behrens wrote:
> > On 28 Mrz., 19:16, Ben Pfaff <b...(a)cs.stanford.edu> wrote:
> >> Lothar Behrens <lothar.behr...(a)lollisoft.de> writes:
> >>> I have implemented a trim function and documented it as follows:
> >>> /** \brief Trim trailing spaces.
> >>> *
> >>> * Removes the trailing spaces in the string.
> >>> */
> >>> void trim();
> >> [...]
> >>> What would you expect from this function?
> >> I would expect it to do what it is documented to do.
>
> > Yes,
> >
> > But with a trap I fell into (http://msdn.microsoft.com/de-de/library/
> > system.string.trim(VS.80).aspx), I assumed a different behavior.
>
> Are you saying you wrote a function to remove trailing spaces, then assumed it
> did the same as someone else's trim function instead of what you implemented and
> documented?

Yes. I simply forgot, that my trim function 'only' trims trailing
spaces. Assumed it will doing both
and therefore wrote the test and it failed :-)

It will be a typical trap when using different languages and doing
different things with the same function name.

>
> > I should indeed change the documentation, code and the test to match a
> > more common and known function :-)
>
> Whether you do that depends on your objectives.
>
> One of the problems here is using a very generic name (trim), which is subject
> to reuse for different purposes with the resulting confusion. Consider
> trimTrailingSpaces() for the function you originally described. Then document,
> code, test, and use correspondingly.

That is what I want to ask. The generic intention one thinks this
function may do.

I'll change the function name to be more clearly about what it will
do. (ltrim/rtrim/trim)
Also I should think about using isspace() instead of ' '.

Thank you all.

Lothar