Navigation:  Appendix B: Dolphin Pattern Book > Method Patterns >

Temporary Variable Role

Previous pageReturn to chapter overviewNext page


You are creating a New Method and see that sometimes it is necessary or convenient to store the value of an expression so that it can be used later within the method.

Also Known As

Role Suggesting Temporary Variable Name


Use a method temporary variable in one of the following roles:

Hold the result of an expression in a temporary variable if the result will otherwise be lost by the time you need it, or if it will be referenced more than once within the scope of a method.
Use a temporary to hold a count/collection which accumulates gradually as a method progresses.
Simplify a complex expression by assigning an identifiable sub-expression to a temporary, and using the temporary in the complex expression.

Use Word Capitalization and a role suggesting name when naming a temporary variable just as you would when adding an Instance Variable Name to a class. Avoid using a temporary variable for more than one purpose within a method since this will most likely just lead to confusion.

Known Uses

Collection>>asArray uses a temporary variable answer to build an Array containing the same elements as the receiver:


  "Answer an Array whose elements are those of the receiver.

  (ordering is that of the #do: operation as implemented by the receiver)."

  | answer i |

  answer:= Array new: self size.

  i := 1.

  self do: [:e |

      answer at: i put: e.

      i := i + 1].



Related Patterns

Instance Variable Name, Instance Variable Role