Tip of the day #20

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

Tip of the day #20

Fernando Rodriguez
Hi,

I just got this tip from Dolphin, but I'm not sure what it means:

--------------------------------------------------------------------------
Avoid creating hard references to classes as this make it harder to
later subclass that class for specialization purposes. Always add a
'constants' method the sole purpose of which is to return the class.
----------------------------------------------------------------------------


Reply | Threaded
Open this post in threaded view
|

Re: Tip of the day #20

Sean M-3
> --------------------------------------------------------------------------
> Avoid creating hard references to classes as this make it harder to
> later subclass that class for specialization purposes. Always add a
> 'constants' method the sole purpose of which is to return the class.
> ----------------------------------------------------------------------------

I read that to mean:

Suppose you have an class which uses an instance of an "Activator"

doSomethingInteresting
    | activator |
    activator := Activator new.
    ...

instead, take out the hard reference to the class, and place it inside it's
own message

newActivator
    ^Activator new

doSomethingInteresting
    | activator |
    activator := self newActivator.
    ...


now a subclass can override #newActivator rather than havingto override
doSomethingInteresting, and risk duplicating various pieces of code?

Or am I off the mark?


Reply | Threaded
Open this post in threaded view
|

Re: Tip of the day #20

Chris Uppal-3
Sean, Fernando:

> instead, take out the hard reference to the class, and place it inside
> it's own message
>
> newActivator
>     ^Activator new
>
> doSomethingInteresting
>     | activator |
>     activator := self newActivator.
>     ...
>
>
> now a subclass can override #newActivator rather than havingto override
> doSomethingInteresting, and risk duplicating various pieces of code?

I think that the intent of the tip is that it should be expressed as:

    activatorFactory
        ^ Activator

    doSomethingInteresting
        | activator |
        activator := self activatorFactory new.
    ...

since otherwise there doesn't seem to be any reason for the advice to put the
factored-out method into the 'constants' category.

FWIW, I think the advice is wrong as stated.  I think the code is much better
the way you have factored it.  Of course, there's nothing to prevent you using
both factorings:

    activatorFactory
        ^ Activator

    makeActivator
        ^ self activatorFactory new.

    doSomethingInteresting
        | activator |
        activator := self makeActivator.
        ...

but that looks like overkill to me, unless there is a need to refer to
Activator separately from its use in #makeActivator.

    -- chris
    ...