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.

Intention Revealing SelectorYou may be naming a method: a Complete Creation Method, Conversion Method, ConverterCreation Method, or Execute Around Method. You may be naming a message: DecomposingMessage, Choosing Message, or Intention Revealing Message. You may be implementing DoubleDispatch or a Send Back.How do you name a method?You have two options in naming methods. The first is to name the method after how itaccomplishes its task. Thus, searching methods would be called:Array>>linearSearchFor:Set>>hashedSearchFor:BTree>>treeSearchFor:The most important argument against this style of naming is that it doesn’t communicate well. If Ihave code that invokes three other objects, I have to read and understand three different pieces ofimplementation before I can understand the code.Also, naming methods this way results in code that knows what kind of object it is dealing with. If Ihave code that works with an Array, I can’t substitute a BTree or a Set.The second option is to name a method after what it is supposed to accomplish and leave “how” tothe various method bodies. This is hard work, especially when you only have a singleimplementation. Your mind is filled with how you are about to accomplish the task, so it’s naturalthat the name follow “how”. The effort of moving the names of method from “how” to “what” isworth it, both long term and short term. The resulting code will be easier to read and more flexible.Name methods after what they accomplish.Applying this to the example above, we would name all of the messages “searchFor:”.Collection>>searchFor:Really, though, searching is a way of implementing a more general concept, inclusion. Trying toname the message after this more general “what” leads us to “includes:” as a selector.Collection>>includes:Here’s a simple exercise that will help you generalize names of messages with a singleimplementation. Imagine a second, very different implementation, then ask yourself if you’d givethat method the same name. If so, you’ve probably abstracted the name as much as you know howto at the moment.Once you name a method, write its body using Composed Method. Format the selector in themethod with an Inline Message Pattern. Add a Collecting Parameter if necessary to collect results.<strong>Coding</strong> <strong>Patterns</strong> page 42 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!