Topics

GOTO statement considered harmful

Richard Russell
 

I have been reminded that next year (2018) is the 50th anniversary of Edsger Dijkstra's famous (or infamous) letter "Go To Statement Considered Harmful".  I would like to suggest that every BBC BASIC programmer makes a resolution to stop using GOTO, for good, before then!  I haven't used GOTO in a BBC BASIC program - other than for test purposes - for something like 35 years and I'm sure my programs have benefitted (both in structure and speed) as a result.  You know it makes sense!

Richard.

Norman Vingoe
 

The subset of BASIC in the Sinclair ZX81 (and ZX80) particularly with the limit of one statement per line (except after IF..THEN) and no structure commands apart from GOSUB..RETURN meant that programmers of these devices would use spaghetti code just to make their programs possible. Even the ZX Spectrum didn't have procedures and its user-defined functions also weren't as comprehensively implemented as was available on the Cambridge Z88 - that notebook computer was a useful way to test code segments for the BBC range.

James Gibbons
 

From Jim Gibbons
If I want to put some explanatory text between procedures, I don't need a REM as it is never accessed. If I want to put say 10 lines of 60 characters at the beginning of a program, I can do a lot of REM lines or jump over it with a GOTO. What am I missing? Additionally, in some other BASICs there is a punctuation mark which stands for REM. I haven't found one in bb4w 

----Original Message----
From: news@...
Date: 26/10/2017 00:01
To: <bb4w@groups.io>
Subj: [bb4w] GOTO statement considered harmful

I have been reminded that next year (2018) is the 50th anniversary of Edsger Dijkstra's famous (or infamous) letter "Go To Statement Considered Harmful".  I would like to suggest that every BBC BASIC programmer makes a resolution to stop using GOTO, for good, before then!  I haven't used GOTO in a BBC BASIC program - other than for test purposes - for something like 35 years and I'm sure my programs have benefitted (both in structure and speed) as a result.  You know it makes sense!

Richard.

Paul F Tolson
 

All I know since getting my first BBC Model B micro in 1982 is that "structured programming" was drilled into me and GOTO was best avoided :)

Paul

On 26/10/2017 10:28, James Gibbons via Groups.Io wrote:
From Jim Gibbons
If I want to put some explanatory text between procedures, I don't need a REM as it is never accessed. If I want to put say 10 lines of 60 characters at the beginning of a program, I can do a lot of REM lines or jump over it with a GOTO. What am I missing? Additionally, in some other BASICs there is a punctuation mark which stands for REM. I haven't found one in bb4w

----Original Message----
From: news@...
Date: 26/10/2017 00:01
To: <bb4w@groups.io>
Subj: [bb4w] GOTO statement considered harmful

I have been reminded that next year (2018) is the 50th anniversary of Edsger Dijkstra's famous (or infamous) letter "Go To Statement Considered Harmful".  I would like to suggest that every BBC BASIC programmer makes a resolution to stop using GOTO, for good, before then!  I haven't used GOTO in a BBC BASIC program - other than for test purposes - for something like 35 years and I'm sure my programs have benefitted (both in structure and speed) as a result.  You know it makes sense!

Richard.


Colin Rose
 

Because the use of REM makes it clear to others that the lines are remarks and not code. Your method is sloppy programming in my mind and hardly saves any effort over doing it “properly”.
Colin
 

From: James Gibbons via Groups.Io
Sent: Thursday, October 26, 2017 10:28 AM
To: bb4w@groups.io
Subject: Re: [bb4w] GOTO statement considered harmful
 
From Jim Gibbons
If I want to put some explanatory text between procedures, I don't need a REM as it is never accessed. If I want to put say 10 lines of 60 characters at the beginning of a program, I can do a lot of REM lines or jump over it with a GOTO. What am I missing? Additionally, in some other BASICs there is a punctuation mark which stands for REM. I haven't found one in bb4w

----Original Message----
From: news@...
Date: 26/10/2017 00:01
To: <bb4w@groups.io>
Subj: [bb4w] GOTO statement considered harmful

I have been reminded that next year (2018) is the 50th anniversary of Edsger Dijkstra's famous (or infamous) letter "Go To Statement Considered Harmful".  I would like to suggest that every BBC BASIC programmer makes a resolution to stop using GOTO, for good, before then!  I haven't used GOTO in a BBC BASIC program - other than for test purposes - for something like 35 years and I'm sure my programs have benefitted (both in structure and speed) as a result.  You know it makes sense!

Richard.


Richard Russell
 

On Wed, Oct 25, 2017 at 10:43 pm, Norman Vingoe wrote:
no structure commands apart from GOSUB..RETURN meant that programmers of these devices would use spaghetti code
BBC BASIC was one of the first BASIC dialects to offer structured alternatives to GOTO (it was a prime requirement by the BBC), followed shortly afterwards by Microsoft's GW-BASIC; subsequently they both acquired fully-featured Structured Programming capabilities (in BASIC V and QBASIC respectively).  Even though Sinclair's BASICs were developed in the same time-frame, evidently he didn't consider this to be a priority.

