4.3 Abstraction

More often than not, even with experienced programmers, an algorithm grows and grows until it becomes ``messy'' and too lengthy. At this point, we can ``abstract'' portions of the algorithm as an abstract operation, and thus hide the details in the definition of the abstract operation.

The key to abstraction is to identify a block of code (within an algorithm) that is somewhat self contained and related. This process, once again, requires some experience. There are methods to evaluate how self contained is a block of code based on the usage of variables. We will revisit this topic in the module that discusses subroutines.

As an example, sometimes we come up with algorithm 3 first, then decide that it is too much to read. Then, we see a self contained block of code as algorithm 2, and extract that out of the main algorithm. Once we extract the self contained block of code, we replace it with a one sentence description ``find a prime factor $ f$ of $ n > 1$ ''. We also end up with a second algorithm as algorithm 2. The main algorithm, then, becomes algorithm 1, which is considerably easier to read than algorithm 3.

Copyright © 2006-09-26 by Tak Auyeung