I want to discuss an issue that, in many ways, straddles the divide between old, "traditional" BASIC dialects and newer, more "modern" versions: are goto, gosub, and line numbers harmful, fundamental to BASIC, or both harmful and fundamental? Do they have a place in any modern BASIC dialect? Where is Yabasic on the scale that ranges from "traditional" to "modern"? In experimenting with Yabasic 3, I have removed these three features, and the decision before me is whether or not to re-add them. I hope to acquire some valuable feedback by provocatively arguing that they are not, actually, fundamental to a BASIC dialect, and additionally that they are harmful and unnecessary. All that said, I am quite willing to hear opposing arguments, and at this point I could fairly easily be convinced to re-add them.
The goto statement jumps to a particular line number (or to a defined label, which is more common nowadays) within a program, and program execution continues from that point. There is no way to return from this jump to the location jumped from, other than by using another goto statement to jump to a label or line number before that location. The gosub statement, however, jumps to a particular line number or label with a view to return later—by using, for example, the return statement.
Line numbers date back to the original BASIC, where every single line was represented a numbered statement (hence line numbers). By combining line numbers with the goto statement, it was possible to write loops and simplify conditionals. For example:
10 PRINT "HELLO, WORLD!"
20 COUNT = COUNT + 1
30 IF COUNT < 10 GOTO 10
40 END
This example displays the string "HELLO, WORLD!" ten times before exiting.
It is technically possible to write any program without using the goto or gosub statements, line numbers, or labels. Using goto can occasionally be useful for breaking out of nested loops prematurely:
while condition_1
while condition_2
if time_to_go then
goto get_out
endif
wend
wend
label get_out
...
However, it is possible to use variables to accomplish precisely the same task:
get_out = false
while condition_1
while condition_2
if time_to_go then
get_out = true
break
endif
wend
if get_out then
break
endif
wend
...
The goto statement, in particular, leads to what computer programmers term spaghetti code, which essentially means that a program, while executing, does not follow a logical consecutive order but instead jumps wildly from one location to another. This is not only considered poor design, it makes a program less understandable to users. However, goto is often very tempting to a beginning programmer: I recall that when I first learnt Yabasic (and it was the first programming language I really learnt), I used goto very often to control program flow, because it was easy and seemed simple—until I had to read and re-write my programs.
Similarly to goto, gosub tends to have adverse effects on program readability and maintainability. There are at least two other pitfalls: there is no guarantee that the location jumped from will be returned to eventually, and there is no way of passing information along with the gosub statement, or of returning information from a routine, other than using global variables:
name$ = "Thomas Larsen" // bad practice!
location$ = "Australia" // bad practice!
gosub display_person // can't pass any arguments
end // must have this to avoid continuing into label
label display_person
print name$ + " lives in " + location$ + "."
return // can't return any value
Conversely, subroutines, defined using the sub statement, and subroutine calls suffer from neither of these flaws:
display_person ("Thomas Larsen", "Australia")
sub display_person (name$, location$)
print name$ + " lives in " + location$ + "."
return true
end subLine numbers inherently have many problems. For example, if a new statement is added to a program then all existing lines in the program must be re-numbered and all references to them updated. Some old BASIC dialects which included an editor performed this automatically or upon receiving a particular command (for example, GW-BASIC), but newer BASIC versions which execute programs stored in files which are created by other editors should not modify those files (for instance, Yabasic).
None of these features, particularly if they are used regularly in a program, are conducive to major re-writes or fundamental changes being made. One has to ask whether such features are actually in keeping with the philosophy of BASIC, which is arguably to make programming relatively easy, understandable, and enjoyable for beginning programmers (and veterans, too). If goto, gosub, and line numbers cause confusion, lead to flawed program design, and encourage undesirable programming habits, are they really fundamental to BASIC dialects? No, I would argue.
But, after all, this is just my two cents.
Quote: "Do they have a place in any modern BASIC dialect?"
ReplyDeleteYes, i think they do have their place in modern dialects. In personal level, we could talk (read argue) about this issue till the day we die, but it is not up to our opinions.
GOTO, GOSUB and line numbers are part of BASIC programming, like a nose is a part of human head. Maybe you dont like your nose, and maybe in future you dont have real need for it, but it does not mean we should prevent everyone having and using it?
I know that using GOTO can cause some idiotic problems, specially for beginners, but solving those problems are part of learning to program. I dont see that someone has right to tell, what is right or wrong way to program.
I beg to differ.
ReplyDeleteThe ugly three -- line numbers, goto, gosub -- were introduced to allow the implementation of *any* kind of program flow control in a small interpreter, not because anybody ever thought they are a splendid idea.
For the last 20 years BASICs have worked without them, but we now have procedures with parameters and break/continue statements, which are superior in all regards to line numbers etc.
Programming languages should be allowed to evolve and to adapt to new requirements/circumstances. I don't see any advantages in deliberately keeping a programming language dumb and difficult to use.
We might as well throw while/loop and long variable names overboard -- they were not available on my Commodore BASIC 20+ years ago either...
And maybe 38911 BASIC BYTES FREE are still enough? ;-))
ReplyDeleteAh, the good old days...
ReplyDeleteActually, there is one point where line numbers would come in handy with Yabasic: Together with the compile()-command, one could insert pieces of code in arbitrary places at runtime!
For example --
10 input username$
20 print "Welcome, ";
30 print username$
...
compile("20 print \"Smeg off, \";")
-- would offer wonderful possibilities!
Ah, "Much of madness, more of sin, and horror the soul of the plot!" ;-)
(No, I don't *really* think that's useful or feasible...)
syzygy
@syzygy: Taking long variable names in this conversation is not fair, when considering line numbers or old-school statements.
ReplyDeleteWhen givin possibility to use long variablenames, it is developement. Taking something off, is developement too, but very different kind-off.
Just like claiming that since there is GUI for something, there is no need for CLI possibility anymore.
Just my own take on all this:
ReplyDeleteApparently this is going to be a conversation along the lines of making YaBasic a "serious" programming language. Personally I am against that.
There are plenty of such languages and I really dont care that there is another. Many of the things being considered for removal are part of what I think of as B.A.S.I.C. and its original purpose. I chose YaBasic as a BASIC. Not as an improved basic, gui basic, or experts basic. Add if you wish but deletions might have me moving on. No offense to others, thats just me.