Structuring a Visual Basic Program
One of the goals in modern programming is to produce modular programs — that is, programs composed of small, discrete units of code, each of which acts independently to accomplish a particular task. Toward this end, Visual Basic enables you to organize your code into procedures, modules, and workbooks.
A program built from small, reliable components has many advantages over a program built as a single structure. Well-structured modular programs are:
- Easy to write, because complex problems are broken down into a series of discrete, easy-to-understand tasks.
- Easy to read, because a top-level procedure typically contains only a series of calls to descriptively named lower-level procedures.
- Easy to debug, because each task is accomplished in one procedure, and you can easily isolate the source of a problem.
- Efficient, because code that accomplishes a common task appears in only one procedure that's called many times, rather than being duplicated in many places throughout a program.
- Easy to modify, because code that accomplishes a specific task appears in only one place in a program. Therefore, if you need to modify this code, you need only make the change in one place.
- Reliable, because you can build up a library of dependable procedures and then construct your programs using these tested, proven components.
- Robust, because you can hide data from all parts of a program that don't require access to the data, and thereby reduce the risk of accidentally making changes to the data.
- Easy to manage and distribute, because you can store and distribute sets of related or frequently used procedures as a unit.
The rest of this chapter will describe how to organize your code into these components and how to control the interaction among procedures stored in different parts of a program.