13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Qualified Subclass NameYou have created a Simple Superclass Name.What should you name a new subclass?One way to name classes is to give each a unique name, indicative of its purpose. Unique namesgive you the opportunity to convey a lot of information without creating names that are long andunwieldy.This is exactly the right thing to do if the names are in common use. Array is a subclass ofCollection, because lots of software engineers guess correctly about the nature of the class byreading “Array”. Number is a subclass of Magnitude, because Number-ness is far more importantto communicate than Magnitude-ness. String is a subclass of Collection, because everyone knowswhat a String is.In general, if I am using inheritance strictly for code sharing, but the role of the subclass is differentthan the role of the superclass, I go back to Simple Superclass Name.More often, the inheritance structure of your code is important for a reader to understand,particularly where a subclass is both conceptually a variation on the superclass and they shareimplementation. The two pieces of information you need to communicate are:How the new class is the sameHow the new class is differentYou can communicate how the new class is the same by naming some superclass. It need not be theimmediate superclass, if some distant ancestor communicates more clearly.You can communicate how the new class is different by finding a word that accurately highlightsthe reason the new class isn’t just the superclass.Name subclasses in your hierarchies by prepending an adjective to the superclass name.For example:OrderedCollection is a Collection in which elements are OrderedSortedCollection is a Collection in which elements are Sorted. Even though SortedCollectionsubclasses OrderedCollection you wouldn’t call it SortedOrderedCollection. The choice tosubclass OrderedCollection is strictly for implementation reasons, not because the twocollections play similar roles.LargeInteger is an Integer that takes many bits to represent.<strong>Coding</strong> <strong>Patterns</strong> page 124 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!