Next: Example: designing an automatic
Up: Programming Help
Previous: File Operations
Adhering to good programming principles
pays in clarity of programs, and facilitates debugging (finding errors).
- Get into the habit of structural programming.
- Be aware of the distinction between main program, and subprograms.
The main program should play different role - it should direct the course of
action, not do the actions. Avoid formulas, computations, algorithms in
the main part of the
program. Have the ``subordinate" subprograms do the tasks
- Split longer subprograms into smaller blocks (also subprograms, or modules),
preferably the ones you can re-use. As a rule of thumb - subprogram listing
should fit within one typed page (60 lines).
- Generalize! If you want to average 3 numbers, you can use
FUNCTION Average(x1,x2,x3). But you should write tt FUNCTION Average (X()) that
averages as many numbers as you ask.
- Use comments! Write the purpose of each subprogram
before writing the code. Test the operation of the program with ``empty", or
test subprograms before you write the actual code for your subprograms.
- Avoid Label and GoTo instructions. Whenever possible,
use While ... End, Do ... Loop, or For ... ... End constructions.
- Test each sub-program separately, ne at a time, and use only well-tested
modules.
Adhering
to the principles below is not a guarantee that the programs will work. It is also possible
to write programs that execute correctly without any of the below. Nevertheless,
it is a good habit to follow these recommendation. The gain is in clarity of the program,
readability of its portions. Consequently, you will be able to design more complex
programs that execute as expected. You will also re-use components easier.
If you wrote a program that uses GOTO statement(s), it is a good exercise to
re-write it without a single GOTO instruction!
Send comment