Structured ProgrammingYou already know the best way to structure programs because you can use Microsofts Visual Basic design as a guide. The small event procedures youve seen and coded are perfect examples of the correct way to code. Dont write long routines that do everything; instead, write small code procedures that each perform only one task, such as respond to a users keystroke. If the keystroke is to trigger a bunch of things, keep the event procedure small and call other small procedures that do the detailed work. Structured programming is a programming method you use to break long programs into numerous small procedures, putting off the details as long as possible. For example, suppose that you need to perform the following tasks when the user clicks a Reconcile command button in a checkbook application: 1. Display checks next to cleared items. 2. Total the cleared items. 3. Total the uncleared items. 4. Recommend an action if the manual checkbook balance and the checkbook computer files balance don't match. 5. Print a reconciliation report. Such a detailed response to a single command button click would take many screens of code. Nevertheless, the Click() event procedure doesn't have to be many screens. Instead, you could insert a series of procedure calls that do the detailed work and keep the Click() procedure small like this: Private Sub cmdReconcile_Click () Call ClearItems () Call UnClearItems () If ChkBkIsBalanced () Then Call OutBalanceAction () End If Call ReconcilePrint () End Sub
All the procedures called by a subroutine should be as small as possible and only perform a single task, or a series of calls to other procedures. When you follow this simple advice, your code becomes a structured, manageable set of routines where each subroutine performs a single task or controls other tasks. Not only does structured programming make writing code easier, it makes managing code really simple. If your application contains a bug, you can more easily locate the bug because you follow the thread of procedures until you get to the routine that controls the logic with the bug. If your uncleared balance is incorrect, you can go directly to the procedure that computes that balance and then locate the problem without affecting lots of other code around that routine.
|
|
![]() |
![]() |
|||