|
-
May 29th, 2000, 11:16 AM
#1
Thread Starter
PowerPoster
I have seen in many programming books that it is generally bad practice to use GOTOs and GOSUBs. The GOTO issue I can appreciate; no self-respecting programmer wants to write spaghetti code. However, I don't see anything wrong with occasionally using GOSUBs to repeat a section of code within a Sub or Function. Does anyone have a convincing argument as to why GOSUB should be avoided?
-
May 29th, 2000, 03:50 PM
#2
Frenzied Member
I've never used GoSub in VB and I had to look in the help file to see what it does!
the help file says:
Creating separate procedures that you can call may provide a more structured alternative to using GoSub...Return.
I first started programming with Modula2 which doesn't even have GOTO or GOSUB in the language so it forces you into writing well structured programs.
So, to anwer you question; no I can't offer a convincing argument against GoSubs. Has Gosub been left in VB to make it backward-compatable with older versions of Basic?
-
May 29th, 2000, 05:21 PM
#3
Frenzied Member
There's a lot of stuff left in VB to make it backwards compatible, Gosub's probably usefull in very rare occasions, If you have a procedure with lots of private variables and a subroutine that you only called from that procedure which changed a lot of variables then it's probably excusable to use a gosub for speed purposes rather than having to pass all the variables around. but there's probably a better way round it. (a largs UDT passed by ref for example.
-
May 29th, 2000, 05:34 PM
#4
transcendental analytic
Gosub's are faster than Subs and functions so when at procedurelevel called repeated code is used and no loops matches, Gosub can be useful. I'm not saying im using it frequently, as you need very frequently used code to notice any better performance.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 29th, 2000, 08:20 PM
#5
Thread Starter
PowerPoster
Thanks for the input, guys. I agree with Sam and Kedaman, and I do only use it in the limited situations described. Although GOSUB is probably included in VB for backwards compatibility, I still find it useful in these situations. Back in the days of QBasic, using GOSUBs was the primary way to write structured code. Not to show my age or anything, but GOSUB is similar to using PERFORM in COBOL (cringe), where PERFORM is the way to structure those beastly COBOL programs. Anyway, you guys confirmed my line of thinking. Thanks again.
-
May 29th, 2000, 11:03 PM
#6
Hyperactive Member
However all that scrap against GOTO, it is one of the fastest functions in VB, and in many other languages too, as it is implemented directly in machine code.
It is also the best way to unstructure the code, thou.
There is a situation in which goto is the best choice: trying to get out of a deeply nested for-next or do-loop routine when a condition comes true. Instead of multiple exit for (or exit do)- one single GOTO will take you out fast and neatly.
-
May 30th, 2000, 12:25 AM
#7
transcendental analytic
That's correct, i've used goto several times in such cases and it's definitely the fastest way to jump somewhere, so don't do any crappy do:for:if:while: and then have it all exit for, do until while or exit in other ways than goto, which is the clean way to do it. Also how do you exit an if?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 30th, 2000, 04:39 AM
#8
Whodo you exit an if? you mean IF as in the If...Then... statement? Just use End If
-
May 30th, 2000, 05:19 AM
#9
Juan is right
Tend not to use gosub and goto in vb, except for On Error conditions....apparent improved in vb7. However we have a couple of bank clients...and in there software GOTOs are used for speed. Yes bad coding technique in theory, in reality needed to cope with the amount of through put. Generally they pay big bucks if we can speed up an app...therefore GOTOs....but we have strict coding standards to keep control of the things.
Once had to support a COBOL app which was all GOTOs not one PERFORM statement...man could take days to fix even simple bugs.
-
May 30th, 2000, 09:53 AM
#10
Hyperactive Member
You mean that Completely Obsolete Business Oriented Language?
Do not abuse GOTO's -keep jumps short too.
-
May 30th, 2000, 10:31 AM
#11
Thread Starter
PowerPoster
Good one, Juan. Thanks for your earlier advice. Thanks also to Jethro and Megatron for your input. Now if only my posts regarding the TreeView control generated as many responses ...
"It's cold gin time again ..."
Check out my website here.
-
May 30th, 2000, 03:38 PM
#12
transcendental analytic
Meg, no End if is the end of an if, like end sub is the end of a sub. Why isn't there any exit ifs?
Code:
IF condition then
.
.
50 lines of code
.
exit if
.
100 lines of code
.
.
end if
Except when you have a goto
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 30th, 2000, 06:11 PM
#13
Fanatic Member
I didn't realize that GoSub was still in the language !!!
I remember it from QB & TRS80 days
don't really nead it, haven't really thought about it
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 31st, 2000, 05:42 AM
#14
GoSub and Wend and probably a bunch of others are really old, but are still part of the original BASIC language, so MS probablky wanted to keep them.
P.S, what is TRS80???
-
May 31st, 2000, 06:17 AM
#15
Fanatic Member
Once apon a time (about 15+ years ago) there was a computer by radioshack called a TRS-80, you pluged it into the TV and it's OS was a flashing cursor that you could type basic into and save it to cassette! There were a few types, mine had 16kilobytes of ram, no HDD etc. the "BIG" ones had 512k ram and more colours (same os though)
It was around between the time of the Commodor Vic-20 and C-64.
I don't suppose you remember the ads?
"Are you keeping up with the Commodor?, 'cause the commodor's keeping up with you!"
But it didn't keep up because I don't think they made any after the C-64 which was quite popular for games before the Atari-ST and Amiga hit the market
bit of history there for the school kids!
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 31st, 2000, 06:28 AM
#16
transcendental analytic
Also another good thing with goto:
Code:
Do
lines
goto label
lines
loop
lines
label:
lines
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 31st, 2000, 06:31 AM
#17
Paul282: I probably wasen't even born back then...Did they actually have any good programs for the computers?
-
May 31st, 2000, 06:34 AM
#18
PowerPoster
Kedaman, you won't ever need that. You have to check something to know if you need to exit the if. And when you're checking that you can put the rest of the code in this if statement. See:
Code:
IF condition then
.
.
50 lines of code
.
If condition Then
'exit if
Else
100 lines of code
End If
.
.
End If
-
May 31st, 2000, 06:45 AM
#19
Hyperactive Member
TRS80
Hi,
affectionately known as the trash 80.
Al.
A computer is a tool, not a toy.
-
May 31st, 2000, 06:48 AM
#20
Fanatic Member
Well,
I thought so at the time :-)
I was about 13, and comuters weren't that common in the home and more often than not just for games anyway, didn't even know anyone with a modem back then. Decent games didn't really appear till the C-64, then the Atari-ST and Amiga started to bring some better graphics in (8-bit)
text based games were bigger back then because of the poor graphics too.
Although a friend of mine had an apple-IIe (green screen) and people marvelled at the 128k of ram, that had some good games. Rescue raiders, Chop-lifter, taipan, Karateka. Also had a printer and 5 1/4 inch drive which was pretty impressive.
I'm starting to feel old...
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 31st, 2000, 06:51 AM
#21
Fanatic Member
Re: TRS80
Originally posted by Al Smith
Hi,
affectionately known as the trash 80.
Al.
hahah, you know it then!
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 31st, 2000, 06:58 AM
#22
Has anyone heard of TI83's? We're going to have to use them next week in my course, and i have no clue what they are.
-
May 31st, 2000, 07:17 AM
#23
transcendental analytic
Actually fox, i have more complicated structures than that, i have one starting from do with 80 lines and indeting level to 14, and i have no goto's there, not one, but it surely is confusing to run trough it. And for my latest and in my opinion best application, is using goto in a select case based structure (to exit the select case) Can you that in another way too Fox?
Also, Meg, TI83 is my best friend, it's a graphical calculator with inbuilt programming language and can be connected to your pc, and it handles complex values and everything, TI83 is great!
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 31st, 2000, 07:32 AM
#24
Fanatic Member
indents to 14 levels ????
couln't you have thrown a function or two in there somewhere?
seems a bit excessive.
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 31st, 2000, 07:32 AM
#25
PowerPoster
Since there is no break() or continue() in VB I dont think so Maybe you should put the code in functions to make it easier to handle... I never use GoTos in my code
-
May 31st, 2000, 07:52 AM
#26
transcendental analytic
Hehe, of course i have functions, but that in project i made alot of my functions inline 
Jump, goto, jump fast, jump with goto all time, hey exit select and exit if would be nice in vb7...
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|