Thursday, July 6, 2023

💡 Making the case for GOTO use cases

It didn't take long for me to be a "never GOTO, never ever" guy once we started to see structured BASIC implementations displace unstructured BASIC implementations.

Since I started working on BASIC Anywhere Machine back in December 2020, I've really come to appreciate the use of GOTO with line labels (instead of line numbers) in a couple of scenarios.

BASIC Anywhere Machine (aka "BAM") links:

 

Always being weary, of course, of not creating spaghetti code, here are two scenarios in which I find GOTO really practical.  When do you find GOTO useful and acceptable ?

Loops involving a lot of code

For the following blocks of code:

  • WHILE ... WEND 
  • WHILE ... LOOP
  • DO ... LOOP
  • DO WHILE ... LOOP
  • DO WHILE ... WEND
  • DO LOOP ... WHILE
  • DO LOOP ... UNTIL
If the bookends of the code block are so far apart that screen scrolling becomes necessary to see all of the code in the block, or if the code block had nested loops, matching the right bookends can become challenging.

I often prefer GOTO in these circumstances (expecially an infinite DO ... LOOP) because line labels make it easy to match the bookends of a code block.

For example:

StartLoop1: ...
...
...
GOTO StartLoop1

I find this approach concise and descriptive.


Skip over a block of code

In some circumstances, it makes more sense to me to have an IF that skips a block of code, rather than have an IF condition be satisfied to execute that block of code (and exclude the block otherwise).

Step1:
IF condition GOTO Step2
...
...
Step2:




No comments:

Post a Comment

🖥 Pongy game

This program is a port (and slight mod) of the QB64pe program by SierraKen.  (SierraKen's program shared with the QB64pe community in th...