Beginning Programming All-in-One For Dummies. Wallace Wang
Чтение книги онлайн.
Читать онлайн книгу Beginning Programming All-in-One For Dummies - Wallace Wang страница 25
FIGURE 2-1: Constantly modifying a program eventually creates an unorganized mess.
Modifying a program several times by yourself may not be so bad because you probably remember what you changed and why. But what happens if seven other programmers modify the same program seven different times and then none of them is around to help you understand what changes they made? Yep, you’d wind up with a bigger mess than before.
With constant modifications, a small, simple program can grow into a convoluted monstrosity that may work, but nobody quite understands how or why. Because the program consists of so many changes scattered throughout the code, trying to figure out how the program even works can get harder with each new modification.
With a simple program, the computer follows each command from start to finish, so it’s easy to see how the program works. After a program gets modified multiple times, trying to follow the order of commands the computer follows can be like untangling spaghetti (hence, the term spaghetti programming).
As programs kept getting bigger and more complicated, computer scientists realized that just letting programmers rush out to write or modify a program wasn’t going to work anymore. That’s when computer scientists created the first programming techniques to help programmers write programs that would be easy to understand and modify later.
SPAGHETTI PROGRAMMING WITH THE GOTO COMMAND
Although you can write spaghetti programs in any language, the BASIC programming language is most closely associated with spaghetti programming. Early versions of BASIC used a GOTO
command, which essentially told the computer to “go to” another part of the program.
The problem with the GOTO
command was that it could tell the computer to “go to” any part of the program. If you had a large program that consisted of several hundred (or several thousand) lines of code, the GOTO
command could tell the computer to jump from one part of the program to another in any order, as the following BASIC program shows:
10 GOTO 5020 PRINT "This line prints second"30 END40 GOTO 2050 PRINT "This line prints first"60 GOTO 40
Line 10 (the first line) tells the computer to “go to” line 50.
Line 50 tells the computer to print This line prints first
onscreen. After the computer follows this command, it automatically runs the next command below it, which is line 60.
Line 60 tells the computer to “go to” line 40.
Line 40 tells the computer to “go to” line 20.
Line 20 tells the computer to print This line prints second
onscreen. After the computer follows this command, it automatically follows the command on the next line, which is line 30.
Line 30 tells the computer this is the end of the program.
Even though this program consists of six lines, you can already see how the GOTO
command makes the computer jump around, so it's hard to understand how this program works. Now imagine this program multiplied by over several hundred lines of code, and you can see how spaghetti programming can make reading, understanding, and modifying even the simplest program much harder.
Structured Programming
The problem with creating programs without any planning is that it inevitably leads to a mess. So, the first step involves keeping a program organized right from the start.
The three parts of structured programming
To keep programs organized, structured programming teaches programmers that any program can be divided into three distinct parts:
Sequences:Sequences are simply groups of commands that the computer follows, one after another. Most simple programs consist of a list of commands that the computer follows from start to finish, as shown in Figure 2-2.FIGURE 2-2: Sequences consist of groups of commands that the computer follows, one after another.
Branches:Branches consist of two or more groups of commands. At any given time, the computer may choose to follow one group of commands or another. Branches allow a program to make a decision based on a certain condition.For example, at the end of most video games, the program asks you, “Do you want to play again (Yes or No)?” If you choose Yes, the program lets you play the video game again. If you choose No, the program stops running, as shown in Figure 2-3.FIGURE 2-3: Branches let the computer choose which group of commands to run at any given time.A branch starts with a command that evaluates a condition (such as determining whether the user chose Yes or No). Then, based on this answer, whether it’s true or false, the branch chooses which group of commands to follow next.
Loops: Sometimes you may want the computer to run the same commands over and over again. For example, a program may ask the user for a password. If the user types an invalid password, the program displays an error message and asks the user to type the password again.If you wanted your program to ask the user for a password three times, you could write the same group of commands to ask for the password three times, but that would be wasteful. Not only would this force you to type the same commands multiple times, but if you wanted to modify these commands, you’d have to modify them in three different locations as well. Loops are basically a shortcut to writing one or more commands multiple times.A loop consists of two parts:The group of commands that the loop repeatsA command that defines how many times the loop should run
By combining sequences, branches, and loops, you can design any program and understand how the program works at each step.
Dividing a program into sequences, branches, and loops can help you isolate and organize groups of related commands into discrete “chunks” of code. That way, you can yank out a chunk of code, modify it, and plug it back in without affecting the rest of the program.
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