Richard.

Richard Russell
 

On Thu, Oct 26, 2017 at 02:28 am, James Gibbons wrote:
If I want to put some explanatory text between procedures, I don't need a REM as it is never accessed.
Typically this will fail if you attempt to 'compile' the program, because obviously the compiler (or in the case of BB4W the cruncher) doesn't know that those lines are not going to be executed at run-time and will attempt to process them as if they are (and of course the resulting EXE file will be larger than it need be).  Another reason why this may break is that the REM switches off tokenising, so the comment can consist of 'free text' that can contain foreign (e.g. accented) characters etc.  If you omit the REM, characters outside the ASCII subset will typically be converted to keywords.

Altogether this is a bad idea with no compensatory advantages.

> Additionally, in some other BASICs there is a punctuation mark which stands for REM. I haven't found one in bb4w 

Why would you want one?  REM is a single-byte token, like all other keywords, so doesn't take up any more space in the  program than a 'punctuation mark' would (and anyway I don't believe there are any 'spare' characters that would be suitable).

This Wiki article discusses alternative commenting styles in BB4W and BBCSDL.

Richard.

J.G.Harston
 

Colin Rose wrote:
Because the use of REM makes it clear to others that the lines are
remarks and not code. Your method is sloppy programming in my mind and
hardly saves any effort over doing it “properly”.
...and if it's between procedures it's nevered executed anyway, example:

DEFPROCfred
PRINT "This is fred"
ENDPROC

REM A procedure that prints the area of a circle

DEFPROCcircle(A)
PRINT "Area of circle ";A;" is ";A*A*PI
ENDPROC

Even when "executing" a REM it does a fast search for the end of the line then continues, which is much faster than a GOTO which scans through the whole program looking for the destination. Also, you can set that the executed program has the REMs stripped from it before running, so they aren't even there at all.

--
J.G.Harston - jgh@... - mdfs.net/jgh

Richard Russell
 

On Thu, Oct 26, 2017 at 07:37 am, J.G.Harston wrote:
and if it's between procedures it's nevered executed anyway
That's not true in the case of single line procedures.  Here the REM line is (or at least may be) 'executed':

      DEFPROCfred : PRINT "This is fred" : ENDPROC
      REM A procedure that prints the area of a circle
      DEFPROCcircle(A) : PRINT "Area of circle ";A;" is ";A*A*PI : ENDPROC

Richard.

bobfburns@...
 

Hi All
 
The only time that I have seen the GoTo statement recommended in structured programming is as an "if all else fails exit call" in recursive functions and that was many years ago.
 
In addition, VBA in MS Access does have an "On Error goto <label> statement.
 
Regards

Bob


Bob F Burns
C Eng, FIET, MSE. G3OOU, @BobFBurns
Retired Electronics and Software Consultant
http://www.g3oou.co.uk/consultancyservices.html

Richard Russell
 

On Thu, Oct 26, 2017 at 08:45 am, <bobfburns@...> wrote:
The only time that I have seen the GoTo statement recommended in structured programming is as an "if all else fails exit call" in recursive functions
Indeed that may be an acceptable usage in languages which don't have any Structured Exception Handling capabilities.  But BBC BASIC has ON ERROR LOCAL and (in BB4W and BBCSDL anyway) RESTORE LOCAL, so errors can be propagated back down the call chain to the lowest level without using GOTO.  BBC BASIC also has a functional equivalent of TRY and CATCH (although somewhat less elegant in code terms) so taken together any residual excuse for using GOTO vanishes.

Cascaded Error Handling can be found at the Wiki here and Structured Exception Handling is here.

Richard.

Norman Vingoe
 

@jim gibbons
the  whole idea of the REM statement (remark) is that it is ignored by the interpreter, you don't have to explicitly "jump over" it with a GOTO or anything, the interpreter automatically skips to the next actionable command.
In ZX Basic REMs were used as a quick way to allocate space to store machine code; REM <type 20 characters here to reserve 20 bytes>, the preceding line would interrogate the system variables to identify the position in memory of the next statement then the reserved bytes would be filled with the machine code routine which could then be invoked from anywhere else in the BASIC program. When LISTed, the REM statement looks to contain garbage, simply the characters corresponding to the machine code bytes.

Paul F Tolson
 

Yes, I rembember that :)

Paul

On 27/10/2017 15:53, Norman Vingoe via Groups.Io wrote:
@jim gibbons
the  whole idea of the REM statement (remark) is that it is ignored by the interpreter, you don't have to explicitly "jump over" it with a GOTO or anything, the interpreter automatically skips to the next actionable command.
In ZX Basic REMs were used as a quick way to allocate space to store machine code; REM <type 20 characters here to reserve 20 bytes>, the preceding line would interrogate the system variables to identify the position in memory of the next statement then the reserved bytes would be filled with the machine code routine which could then be invoked from anywhere else in the BASIC program. When LISTed, the REM statement looks to contain garbage, simply the characters corresponding to the machine code bytes.


Richard Russell
 

On Fri, Oct 27, 2017 at 07:54 am, Norman Vingoe wrote:
In ZX Basic REMs were used as a quick way to allocate space to store machine code
If I remember rightly, in BBC BASIC the REM statement is 'skipped' by means of the interpreter searching forwards for the terminating CR character (&0D), so if the machine code happens to contain a &0D byte it won't work.  But if you're lucky and there's no &0D in the code then, yes, you could use that technique (not that there's any need to in BBC BASIC, usually).

Richard.

J.G.Harston
 

Richard Russell wrote:
Additionally, in some other BASICs there is a punctuation mark
which stands for REM. I haven't found one in bb4w
Why would you want one? REM is a single-byte token, like all other keywords, so doesn't take up any more space in the program than a 'punctuation mark' would (and anyway I don't believe there are any 'spare' characters that would be suitable).
If you really want a single-character (as opposed to single-byte)
comment marker, the assembler semicolon comment has been used by
some people. The difference is that it can't be executed and
generates an error, and the contents are tokenised. That property
can be deliberately used, for instance at the start of a library
to protect against it being accidently executed by the previous
code not terminating, eg:

REM > A library
;
DEFPROCreg_WrStr(etc.)

I use it in my programming as I have a "linker" that builds a
program from multiple sources and removes subroutines that aren't
used, so if some are called via EVAL there needs to be a tokenised
reference in the program to stop the linker removing the destination, eg:

; Keep FN80, FN81, FN82, FN83
DEFPROCcall(B%):A%=EVAL("FN"+STR$~B%):ENDPROC

jgh

Cecil Bayona
 

But is it needed, doesn't BBC Basic have assembler built in, the only thing I'm not familiar with when did the assembler become available.

On 10/27/2017 11:26 AM, Richard Russell wrote:
On Fri, Oct 27, 2017 at 07:54 am, Norman Vingoe wrote:
In ZX Basic REMs were used as a quick way to allocate space to store
machine code
If I remember rightly, in BBC BASIC the REM statement is 'skipped' by means of the interpreter searching forwards for the terminating CR character (&0D), so if the machine code happens to contain a &0D byte it won't work.  But if you're lucky and there's no &0D in the code then, yes, you could use that technique (not that there's any need to in BBC BASIC, usually).
Richard.
_._,_._,_
------------------------------------------------------------------------

Richard Russell
 

On Fri, Oct 27, 2017 at 09:43 am, J.G.Harston wrote:
If you really want a single-character (as opposed to single-byte)
comment marker, the assembler semicolon comment has been used by
some people. The difference is that it can't be executed and
generates an error
So therefore it doesn't actually work as a comment marker at all!   If you don't care that it "can't be executed" and that it "generates an error" there are several characters you could choose from, but none of them meet the OP's requirement of being a 'single character REM'.  ;-)

Richard.

J.G.Harston
 

Richard Russell wrote:
If I remember rightly, in BBC BASIC the REM statement is 'skipped' by means of the interpreter searching forwards for the terminating CR character (&0D), so if the machine code happens to contain a &0D byte it won't work.
Yes, it's the tightest possible loop, almost
.cmdStar
\ do *command
.cmdDEF
.cmdREM
.skip_line
INY:LDA (lptr),Y
CMP #13:BNE skip_line
.execute_next_line

jgh

Richard Russell
 

On Fri, Oct 27, 2017 at 10:39 am, J.G.Harston wrote:
Yes, it's the tightest possible loop
That's (Acorn's) 6502 BASIC, not one of mine (and therefore off-topic for this group)!!

Richard.

Richard Russell
 

On Fri, Oct 27, 2017 at 09:50 am, Cecil Bayona wrote:
But is it needed, doesn't BBC Basic have assembler built in
There can still be reasons for wanting to incorporate machine code in a 'binary' form rather than as assembler source code.  For example if you want to make your program as small as possible you may not want the overhead of including the source code, but rather assemble it ahead of time.  Or you may be concerned about the time it takes to assemble the code at run-time.  Or you may want to use CPU instructions that aren't supported by the built-in assembler (perhaps ones only introduced in recent CPU versions).  Or you may simply want to hide the source code from 'prying eyes'.

I'm not suggesting that such situations are common, but they can occur and in those cases ways of embedding binary machine code in a BASIC program are of relevance.  For example LBLIB.BBC (the library used by 'LB Booster') contains sprite-plotting code as a 'binary blob'.  It was originally assembled by the built-in BB4W assembler, but the source code is not included in the library.  In that case the machine code is incorporated as the first 'line' of the program; and since it's a library the issue of preventing it being executed as BASIC code does not arise.

> when did the assembler become available

Virtually all versions of BBC BASIC have incorporated an assembler, right from the start (a notable exception is Brandy).  If there's no assembler, I'm not sure it's really BBC BASIC!

Richard.