Ideas for a refactoring with traits?

Jason Johnson jason.johnson.081 at gmail.com
Thu Sep 6 16:08:59 UTC 2007


Keep in mind that Traits are not at the same level as inheritance, for
instance.  I wouldn't think of traits as a means of eliminating code
duplication, even if that is how they are often sold.  I would try to
keep the mind set that Traits are.... traits.  That is "am I working
here with a new entity? (create a class) An entity that has various
specializations? (create subclasses) Or am I working with a trait that
many unrelated classes have? (trait)".

If you use this mindset then I think you can find elegant places for
traits when they are needed.  If we go around looking for duplicated
code and stick a trait in there we will end up with a mess.  There are
many ways of eliminating duplicate code, you don't need traits for
this.

Having said that, I would like to see what you've done with Magnitude
as that is a very obvious trait in my opinion.  To find a trait look
for classes that seem to be inherited to the wrong place, purely to
gain some interface or something.  A trait could be hiding there, or
it could simply be a case that needs normal traditional refactoring.

The other thing I would personally keep in mind is that traits
probably work best when kept small.  For example, in Magnitude you
basically just have equality and comparison.  I would even break that
up to an equality trait (for things which can be equal) and a
comparison trait (for things which can be compared).  If you get a
trait that is as big as a class, then I would consider that a strong
code smell.

On 9/6/07, Alejandro Gonzalez <mrgonza78 at gmail.com> wrote:
> Hi,
>   I'm working in trait aware refactorings, and I reached a point in which I
> have to use them so as to give them a try in real cases and see how suitable
> they are or see what other features should be nice to have.
>   I'm wandering through the image looking for good candidates to apply
> traits. I started (and I already finished)  with Magnitude and a few
> particular classes. Now it's time for another refactoring a little more
> ambitious, but not many ideas have came out of my mind so far :(
>   Stream and Collection (I know the latter was already refactored) are good
> candidates, I'll start looking at them, but I was wondering if there wasn't
> other projects/frameworks like Seaside, Magritte, Morph? or something else,
> that could benefit from traits.
>   I'm not looking to implement something with traits from scratch, I just
> want to take a set of classes, methods (or traits, why not) and restructure
> code by applying refactorings instead of doing it manually.
>    Anyone having an idea, please let me know it. Just name a class and a
> couple of methods you know are spread across several classes and where you
> think traits would help, and I'll figure out what and how to refactor.
>   Thanks in advance
>   Alejandro
>
>
>
>



More information about the Squeak-dev mailing list