BASIC PROGRAMMING TECHNIQUES

16K cassette 24K disk

by Thomas M. Krischan 

There are two fundamental principles in good programming technique: function and cosmetic. The functional principle is the first to be mastered by the novice. It involves the duty portion of programming, which covers these questions:
 

Does the program work?
Is it error free?
Is it comprehensive?
 
The cosmetic principle is usually overlooked by the novice programmer, and too often by even the advanced programmer. It addresses a less visible, yet equally important portion of the program:
 
Is the code structured?
Is it readable?

Are sufficient comments embedded into the listing to document the work properly?

The failings of the programmer to address this second principle are not necessarily the fault of the programmer alone. Most programming courses and text books neglect this subject area. The intent of this article is to outline the cosmetic principle.

A good program contains the following cosmetic elements: structure, readability, and internal documentation. A program listing, like a magazine article, is subdivided into smaller logical units of composition. Articles refer to logical units as paragraphs, programsrefer to logical units as subroutines. Paragraphs are not magically created by indented openings, likewise, subroutines are not magically created by adding a RETURN statement. Your subroutine should therefore mark the logical subdivision of the function of the whole program. A subroutine must be complete in itself. Ask these questions of each subroutine:

Is the subroutine unified?
Does it hang together and read smoothly?
Is the function of the subroutine adequately developed?

Another element to consider is if the flow of the program is readable. Unlike a magazine article, logical subdivisions do not have to be read sequentially. Furthermore, some subdivisions may be read from other subdivisions. Proper programming technique requires that the vast majority of the program is read sequentially. This technique is referred to as the “top-down”approach. The alternative approach to this is entitled “spaghetti” programming. Spaghetti programming has nothing to do with the nationality of the programmer. Rather, it describes the flow of the program;a bowl of spaghetti. An excellent exercise for mastering structure and readability is to outline a program listing. Draw a bracket around each logical subdivision, connect the brackets with a line to indicate flow, and sketch an arrow on the line to designate direction. Figure 1. illustrates a program which has good structure and readability. Figure 2. illustrates a program with little structure and poor readability; spaghetti programming.

Figure 1. Good structure and readability.
 

Figure 2. Poor structure and readability.
 

The last element in our outline is internal documentation. Documentation can be in the form of remark statements or self defining code. Since the BASIC language features English-like statements, I much prefer the latter option. Figure 3. is a listing of BASIC programming statements which use variable names so appropriately that further remarks from the programmer would seem redundant.

Figure 3. Self defining BASIC statements.
POKE CURSOR, OUT
SETCOLOR, BACKGRND, GREEN, DARK
WRONG=TOTAL-RIGHT
GOSUB NOISE
GOTO ERROR

The fundamental principles of functional and cosmetic programming technique are not without their conflicts. For example:

Assigning numeric constants in GOSUB, GOTO, SETCOLOR, PEEK and POKE commands will quickly use up the 128 available variable names offered by ATARI BASIC.

Long descriptive variable names require more space than do single letter variable names. Modular design may waste time; GOSUB commands are slower than GOTO commands.

Frequently used subroutines are addressed most quickly if they reside closer to the top of the program.

Despite the conflicts, compromise can usually be reached. The good programmer can balance the benefits of functional programming with the desire to create an internally appealing program listing.

The sample program illustrates such a balanced approach. Figures 4-7 are the external documentation which accompany the program. The program functions as an education tutorial about natural events which occur in our native enviornment during the spring of the year. Because of the structure and readability of the program, questions can be easily added or changed to suit your needs. One word of advice; after running this program with its original contents, if you have not managed to get at least 50% of the questions correct, perhaps you should get up out of your chair and view the world from the other side of your window! 

Figure 4. String Variables
ASK1$ - Questions.
ASK2$
TELL1$ - Supplemental information.
TELL2$
TELL3$
GUESS1$ - Possible answer.
GUESS2$
GUESS3$

Figure 5. Numeric Variables
RIGHT - The number of questions that were answered correctly.
WRONG - The number of questions that were answered incorrectly.
TOTAL - The total number of questions asked.
PITCH - The pitch value for a sound.
TIME - The time delay of a programming loop.

Figure 6. Numeric Constants
LIGHT - Color luminance.
MEDIUM
DARK
GREEN - 3 Color hue.
RED
YELLOW
BLACK
BLUE
WINDOW - Color register
TEXT
TEXTCAPS
BACKGRND
BACKTEXT
VOICE0 - Sound generation.
PURE
NORMAL
SMALL - Graphics mode.
AVERAGE
LARGE
CURSOR - Hardware memory location.
ATTRACT
UNTOUCH
RESPONSE
KEYBOARD
OFF - Reset memory locations.
OUT
PAUSE - Subroutine location.
NOISE
BUZZ
TWINKLE
WAIT
QUIZ
ERROR
SCORE
INTRO

Figure 7. Major Program Subdivisions.
1-7 General information.
11-99 Main program; calls all supplemental subroutines.
100-199 Quiz subroutine; generates questions, possible answers and supplemental information.
200-299 Score subroutine; displays number of correct and incorrect responses, and remarks about the score.
500-560 Data for closing remarks.
1000-2490 Data for questions, possible answers, and supplemental information.
5000 Noise subroutine; used for correct responses (up tone).
5050 Buzz subroutine;used for incorrect responses (down tone).
5100 Pause subroutine; pauses display for a few seconds before continuing on.
5150-5170 Wait subroutine; waits display for a keystroke.
5200-5299 Twinkle subroutine; flashes text for additional attraction.
5300 Error subroutine; when a wrong key is pressed.
6000-6099 Intro subroutine; draws first display screen.