Mantis - Squeak
Viewing Issue Advanced Details
2143 Collections minor always 10-30-05 18:36 01-18-06 19:21
johnmci  
 
normal  
closed  
fixed  
none    
none 3.9  
0002143: usage of second/third/fourth/fifth/etc
Use of method accessors that resolve to at: are expensive and contain uneeded code.

SequenceableCollection>>fifth for example
fifth
    "Answer the fifth element of the receiver.
    Raise an error if there are not enough elements."

    ^ self checkedAt: 5

SequenceableCollection>>checkedAt: index
    index > self size ifTrue: [self error: 'not enough elements'].
    ^ self at: index

Note the index check, but then we fall into self at: which I believe all at: do an index anyways to protect us from disaster. I'll also note that foo fifth under an error condition would have raised error: 'not enough elements': versus saying foo at: which would have raised error: 'subscript is out of bounds: '

at: index
    "Primitive. Assumes receiver is indexable. Answer the value of an
    indexable element in the receiver. Fail if the argument index is not an
    Integer or is out of bounds. Essential. See Object documentation
    whatIsAPrimitive."

    <primitive: 60>
    index isInteger ifTrue:
        [self class isVariable
            ifTrue: [self errorSubscriptBounds: index]
            ifFalse: [self errorNotIndexable]].
    index isNumber
        ifTrue: [^self at: index asInteger]
        ifFalse: [self errorNonIntegerIndex]

I've included a changset to change the second/third/.../ninth methods to just invoke at:
method first is a bit different and I left it unaltered for now.

However one might look at first and see if the logic there is needed?
Some implementations invoke self emptyCheck, but is that any different than saying at: 1 ?
 JMMRemoveExtraIndexCheck.1.cs [^] (1,695 bytes) 10-30-05 18:36

Notes
(0003535)
MarcusDenker   
01-18-06 19:21   
will be in 3.9a6715