Efficient Basic Coding for the Zx Spectrum (2020)
Key topics
A lively discussion erupted around an article claiming to reveal "efficient" BASIC coding techniques for the ZX Spectrum, with some commenters scratching their heads over the article's unclear or obvious "tricks." As it turns out, many of the "tips" were already well-known to retro computing enthusiasts, who chimed in with their own nostalgic experiences and humorous anecdotes. The conversation quickly derailed into a series of tongue-in-cheek suggestions for fixing a mysteriously failing database connection, with commenters jokingly recommending everything from adjusting a tape player's head to propping up the server with a book. Amidst the banter, a relevant video about programming the ZX81 efficiently was shared, keeping the retro tech spirit alive.
Snapshot generated from the HN discussion
Discussion Activity
Light discussionFirst comment
4h
Peak period
4
4-6h
Avg / period
1.9
Based on 15 loaded comments
Key moments
- 01Story posted
Dec 14, 2025 at 7:04 AM EST
20 days ago
Step 01 - 02First comment
Dec 14, 2025 at 10:34 AM EST
4h after posting
Step 02 - 03Peak activity
4 comments in 4-6h
Hottest window of the conversation
Step 03 - 04Latest activity
Dec 15, 2025 at 8:26 AM EST
19 days ago
Step 04
Generating AI Summary...
Analyzing up to 500 comments to identify key contributors and discussion patterns
Want the full context?
Jump to the original sources
Read the primary article or dive into the live Hacker News thread when you're ready.
'Tricks' like 'not including too many comments' were already well-known from day one of the ZX line (which started around 1980) because, well, you had 1K, 16K or 48K or RAM to work with, so every character counted!
Also, you were painfully aware of the performance of inner/outer loops, because, absolutely, a sub-3 MHz clock speed doesn't leave many other options. Other than to migrate to assembly coding, which was where most serious Sinclair coding took place.
The article is right about one thing, though: the Sinclair BASIC interpreter was a work of minimalist art, as was the hardware. "Sure, let's multiplex the audio-in line with the video sync signal, so we can save a pin on the ULA" is not something that gets a lot of consideration these days...
They key problem is that GO SUB and GO TO are not instantaneous like a CALL or JP instruction would be in Z80. The BASIC interpreter does a linear scan through the entire source code, until it reaches the specified line number. Every time you want to jump or call.
That's why this article is called "Efficient Basic Coding"... all the "tricks" are about moving the most frequently called code to the lowest line numbers, so the interpreter spends as little time scanning the source code destination line numbers as possible.
The second article in the series is on a theme, where variables aren't indexed either, the interpreter scans through each variable in turn until it finds the one with the name referenced in the source code... again you want to define them in order of most frequently accessed...
But that GO TO/GO SUB target position in the code list matters, because does a linear search (so the no use comments, isn't only about wasting RAM), was new for me. And I toyed with a ZX Spectrum as child.
1: https://spectrumcomputing.co.uk/entry/21001/ZX-Spectrum/1_Li...
Error establishing a database connection
https://www.youtube.com/watch?v=TTvrIPzGAFQ
gopher://gopherddit.com
For instance, the symbol table is built as each variable is encountered, and it is not ordered. Thus, it was common for the first line of a BASIC program to be something like:
because Y, X, B, and Z were the most frequently accessed variables, so getting them at the start of the symbol table sped up future look-ups.Another thing: I'm pretty sure MS BASIC was smart enough to check if the GOTO or GOSUB line number was greater than the current line number, and if so, it would seek the target line from the current line, instead of the start of the program.
WANG 2200 BASIC-2 had an optimization for DEFFN statements. There was a small, fixed-size table, something like 16 entries, to hold a pointer to the first n DEFFN statements. Most programs used less than this limit. When an "FNX(...)" or whatever was encountered, it would search that DEFFN table, which then had a direct pointer to the line where the DEFFN was located. If there were more than 16 DEFFN statements, it would then have to do a linear search through the program text to find it.
Guess the ZX BASIC did not do that, or it would have improved the measurements seen in the article?
BLUE Book of GW-BASIC is full of details and tricks like that and some of it probably is useful for other BASICs as well.
https://github.com/robhagemans/hoard-of-gwbasic