When I recently was working on SSB 2.6, I was using a SuperBasic implementation of a CASE structure for the core of the program. The deeper the structure got, the harder it was to read and understand. I starting thinking of using another way of implementing a CASE structure.
For those that don’t know what a CASE structure or statement is, it is essentially the same structure as a SuperBasic SELECT ON statement, but it is not limited to numbers. Plus the general idea of a CASE structure is to have a number of possible logic statements with an action for all cases that do not fit one of the logic statements. In a generalized CASE structure not all of the comparisons must be based on the same data (string, number, etc), but can vary.
Traditionally, a way of creating the CASE structure is to have a number of nested IF..THEN..ELSE statements, with the final ELSE handling the default value. An example would be something like this: you are reading in text from a file. You want to handle the following cases: a line starts with a ##, or a line starts with a **, a line starts with a period (.). If none of these cases, then the line is passed to the output file.
Using nested IF..THEN..ELSE statements the pseudo code would look something like this:
IF line starts with ## THEN
......
ELSE
IF line starts with ** THEN
......
ELSE
IF line starts with a period THEN
.......
ELSE
pass to output file
END IF
END IF
END IF
If you start having more than just a hand full of possible cases, the structure can get long and difficult to read.
SuperBasic allows the use of NEXT in conjunction with a REPEAT statement. This means that when the NEXT is reached, the rest of the REPEAT loop is skipped and the processing goes onto the next iteration of the REPEAT loop. Using NEXT in this manner allows for the creation of a different implementation of a CASE structure. Below is an example of the two implementations listed side-by-side.
REPeat loop REPeat loop
IF .... THEN IF .... THEN
.... ....
ELSE NEXT loop
IF .... THEN END IF
.... IF .... THEN
ELSE ....
IF .... THEN NEXT loop
.... END IF
ELSE IF .... THEN
default ....
END IF NEXT loop
END IF END IF
END IF default
END REPeat loop END REPeat loop
Using the REPEAT..NEXT..END version may not seem as clean as the “classical” IF..THEN..ELSE structure, but I think it is cleaner when it come to reading and debugging. What I am looking for from readers is to ponder over any downsides from using the REPEAT..NEXT structure. I can’t think of any logical problems with using this structure over the classical one. If you know of any, please send me a note. I’ll add the productive comments in the next issue.