Re: Number #asString

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Number #asString

Thomas Gagne
Doesn't it really end up as 3 message sends because it has to be written as:

^self printString last isDigit
    ifTrue: [ self printString ]
    ifFalse: [self printString copyUpTo: (self printString last) ]

Joseph Bacanskas wrote:

> Hi Thomas:
>
> I don't think it is a problem.  At the very least, I'd change ours to:
>
>         ^self printString last isDigit
>                 ifTrue: [self printString]
>                 ifFalse: [self printString
>                         copyUpTo: tempString last]
>
> IMHO, 1 temp is more expensive than 2 message sends; especially since the
> compiler will be holding on to the result of the first send anyway.  I could
> be wrong on this.  In any case, balanced #ifTrue:ifFalse: are less bug-prone
> and clearer to read.
>
> Cheers!!
>
> On Wednesday 19 September 2001 10:08 am, Thomas Gagne wrote:
> > I just discovered, Swazoo writes its own Number #asString method, which
> > over-wrote our own.  Luckily, I caught it before it caused us any problems,
> > and I'm curious what you need it for.
> >
> > The difference between Swazoo's and eFinNet's is ours removes the trailling
> > suffix.  So, scaled decimal numbers convert as: "100.00" instead of
> > "100.00s".
> >
> > Is that a problem for you?
> >
> > Swazoos:
> >
> > asString
> >
> >       | s |
> >
> >       s := WriteStream on: ''.
> >       self printOn: s.
> >       ^s contents
> >
> > eFinNet's
> >
> > asString
> >       "convert aNumber to its string equivelant, without an exponent
> >       character
> >
> >       Added by Thomas Gagne, at the suggestion of
> >       Chris Lopeman
> >       [hidden email]
> >       Object Link, Inc.
> >       "
> >       "18.78d asString"
> >       "2135 asString"
> >
> >       | tempString |
> >
> >       (tempString := self printString) last isDigit ifFalse: [tempString :=
> > tempString copyUpTo: tempString last].
> >       ^tempString
>
> --
> Joseph Bacanskas, Principal Software Engineer
> eFinNet, Corp

--
.tom





Reply | Threaded
Open this post in threaded view
|

Re: Re: Number #asString

Ken Treis-4
This is interesting.  I only find one sender in Swazoo of #asString that
looks like it's sent to a Number.  And here's where I find it: it's in
Swazoo-Tests, and it's a helper method:

ResourceTest>>basicGetUri:host:port:
    ....
    port notNil ifTrue: [ws nextPutAll: ':', port asString].

This could easily be removed / replaced with

    port notNil
        ifTrue:
            [ws
                nextPut: $:;
                print: port].

I've made this change and removed Swazoo's (Number>>asString), and all
of the tests still run.

----

As for the eFinNet version of #asString... this would be much better
solved using the inheritance hierarchy that already exists.  Why branch
on the last character of a printString when that character's very
presence depends on the class?

IOW, implement these methods:

Number>>asString
    ^self printString

FixedPoint>>asString
    ^self printString withoutLast: 1

SequenceableCollection>>withoutLast: anInteger
    ^self copyFrom: 1 to: self size - anInteger

There's no branching based on the last character, because you've
implemented it in the proper places.  And there's no storing of the new
string in a temporary for the sake of examining its size, because you've
pushed the behavior into SequenceableCollection.


Ken Treis
[hidden email]