Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   What's the deal with Goto's? (http://www.programmingforums.org/showthread.php?t=7834)

Indigno Jan 4th, 2006 6:01 PM

What's the deal with Goto's?
 
My programming teacher absolutely hates them. I find them dead usefull and a lot easier to set than loops. I asked him once and all he could come up with is "I dunno, I just don't like them."

OpenLoop Jan 4th, 2006 6:55 PM

Short Story: Goto's are EVIL
Longer story: A good design principle is that a module should have one exit point (e.g: return; in C). Goto violates the hell out of that principle rendering your source code unreadable (except by you).
Long Story: I'll let someone else do it :)

para Jan 4th, 2006 7:02 PM

Goto's are "bad" since they avoid using high level language control structures.
These include (in C):
:

if (x) { ... } else if { ... } else { ... }
while (x) { ... }
do { ... } while (x);
switch (x) { ... }

And so on. These control structures give you virtually everything you need, and goto's are really only useful (so far that I've enountered) is with abstract program flow (like jumping directly to labels without a switch construct).

I advise forgetting all about goto.

elford Jan 4th, 2006 7:46 PM

Quote:

Originally Posted by Indigno
My programming teacher absolutely hates them. I find them dead usefull and a lot easier to set than loops. I asked him once and all he could come up with is "I dunno, I just don't like them."

For small programs, I can see why someone might find them easier than loops, but if you're doing a project of any decent size (even as small as 100 lines), your code becomes an unintelligble mess with all the jumps that occur. Object oriented design principles avoid the goto for a very good reason.

Plus...I don't see how you can use them to get around loops. In a current project I'm doing, there's a data structure called a linked list, and i need a loop to do something on every node of the list, there is no way of not doing that without a loop.

para Jan 4th, 2006 8:16 PM

Quote:

Originally Posted by elford
Plus...I don't see how you can use them to get around loops. In a current project I'm doing, there's a data structure called a linked list, and i need a loop to do something on every node of the list, there is no way of not doing that without a loop.

You just have a conditionally executed goto to a label where the loop should start. This is how loops are done in assembly. The example of a linked list is good -- you'll actually have less code trying to find a specific node with a loop than using a goto structure.

:

while (not last node) {
  if (this node = the node i want)
      break;
  this node = next node;
}

:

start:
if (this is the last node)
  goto end;
if (this node = the node i want)
  goto end;
this node = next node;
goto start;
end:


The first is a lot cleaner, especially if you have a situation in which you need nested looping constructs.

B3TA_SCR1PT3R Jan 4th, 2006 9:21 PM

2 words: spaghetti code ;)

Silvanus Jan 4th, 2006 9:49 PM

Gotos are also used in very simple languages when functions are not available- it allows one to reuse code without typing it twice. They really have no point in a higher-level language, though, as other people have said.

Rory Jan 5th, 2006 1:18 AM

Yeah goto's are bad, but it's the only way of implementing structured error handling in VB6 for instance. Also there are some specific algorithms that rely on the goto that would be very hard and inefficient to implement otherwise. One thing that perhaps could be more useful is the Gosub...Return structure, similar to a static (variables) dynamic (execution order) inline routine, which allows more controlled jumping, but you're still jumping.

Indigno Jan 5th, 2006 6:48 AM

I use loops when I can, but I find it easier just to "jump" back to the beginning of a program with a goto.

Ooble Jan 5th, 2006 4:14 PM

It may be easier to write, but it's definitely harder to read. My advice is this: code everything with the view "If I come back to this in a year's time, will I understand what I've just written?"


All times are GMT -5. The time now is 6:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC