Regularity

From Maze

(Difference between revisions)
Jump to: navigation, search
(→"Go To Statement Considered Harmful")
(pre tags)
Line 45:Line 45:
In the case of 10 Print the GOTO statement avoids most of Dijkstra's complaints. Putting a GOTO statement at the end of a one-line program that simply references itself does not leave much room for unexpected entrances and exits. However, expanding the program to take more than one line demonstrates the potential problems that GOTO introduces:
In the case of 10 Print the GOTO statement avoids most of Dijkstra's complaints. Putting a GOTO statement at the end of a one-line program that simply references itself does not leave much room for unexpected entrances and exits. However, expanding the program to take more than one line demonstrates the potential problems that GOTO introduces:
−
5 CHARCODE%=205.5+RND(1)
+
<pre>5 CHARCODE%=205.5+RND(1)
10 PRINT CHR$(CHARCODE%);
10 PRINT CHR$(CHARCODE%);
−
20 GOTO 5
+
20 GOTO 5</pre>
This code is functionally identical to the one-line version of 10 Print, but is a bit more representative of how developers create more complex programs. Line 5 creates a variable called CHARCODE% that stores a character code determined by the same formula used in the original 10 Print, but the character code is not used until line 10 when it is printed to the screen. Line 20 has the GOTO statement that makes the program iterate, but it now points to the new beginning of the program at line 5.
This code is functionally identical to the one-line version of 10 Print, but is a bit more representative of how developers create more complex programs. Line 5 creates a variable called CHARCODE% that stores a character code determined by the same formula used in the original 10 Print, but the character code is not used until line 10 when it is printed to the screen. Line 20 has the GOTO statement that makes the program iterate, but it now points to the new beginning of the program at line 5.
Line 55:Line 55:
GOTO's potential problems only get worse when it is combined with other flow control statements. Consider this variant of 10 Print:
GOTO's potential problems only get worse when it is combined with other flow control statements. Consider this variant of 10 Print:
−
1 FOR I FROM 0 TO 999
+
<pre>1 FOR I FROM 0 TO 999
5 CHARCODE%=205.5+RND(1)
5 CHARCODE%=205.5+RND(1)
10 POKE 1024+I,CHARCODE%
10 POKE 1024+I,CHARCODE%
15 NEXT I
15 NEXT I
−
20 GOTO 1
+
20 GOTO 1</pre>
This version uses a FOR loop to fill the entire screen with the two 10 Print characters, beginning at the top-left corner. Immediately after finishing the FOR loop the program hits line 20 and GOTO sends execution back to line 1, restarting the loop from the beginning. If the forgetful programmer again fails to update line 20 though, leaving it as:
This version uses a FOR loop to fill the entire screen with the two 10 Print characters, beginning at the top-left corner. Immediately after finishing the FOR loop the program hits line 20 and GOTO sends execution back to line 1, restarting the loop from the beginning. If the forgetful programmer again fails to update line 20 though, leaving it as:
−
20 GOTO 5
+
<pre>20 GOTO 5</pre>
The program will now not only fail to work as intended, it will fail to work at all and throw an error:
The program will now not only fail to work as intended, it will fail to work at all and throw an error:
−
? NEXT WITHOUT FOR ERROR IN 15
+
<pre>? NEXT WITHOUT FOR ERROR IN 15</pre>
Unlike the previous version where lines 5 and 10 were only implied to be a block, lines 1-15 are now explicitly a block of code defined as a FOR loop. NEXT relies on FOR, but since the block was re-entered on line 5 FOR never executes and NEXT is no longer valid.
Unlike the previous version where lines 5 and 10 were only implied to be a block, lines 1-15 are now explicitly a block of code defined as a FOR loop. NEXT relies on FOR, but since the block was re-entered on line 5 FOR never executes and NEXT is no longer valid.

Revision as of 21:00, 21 February 2011

View this revision of Regularity.

Personal tools