Readability

Readability is also a subjective matter. What is readable to one person may not be readable to another. In fact, it is probably fair to say that what is readable to the author of a program is likely to be less readable to everyone else, at least to some degree. It is wise to keep this in mind when you start programming (that is, assuming you want others to be able to read your programs).

One of the greatest offenders to code readability is the infamous GOTO statement, of which many languages (including VBA) have some variety or other. It is not my intention to dwell upon the GOTO statement, but it will help illustrate the issue of good programming style.

The GOTO statement is very simpleā€”it just redirects program execution to another location. For instance, the following BASIC code asks the user for a positive number. If the user enters a nonpositive number, the GOTO portion of the code redirects execution to the first line of the program (the label TryAgain). This causes the entire program to be executed again. In short, the program will repeat until the user enters a positive number:

TryAgain:

INPUT "Enter a positive number: ", x IF x <= 0 THEN GOTO TryAgain

While the previous example may not be good programming style, it is at least readable. However, the following code is much more difficult to read:

TryAgain:

INPUT "Enter a number between 1 and 100: ", x IF x > 100 THEN GOTO TooLarge IF x <= 0 THEN GOTO TooSmall PRINT "Your number is: ", x

GOTO Done TooLarge:

PRINT "Your number is too large"

GOTO TryAgain

TooSmall:

PRINT "Your number is too small"

GOTO TryAgain

Done:

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:

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:

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:

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.

0 0

Post a comment