I stated earlier that I tend to favor a top-down, macro algorithm as a starting point in the program design process. After all, you have to understand the problem before you can solve it. Still, the time will come when we have to look at the micro issues that must be addressed to solve the problem. As we proceed from the general overview of the program to its specifics, we're increasing the details, or granularity, of our view. Stated another way, we're moving from the general to the specific.
We can use the five programming steps as the starting point for our macro view of the design process. Next, we can take each of these steps and provide more detail about what each step involves. This process is called sideways refinement. An example will help you to understand the process.
Suppose that you have a user that has a database file that holds all the user's appointments in it. These appointments are stored in the database in chronological order by the date of the appointment. The user, however, wants to be able to view the appointments in alphabetical order, based on the last name of the person the user is meeting. Let's see how we might use a sideways refinement approach to designing a solution.
Sideways Refinement of the Initialization Step
We already know that the user has a database of the appointments. We also know the user wants a list of the appointments in alphabetical order by last name. This is our macro view of the algorithm.
So, what should the Initialization step do? Well, it seems clear that we need to open the appointments database. We also need a Visual Basic .NET form to display the results after the appointments have been sorted. We'll assume that our task is a little easier because we know where the database is located on the network, and we can also determine the user's name and password from the same database as soon as the user starts the program. With this information in mind, our first sideways refinement might look like the one shown in Figure 3.1.
Figure 3.1 A sideways refinement of the Initialization step.
Notice how the detail increases as we move sideways from left to right in Figure 3.1. In the figure, the sideways refinement presents a list of subroutines or functions that the step needs to accomplish its task. These subroutines and functions will be similar to those we discussed in Chapter 2, "The Basics of Object-Oriented Programming." Each small routine has a specific task to accomplish.
We can continue to further refine each step by the use of pseudo code. Pseudo code is an algorithm for the routine stated using an English-like syntax. For example, we might take the IsValidUser() routine and write its pseudo code as follows:
IsValidUser() -_ If CurrentUserName Not in ValidUserList Display Invalid User Error Message Terminate Program Else Return ValidUserIDNumber End
Notice how the pseudo code describes what the routine is supposed to do, but without the formal syntax of the programming language being used. Pseudo code is not a language-based syntax. Pseudo code is an algorithmic statement of what the routine is supposed to do.
The real advantage of pseudo code is that, because it's very English-like, you can share it with the (non-programming) user in the design stage to see whether you're addressing the problem correctly. This review process with the user at an early stage of the design is a huge benefit to both of you. Catching design errors and program changes early in the process prevents a lot of nasty problems down the road. Involving the user at the outset usually results in a better program produced in less time with fewer changes later on. It's a win-win approach to program design.
Once you have an understanding of the purpose of the routine stated in pseudo code, translating the pseudo code into actual program code is pretty simple. Good design promotes easier program development.
I encourage you to stop reading at this point and take a moment to do a sideways refinement of the remaining four program steps for our appointment program. You might also take a crack at trying to write the pseudo code for each refinement you create in the list. Even though I realize that most of you won't take the time to do this, there are a few of you die-hards who will. Trust me...it's well worth the effort.