sending #respondsTo:

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

sending #respondsTo:

Sophie424
Is it considered bad practice to use #respondsTo:? If so, why?

Lint flags it as a "questionable" message.

Thanks - Sophie



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: sending #respondsTo:

David Mitchell-10
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 <[hidden email]> wrote:

> Is it considered bad practice to use #respondsTo:? If so, why?
>
>  Lint flags it as a "questionable" message.
>
>  Thanks - Sophie
>
>
>
>  _______________________________________________
>  Beginners mailing list
>  [hidden email]
>  http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners