Home Web Design Programming Fairlight CMI Soap Box Downloads Links Biography About... Site Map

The Holmes Page Outside-In Programming

CA-Clipper Opportunities Tips and Tricks Networking Internal Errors Source Code CA-VO



BACK TO
CA-CLIPPER
TIPS & TRICKS

There are several types of errors that a programmer can make, including errors in logic, syntax, and structure. 
The CA-Clipper compiler will catch some of these errors, particularly the syntax errors, but many errors will either pass through unnoticed or cause a strange series of error messages to appear. 
Often, the cause of the strange messages is simply an unbalanced control structure, such as endif, or a missing closing quote or bracket. These problems are notoriously hard to spot when scanning source code for errors. On top of this, the CA-Clipper compiler often indicates a line number that is nowhere near the actual problem. 
The best solution for these kinds of errors is to avoid making them! 
This can be accomplished by using a technique that I call Outside-In Programming. The key is to type in the beginning and ending of all control structures before filling in the middle. In effect, you create a template of the program structure, and then customize it. 
Here is an example using a simple for...next loop. Suppose that the program needs to print out the squares of the integers from 1 to 10. First, the beginning of the control structure is entered.
01  for nI := 
Then a few blank lines are added, and the ending of the control structure is typed in.
01  for nI :=
02
03
04  next nI
Now pause for thought, considering what to fill in. The benefit of this approach is that the code practically writes itself. It is now just a matter of fill-in-the-blanks. The loop should go from 1 to 10, and, inside the loop, the counter variable (nI) should be squared and printed out.
01  for nI := 1 to 10
02     ? nI * nI
03
04  next nI
Once the extra blank line is removed, the process is complete.
01  for nI := 1 to 10
02     ? nI * nI
03  next nI
This technique is far more valuable in a real program, where there would be many lines of code within the loop. 

Consider a larger example. Every CA-Clipper program involves the manipulation of data files, typically using the do while construct. A common mistake is to forget to skip from one record to the next. Here is the solution. 
Suppose that you need to write a program that calculates and prints out the average age of the employees in the accounting division. Start by creating the template for the entire program.
01  procedure Main
02     local
03
04
05  return
Then, considering how an average is calculated (total/count), add the calculation at the end, and some matching local variables at the beginning. Care should be taken to initialize the variables (another common mistake). Also, open and close the EMPLOYEE table. Notice how symmetrical these additions are. Add blank lines as needed.
01  procedure Main
02     local nAgeTotal:=0, nRecCount:=0
03     use EMPLOYEE new shared
04
05
06     close EMPLOYEE
07     ? "The average age is", nAgeTotal / nRecCount
08  return
Now add a loop to navigate through the table. This will be the very common do while construct. Note that the skip command is not forgotten with this approach.
01  procedure Main
02     local nAgeTotal:=0, nRecCount:=0
03     use EMPLOYEE new shared
04     do while .not. eof()
05
06
07        skip
08     enddo
09     close EMPLOYEE
10     ? "The average age is", nAgeTotal / nRecCount
11  return
Then, add the if...endif template for the test of the DIVISION field.
01  procedure Main
02     local nAgeTotal:=0, nRecCount:=0
03     use EMPLOYEE new shared
04     do while .not. eof()
05        if EMPLOYEE->DIVISION == 'ACC'
06
07
08        endif
09        skip
10     enddo
11     close EMPLOYEE
12     ? "The average age is", nAgeTotal / nRecCount
13  return
Finally, add the accumulation into the two variables.
01  procedure Main
02     local nAgeTotal:=0, nRecCount:=0
03     use EMPLOYEE new shared
04     do while .not. eof()
05        if EMPLOYEE->DIVISION == 'ACC'
06           nAgeTotal := nAgeTotal + EMPLOYEE->AGE
07           nRecCount := nRecCount + 1
08        endif
09        skip
10     enddo
11     close EMPLOYEE
12     ? "The average age is", nAgeTotal / nRecCount
13  return
This technique can be extended to many areas of programming. For example, the second-to-last line of the program was also created using the Outside-In technique. It initially looked like this:
12     ? "",
Once this was entered, the actual values were filled in to create the complete line of code. Thus, the closing quote was not forgotten, neither was the comma. 
Although an experienced programmer could have created this program from top to bottom in one go, as the code gets more complex the likelihood of ommisions increases. Even in this "simple" example, the programmer would have to remember to close out 7 code elements, among them the if statement, the do while loop, and the enclosing procedure itself.


Home Web Design Programming Fairlight CMI Soap Box Downloads Links Biography About... Site Map

Site Map Send comments about this site to Greg at gregh@ghservices.com
All pages copyright © 1996-1999 GH Services™   Created 1997/07/07   Last updated 1999/09/30
All trademarks contained herein are the property of their respective owners