Microsoft Office Tutorials and References
In Depth Information
2.2.2 Readability
GOTO Done
TooLarge:
PRINT "Your number is too large"
GOTO TryAgain
TooSmall:
PRINT "Your number is too small"
GOTO TryAgain
Done:
END
Because we need to jump around in the program in order to follow the possible flows of execution,
this type of programming is sometimes referred to as spaghetti code . Imagine this style of
programming in a program that was thousands of lines long! The following version is much more
readable, although it is still not the best possible style:
TryAgain:
INPUT "Enter a number between 1 and 100: ", x
IF x > 100 THEN
PRINT "Your number is too large"
GOTO TryAgain
ELSEIF x <= 0 THEN
PRINT "Your number is too small"
GOTO TryAgain
END IF
PRINT "Your number is: ", x
END
The following code does the same job, but avoids the use of the GOTO statement altogether, and
would no doubt be considered better programming style by most programmers:
DO
INPUT "Enter a number between 1 and 100: ", x
IF x > 100 THEN
PRINT "Your number is too large"
ELSEIF x <= 0 THEN
PRINT "Your number is too small"
END IF
LOOP UNTIL x >= 1 AND x <= 100
PRINT "Your number is: ", x
END
Readability can also suffer at the hands of programmers who like to think that their code is
especially clever or elegant but, in reality, just turns out to be hard to read and error-prone. This is
especially easy to do when programming in the C language. For instance, as a very simple
example, consider the following three lines in C:
x = x + 1;
x = x + i;
i = i - 1;
The first line adds 1 to x , the second line adds i to x , and the third line subtracts 1 from i . This
code is certainly readable (if not terribly meaningful). However, it can also be written as:
x = ++x+i--;
This may be some programmer's idea of clever programming, but to me it is just obnoxious. This
is why a sagacious programmer always favors readability over cleverness or elegance.
Search JabSto ::




Custom Search