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...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Decomposing MessageYou are using a Message to break a computation into parts.How do you invoke parts of a computation?A Choosing Message gets work done. It is the equivalent of a case statement in procedurallanguages. Depending on a circumstance, different code is invoked.Another way messages are used is to break a computation down into pieces. As you are writing thecode, you don’t think about possible variations. A method is getting too big and you need to breakit into parts so you can understand it better. Alternatively, you may have noticed that two or moremethods have similar parts and you’d like to put the parts in a single method.This is very similar to the way subroutines are used in procedural programming. You take a bigroutine and break it into pieces.<strong>Smalltalk</strong> code reveals a much more aggressive attitude towards decomposing code than otherlanguages. Most style guides say “keep the code for a routine on one page”. Most good <strong>Smalltalk</strong>methods fit into a few lines, certainly less than ten and often three or four.Partly this is possible because the abstractions <strong>Smalltalk</strong> provides are higher level than what youfind in most languages. You don’t spend three or four lines expressing iteration, you spend oneword. Partly it is possible because <strong>Smalltalk</strong>’s programming tools let you manage more smallerpieces easily.Send a message to “self”.The classic example of this from the original <strong>Smalltalk</strong> image was:Controller>>controlActivityselfcontrolInitialize;controlLoop;controlTerminateLater, these messages all became Choosing Messages, because they were all overridden a hundreddifferent ways.Use Composed Method to break the method into pieces. Give each method an Intention RevealingSelector. Use Intention Revealing Messages to communicate intent separate from implementation.<strong>Coding</strong> <strong>Patterns</strong> page 40 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!