7E.3. Encapsulating Decisions and Algorithms

A function is an expert on some small thing. For example, a function that computes square roots is an expert on how to compute square roots. No other part of the program needs to be concerned with that; if you want to compute a square root, ask the expert to do that for you.

A programmer needs to make decisions when writing software, such as the value of a constant or how to represent a particular piece of information. Functions can also be experts on those decisions. Decisions are encapsulated in function bodies.

A given decision might need to be known by more than one function, but as a software designer, you try to minimize the number of functions that need to know the result of a decision. That way, if you change the decision (and you will change decisions), you have only a small number of functions to modify. We will see more on that later when we look at abstract data types.