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.

Role Suggesting Temporary Variable NameCollecting Temporary Variable stores the intermediate results of a computation. CachingTemporary Variable improves performance by eliminating redundant computation. ExplainingTemporary Variable makes methods containing complex expressions easier to read. ReusingTemporary Variable correctly executes methods containing side-effecting expressions.What do you call a temporary variable?There are two important dimensions to communicate about a variable. The first is its type.Readers wishing to modify code need to know what responsibilities are assumed for an objectoccupying a variable. The second important dimension is role, that is, how the object is used in thecomputation. Understanding the role is important to understanding the method in which thevariable is used. Different kinds of variables require different naming treatment to communicatetype and role.Temporary variables communicate their role by context. If you are staring at:| sum |sum := 0.... sum ...you cannot possibly be confused about its type. Even if a temporary variable is initialized by aexpression, you will be able to understand its type as long as the expression is well written:| bounds |bounds := self bounds.... bounds ...Role, on the other hand, requires explicit communication. You have probably had the experienceof reading code whose temporary variables had names like "a", "b", and the ever popular "temp".As a reader, you have to go through the code holding these useless names in your head until thelight comes on. "A ha! 'b' is really the left offset of the parent widget."Name temporary variables for the role they play in the computation. Use variable naming asan opportunity to communicate valuable tactical information to future readers.<strong>Coding</strong> <strong>Patterns</strong> page 83 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!