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.

Temporary VariableA Composed Method needs temporary storage.How do you save the value of an expression for later use within a method?A state-less language like FP contains no notion whatever of a variable. If you need the value of anexpression, you evaluate the expression. If you need it in many places in a program, you evaluate itmany places.While the abstract properties of a state-less language might be attractive to a language designer,practical programs use variables to simplify the expression of computation. Each variable hasseveral distinguishing features. The scope of a variable is the textual area within which it can beused. The extent of a variable defines how long its value lasts. The type of a variable is signatureof messages sent to it.Long extent, wide scope, and large type all make variables difficult to manage. If all three factorsare present in many variables, you have a program that can only be understood in its entirety.Limiting the scope, extent, and type of variables wherever possible produces programs that areeasier to understand, modify, and reuse.Another language design decision is whether to require the explicit declaration of variables. Earlylanguages like FORTRAN detected the presence of variable automatically. ALGOL and itsdescendants required explicit declaration. Explicit declaration puts a burden on the programmerwriting the program, but pays off when someone else needs to understand the program. Thepresence and use of variables is often the first place a reader begins.Create variables whose scope and extent is a single method. Declare them just below themethod selector. Assign them a value as soon as the expression is valid.Collecting Temporary Variable saves intermediate results for later use. Caching TemporaryVariable improves performance by saving values. Explaining Temporary Variable improvesreadability by breaking up complex expressions. Reusing Temporary Variable lets you use thevalue of a side-effecting expression more than once in a method.<strong>Coding</strong> <strong>Patterns</strong> page 78 of 147 9/30/2006

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

Saved successfully!

Ooh no, something went wrong!