Friday, July 7, 2023

💡 How to (quickly) temporarily comment out large blocks of code

We can certainly comment out code with either the REM statement or a single-quote.  That gets tedious when we have many lines of code in a block we wish to comment out.

Instead, we can make use of a "pre-processor" directive (or "macro-programming") to exclude, at run-time and on export, any and all code between

  • a <!-- opening tag
  • and a --> closing tag

NOTE: BASIC comments with REM statements and single-quotes remain part of the program.  They are not excluded at either runtime (their, they are ignored) or on export.

Here's a short program with some code commented out, and the results of running that program:


Just to make the leading and trailing tags jump out more, I like to make use of some jugular-grabbing emojis.  You might find it useful to also add a little note to remind you why the code is commented out, and remind you to uncomment that code later.


Do keep in mind, though: When there are errors in a program, BAM references the line number (as in the line at which the code is located vis-à-vis the first line of the program AT RUNTIME (or upon export), as in after the code has been altered by the preprocessor.

For example, here's an error in the 6th line of the program (using the editor's line numbers), versus the line with the error at runtime:


How to find in the program what the interpreter is flagging as line 3?  Figure out what code is causing the problem by Viewing the preprocess code (Project Menu and "View Preprocess Code") for whichever version of the program you ran.


Notice that the lines of code that got commented out were replaced by a blank line.  We can see the error is a mispelled print statement, and we can use that to scan/search our program and fix the problem.


Try it out for yourself !


Another use case: to add comments to your program that will be excluded when you export the program for sharing !


No comments:

Post a Comment

🖥 Fan Dance

This is a port and mod of a SpecBAS program by ZXDunny. Run the program View the source code And, a "less is more?" version: Run t...