Regularity

From Maze

(Difference between revisions)
Jump to: navigation, search
(removed redundant outline)
(→"Go To Statement Considered Harmful")
Line 40:Line 40:
==Iteration – code and structures==
==Iteration – code and structures==
==="Go To Statement Considered Harmful"===
==="Go To Statement Considered Harmful"===
 +
 +
(tk introduce the letter)
 +
 +
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)
 +
10 PRINT CHR$(CHARCODE%);
 +
20 GOTO 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.
 +
 +
Already there is one potential problem: what if the programmer had forgotten to change GOTO's target from line 10 to line 5? The program would still function, but since the loop only returns to line 10 CHARCODE% never changes and the program only prints the same character over and over again with no randomization. Nowhere in this code does it say that the computer must select a random character code every time it prints a character, it is only implied by the fact that line 10 comes after line 5. GOTO allows the programmer to reference any line of code, however; it is just as valid to say GOTO 10 as it is GOTO 5, regardless of the intention that line 10 always executes right after line 5. GOTO's ability to target arbitrary line numbers means that sections of code that are intended to be monolithic can be entered at any point instead of just the beginning of the block.
 +
 +
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
 +
5 CHARCODE%=205.5+RND(1)
 +
10 POKE 1024+I,CHARCODE%
 +
15 NEXT I
 +
20 GOTO 1
 +
 +
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
 +
 +
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
 +
 +
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.
 +
===Structured programming===
===Structured programming===
===OOP===
===OOP===

Revision as of 20:58, 21 February 2011

View this revision of Regularity.

Personal tools