[Newbies] sending #respondsTo:

David Mitchell david.mitchell at gmail.com
Sat Apr 12 01:26:13 UTC 2008


Responds to certainly has its place, but you want to favor polymorphism first.

Say you need to move some animals around but some of the animals are
fish and some are birds.

You might have client code that looks like this (but it would be bad)

anAnimal respondsTo: #swim ifTrue: [anAnimal swim] ifFalse: [anAnimal fly]

It would be better to come up with a common message name and just send
the message

anAnimal move

This would require two methods:
Fish>>#move
Bird>>#move

The beauty of this is that your client code doesn't change if you've
got a flightless bird subclass (Penguin) or a flying fish...

(I've never worked on zoological code, but I have refactored code like
the above for a company with three different billing systems that had
incompatible protocols.)

On Fri, Apr 11, 2008 at 7:48 PM, itsme213 <itsme213 at hotmail.com> wrote:
> Is it considered bad practice to use #respondsTo:? If so, why?
>
>  Lint flags it as a "questionable" message.
>
>  Thanks - Sophie
>
>
>
>  _______________________________________________
>  Beginners mailing list
>  Beginners at lists.squeakfoundation.org
>  http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list