Читать книгу Beginning Programming All-in-One For Dummies - Wallace Wang - Страница 37

Top-down programming

Оглавление

For small programs, organizing a program into sequences, branches, and loops works well. But the larger your program gets, the harder it can be to view and understand the whole thing. So, a second feature of structured programming involves breaking a large program into smaller parts where each part performs one specific task. This is also known as top-down programming.

The idea behind top-down programming (as opposed to bottom-up programming) is that you design your program by identifying the main (top) task that you want your program to solve.

For example, if you wanted to write a program that could predict the next winning lottery numbers, that is a top design of your program. Of course, you can’t just tell a computer, “Pick the next winning lottery numbers.” You must divide this single (top) task into two or more smaller tasks.

One of these smaller tasks may be, “Identify the lottery numbers that tend to appear often.” A second task may be, “Pick the six numbers that have appeared most often and display those as the potential future winning numbers.”

The idea is that writing a large program may be tough, but writing a small program is easy. So, if you keep dividing the tasks of your program into smaller and smaller parts, eventually you can write a small, simple program that can solve that task. Then you can paste these small programs together like building blocks, and you’ll have a well-organized big program — theoretically.

Now if you need to modify part of the large program, just find the small program that needs changing, modify it, and plug it back into the larger program, and you’ve just updated the larger program.

Ideally, each small program should be small enough to fit on a single sheet of paper or a single screen. This makes each small program easy to read, understand, and modify. When you divide a large program into smaller programs, each small program is a subprogram.

If you divide a program into multiple subprograms, you have two options for where to store your subprograms:

 Store all your subprograms in a single file. This option is fine for small programs, but after you start dividing a program into multiple subprograms, trying to cram all your subprograms into a single file is like trying to cram your entire wardrobe into your sock drawer. It’s possible, but it makes finding anything later that much more difficult.

 Store subprograms in separate files, as shown in Figure 2-4. Storing subprograms in separate files offers three huge advantages:The fewer subprograms crammed into a single file, the easier it is to find and modify any of them.If you store subprograms in a separate file, you can copy that file (and any subprograms stored in that file) and then plug it into another program. In that way, you can create a library of useful subprograms and reuse them later.By reusing subprograms that you’ve tested already to make sure they work properly, you can write more complicated programs in less time, simply because you’re reusing subprograms and not writing everything from scratch.


FIGURE 2-4: You can store subprograms in one big file or in separate files.

Beginning Programming All-in-One For Dummies

Подняться наверх