Results 1 to 26 of 26

Thread: Evils of GOSUB?

  1. #1

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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?

  2. #2
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845
    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?


    Mark
    -------------------

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    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.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  5. #5

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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.

  6. #6
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Wink

    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.





  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  8. #8
    Guest
    Whodo you exit an if? you mean IF as in the If...Then... statement? Just use End If

  9. #9
    Guest

    Cool 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.

  10. #10
    Hyperactive Member Juan Carlos Rey's Avatar
    Join Date
    Aug 1999
    Location
    Mendoza, Argentina
    Posts
    301

    Talking

    You mean that Completely Obsolete Business Oriented Language?

    Do not abuse GOTO's -keep jumps short too.

  11. #11

    Thread Starter
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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.

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  13. #13
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    Question

    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!)

  14. #14
    Guest
    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???

  15. #15
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    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!)

  16. #16
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  17. #17
    Guest
    Paul282: I probably wasen't even born back then...Did they actually have any good programs for the computers?

  18. #18
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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

  19. #19
    Hyperactive Member Al Smith's Avatar
    Join Date
    May 1999
    Location
    Marcellus, MI. USA
    Posts
    330

    TRS80

    Hi,
    affectionately known as the trash 80.
    Al.
    A computer is a tool, not a toy.

  20. #20
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    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!)

  21. #21
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840

    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!)

  22. #22
    Guest
    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.

  23. #23
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  24. #24
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    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!)

  25. #25
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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

  26. #26
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
  •  



Click Here to Expand Forum to Full Width