CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

View Poll Results: Is goto an acceptable command in VB?

Voters
15. You may not vote on this poll
  • Yes!

    5 33.33%
  • No!

    2 13.33%
  • What's Goto!?

    0 0%
  • Whatever Tickles Your Fancy...

    2 13.33%
  • Only in error trapping

    5 33.33%
  • It depends

    1 6.67%
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2002
    Location
    CANADA
    Posts
    55

    Using Goto in VB

    Well, I'm reading a C++ Book. It says that using the goto command in C++ is shunned by programmers. I'm just wondering if that applies to vb...

    Now not being an expert programmer, I've found the goto command very useful in vb to get out of loops, and just to streamline my whole code.

    So thats the question at hand... Is goto an acceptable command in VB?

    -Edited by Cimperiali to add two more options
    Last edited by Cimperiali; January 28th, 2003 at 11:52 AM.

  2. #2
    Join Date
    Nov 2002
    Location
    Oakville, Ontario
    Posts
    48
    Good question for debate....I think what your c++ source is intimating is the notion of 'spaghetti code' which is an archaic style of programming. In today's world of modular and object oriented approaches, using goto statements to jump around various bits of logic is indeed frowned upon, however, in VB, the goto is obviously needed in the error handling department (on err goto error_trap ie.) This sort of flow control is certainly useful, but if you are using goto to jump to various parts of code that could be encapsulated in a sub or function (perhaps within a class or module) the latter is definately preferred. Otherwise, altering the app down the road would be nightmarish, not to mention the great advantage in terms of code reusibility that is gained with the modular approach....and that's my two cents...next!!

    DA

  3. #3
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    I think you need to modify the poll to add "It Depends"

    I don't think any of the poll answers actually reflect what I think. It is good (and pretty much imperative for error handling), but I've seen it used in some pretty crazy ways!

  4. #4
    Join Date
    Nov 2002
    Posts
    86
    Used properly and not to excess its excellent. I like it...
    but like TwoDogs said, it can be misused.

  5. #5
    Join Date
    Jan 2003
    Posts
    50

    Cool goto has ots own place

    Yes GOTO has its own place in coding. In some cases like error handling we have no other option but GOTO.

    But i think i is not good idea to use this command in any logic. GOTO is makes code less-readable and less-undertandable.

  6. #6
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

    Talking

    Well there's no harm in GOTO as long as it is handled properly. In fact, it is useful in many ways!
    Busy

  7. #7
    Join Date
    Feb 2002
    Location
    Makati City, Philippines
    Posts
    1,054
    It's the fastest 'jump' because it doesn't have the overhead of inserting return address to the stack that are common on jump commands for those so-called 'structured' programming languages.
    Marketing our skills - please participate in the survey and share your insights
    -

  8. #8
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    I use it only in error trapping

    And usually avoid it as long as I can in normal code.
    But I accept it as long as you're able not to make code obfuscated
    That is: Clarity of code (not only for me but also for those who may follow) is a priority to which I am ready to sacrify time (mine, in coding more, and of Pgm in quikness).

    Have a nice day, you all

    Cesare Imperiali
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  9. #9
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Exit function substitute

    It depends of the use of the GoTo, most of the time, it can be avoided, however, I think it can make a good Exit Function substitute.

    Lets think about a validation function validating many entries. You will probably have to use Exit Function now and then, like:

    If not IsNumeric(Variable) Then
    Exit Function
    End if

    However, you could want to replace the Exit Function by a GoTo EndOfFunction, so you can do additionnal thing when the function exit, like resetting the MousePointer of the screen to normal.

    Most of the times, you can avoid (or replace) a GoTo by using imbricated IF, Select statement and Loop. Also, naming the Goto Label is important. I did never use Goto except for error trapping and to replace Exit function, it can be avoided in most of the case

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

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

Featured