Accessing Information**
An Elementary Coding Pattern

Byron Weber Becker
University of Waterloo
Waterloo, Ontario, N2L 3G1
bwbecker@uwaterloo.ca

Suppose that you are performing a calculation in a program. Where should the information for this calculation be obtained? There are a number of possibilities:

You want your program to be maintainable, flexible, reliable, and reusable with a user interface which is a pleasure to use. Maintainability and reusability are enhanced by keeping information close to where it is used. Flexibility and reusability are enhanced by explicitly passing information via parameters. File input and output is an error-prone process and is slow relative to other activities. Users are unreliable and don't want to be bothered for unnecessary information.

Therefore, prefer to obtain information from nearby sources such as defined constants, local variables and method parameters. If the required information is not available there, try to obtain it from an instance variable, by calling another method or from another object. Finally, as a last resort, obtain it from a file or the user.