isKindOf: vs. isClassX methods

Eddie Cottongim cottonsqueak at earthlink.net
Wed Oct 10 20:41:08 UTC 2001


I've noticed in many places in the image there are methods such as isColor
or isMorph that identify the 'type' of an object. (See: Object category
testing, Morph category classification). I always thought that this kind of
test was done with "isKindOf:".

isClassX method
pros: allows you to identify the 'type' of an object arbitrarily (not
strictly from the inheritance tree), faster
cons: clutter, possibly encourages a programming style that wants a
too-specific kind of object (this argument seem weaker the more I think
about it-any construct can be used poorly if you try hard enough)

isKindOf:
pros: more general & uniform
cons: slower

"Simple speed test which may or may not characterize average case usage:"
m := EllipseMorph new.
Time millisecondsToRun: [ 100000 timesRepeat: [m isHandMorph] ] 60 ms
Time millisecondsToRun: [ 100000 timesRepeat: [m isKindOf: HandMorph] ] 351
ms

Another way to look at it, isClassX puts the burden of type identification
on the object being questioned, isKindOf puts it on the object that wants to
know.

I've always stuck with isKindOf in my work beacuse I thought it was the
"right thing" to do. I'm guessing that the testing methods in Object, Morph
etc are there for speed reasons. So, as a programmer, I should start with
isKindOf and create explicit methods if isKindOf is a speed bottlenect. Am I
missing any other considerations ?

Thanks,
Eddie





More information about the Squeak-dev mailing list