Jim’s Programming Notes

Author(s): Jim Wagner
Publication: The Plotter
Pub Details: The Plotter v12 n6 (Volume 12, Issue 6)
Date: June 1994
Pages: 6-7

C. STRUCTURE

1. Avoid (read NEVER, NEVER) multiple entry points to subroutines.

2. Avoid multiple exit points from subroutines.

3. If names are permitted for subroutines, use names which are as descriptive as possible, even if they seem a little long.

4. Sometimes subroutines make sense, even if the subcode is used only one place in the rest of the program. Breaking out a block of code as a subroutine may make the code easier to read. It often permits better documentation with comments than would fit where the code would appear in the program section which calls it. For example, if a section of a routine (perhaps a few dozen lines) sends a character to the serial port, break out those lines as a subroutine, & name it (if permitted) SENDCHAR. Then, something like “call SENDCHAR” appears in the section of code which uses this. This makes it much easier to see what is happening when you come back later without having to struggle with details. At the place where the subroutine is written out, you can place a whole block of information which tells how the subroutine works. When you come back to change or debug the program, you need not wade through the documentation unless you really want to, and you know more about what the program is intended to do.

5. Avoid multiple operations per line (as permitted in BASIC). Especially, do not cram operations together just to save lines. This prohibition is less strong if the operations are really intimately related.

6. Avoid GOTOs. Do not use them unless there is no other way out!

7. DOCUMENT! Even if the program is only for your own use, do it. Unless a line is totally obvious, say why it is there and what is happening. Documentation adds little to the execution speed of a program (none, if it is compiled). When the program is compiled rather than interpreted, it adds absolutely nothing to the size of the program which actually runs. Here is an example of a subroutine header:

1000 REM ============================================================
1001 REM This subroutine sends a character in $OUTCHAR to the serial port. It assumes that
1002 REM that the serial port has been initialized and that the COM port has been selected.
1003 REM The subroutine does not change any variables. If there is a character already in
1004 REM the serial port being sent and the new one cannot be sent immediately, the routine
1005 REM will wait until $OUTCHAR can be placed into the output.
1006 REM ============================================================
1010 (body of subroutine)

In subroutine headers, it is very useful to specify what variables affect, in any way, the flow of the program within the routine. Also list any variables which are changed inside the subroutine. So often, when trying to find problems, it is easy to overlook a statement within the code (especially on multi-statement lines). If you have it all written out in such a header, then you always know where to look to find the information you need without scanning code. It is also very important to remember to update the header every time you change that section of the program.

D. VARIABLE NAMES

1. Avoid meaningless variable names. Example, x,y,z (unless you are using them somewhere that they have a very common usage, such as 2D or 3D rectangular coordinate system).

2. Choose names which represent how the variable is actually used. For example, if a variable is going to hold a count value, name it “COUNT”, not “M”!

3. Reserve one variable for a single use. Avoid multiple uses. Example, do not use COUNT for a bit-counter in one place and a character counter in another. The chance for error is too high!

E. CONSTANTS

Constants are values which cannot change during the execution of a program. They do not take any memory space. That is, they do not take any more than if you had used an explicit number. For example, in the statement “IF X=1 THEN…”, the “1” is a constant and it does not change during the program. With normal BASIC, the statement “IF X=Y THEN…” uses two variables, “X” and “Y”; they both take memory space and either is permitted to change while the program runs. “Named constants” permits you to use names for constants. When the program compiles or begins to run, the compiler or interpreter simply substitutes the number value everywhere the name appears as one of its very first actions. Once this is done, the program runs as if there are numbers instead of the names.

If your language implementation permits named constants (GW Basic does not, Quick Basic does), use them. An example is decision making. Suppose that you have a variable (call it DIRECTN) with one value which represents up and another value, down (maybe for sprite motion). If you say that up is represented by 0 and down by 1, you might have statements like IF DIRECTN=0 THEN …. . But if you are able to name constants, you could say UP=0 and DOWN=1. Then the decision statement becomes IF DIRECTN=UP THEN… . It should be quite obvious that this kind of statement is much easier to read and interpret without question when you have to come back later.

F. USER INTERFACE

Look carefully at other programs. Observe those features which make programs difficult to use and those which make it genuinely easy. Pop-up menus (in the Windows/Macintosh style) are becoming more widely recognized. The more you can make your program like another in the interface, the easier it will be to use because the user will not have to relearn. In spite of what you might think, popup menus can be done in DOS, even in purely text (that is, non-graphic) screens.

Also, when designing an interface, go though it carefully for what seems “intuitive”. At each point in the program, try to imagine “what action seems most natural here”. If there is even a hint of an answer, try to make it work that way.

Plan for on-line help from the very beginning of writing a program. The more logical you can make a program feel and the better the on-line help you provide, the less documentation needed. Your goal should be no support documentation required! This is likely impossible (since something is usually needed to tell how to install the program). But it is a worthwhile goal.

When keys are used to control on-screen motion, as in games, choose the keys carefully. There are some conventions which have grown up for keys representing up/down/left/right and a few other standard game functions. Try to use the same pattern used in other programs. If there is no standard pattern, choose keys which seem to fit naturally. Avoid long stretches, caps shifts, etc. If you have to use control or alt keys, most people prefer the left hand for the control or alt so avoid alphabetic keys in the same half of the keyboard. For example, ALT-P is fine but alt-Z is poor. This is particularly true for the many keyboards which have only one control and one alt key. When you choose keys, also take into account the fact that not all keyboards have the same set of keys or keys in the same pattern. For example, F11 and F12 are missing from many keyboards. An even better choice is to make key assignments user selectable; its not very hard and it makes the key usage, at least, the way the user likes.