32 Planning Ahead with Structured Programming Original Program Original Program Original Program Modification #5 Figure 2-1: Constantly modifying a program eventually creates an unorganized mess. Modification #1 Modification #2 Modification #1 Modification #3 Modification #2 Modification #4 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 any more. So that’s when computer scientists created the first programming techniques to help programmers write programs that’d be easy to understand and modify later. Planning Ahead with Structured Programming The problem with programs created without any planning is that it inevitably leads to a mess. So the first step involves keeping a program organized right from the start.
Planning Ahead with Structured Programming 33 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 50 20 PRINT “This line prints second.” 30 END 40 GOTO 20 50 PRINT “This 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 prints first. on-screen. 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. 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. Book I Chapter 2 Different Methods for Writing Programs 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 ✦ Branches ✦ Loops Sequences Sequences are simply groups of commands that the computer follows one after another. Most simple programs just consist of a list of commands that the computer follows from start to finish, as shown in Figure 2-2.