Microsoft Office Tutorials and References
In Depth Information
2.2.2 Readability
Let me emphasize that commenting code is almost as much of an art as writing the code itself. I
have often seen comments similar to the following:
' Set x equal to 5
x = 5
This comment is pretty useless, since the actual code is self-explanatory. It simply wastes time and
space. (In a teaching tool, such as this topic, you may find some comments that would otherwise
be left out of a professionally written program.)
A good test of the quality of your comments is to read just the comments (not the code) to see if
you get a good sense not only of what the program is designed to do, but also of the steps that are
used to accomplish the program's goal. For example, here are the comments from a short BASIC
program that appears in Appendix F :
' BASIC program to compute the average
' of a set of at most 100 numbers
' Ask for the number of numbers
' If Num is between 1 and 100 then proceed
' Loop to collect the numbers to average
' Ask for next number
' Add the number to the running sum
' Compute the average
' Display the average
2.2.2 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
Search JabSto ::




Custom Search