[Coco] GOTO and code maintainability

Wayne Campbell asa.rand at yahoo.com
Tue Sep 8 14:21:56 EDT 2009


An example of what I consider to be good use of GOTO:

When I first wrote DCom, I used complex condition tests. I found out that Basic09's editor doesn't like long statements. Why? Because Basic09 wraps long lines, and the search and change functions can't find text if it's wrapped, like (notice the wrap in the <=$63):

IF vTokenCode>=$40 AND vTokenCode<=$43 OR vTokenCode>=$60 AND vTokenCode<=
$63 OR vTokenCode>=$80 AND vTokenCode<=$83 THEN

If I try:
s/<=$63/

 I will get, " can't find "<=$63" ". The same happens with c/<=$63/. It will find <= or $63, but not both. Also, If you look at the above code, this instruction is having to test 11 conditions before arriving at whether the statement is true or false.

To get around this, and which has also proven to be faster in execution, I wrote the code as follows in unpack (THEN 20 is the same as THEN GOTO 20, except it uses a different token in the I-Code):

IF vTokenCode>=$40 AND vTokenCode<=$43 THEN 20
IF vTokenCode>=$60 AND vTokenCode<=$63 THEN 20
IF vTokenCode>=$80 AND vTokenCode<=$83 THEN
20 variables.varDST.DMAddr:=vTokenPointer

In addition to making the code faster, it also removes the need to duplicate code. The code within the last IF THEN/ENDIF block would have to be repeated, or put into a subroutine (a GOSUB and a RETURN) for each separate condition to use it. This way, I'm only branching a short distance, and using the same code.

It also allows for special conditions that apply to just one of the tests. For example, let's say I find I need to set one variable if vTokenCode is between $40 and $43, but nowhere else. I can modify the code:

IF vTokenCode>=$40 AND vTokenCode<=$43 THEN
special code
GOTO 20
ENDIF
IF vTokenCode>=$60 AND vTokenCode<=$63 THEN 20
IF vTokenCode>=$80 AND vTokenCode<=$83 THEN
20 variables.varDST.DMAddr:=vTokenPointer

For me, this has proven to be a much more efficient, and faster, method of coding. It is also readable, followable, and easier to debug.

Wayne



      



More information about the Coco mailing list