Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 4th, 2006, 6:01 PM   #1
Indigno
Professional Programmer
 
Indigno's Avatar
 
Join Date: Dec 2005
Location: Anywhere non-productive
Posts: 267
Rep Power: 0 Indigno is an unknown quantity at this point
Send a message via AIM to Indigno Send a message via MSN to Indigno Send a message via Yahoo to Indigno
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."
Indigno is offline   Reply With Quote
Old Jan 4th, 2006, 6:55 PM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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
OpenLoop is offline   Reply With Quote
Old Jan 4th, 2006, 7:02 PM   #3
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
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.
para is offline   Reply With Quote
Old Jan 4th, 2006, 7:46 PM   #4
elford
Programmer
 
elford's Avatar
 
Join Date: Nov 2005
Posts: 35
Rep Power: 0 elford is on a distinguished road
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.
elford is offline   Reply With Quote
Old Jan 4th, 2006, 8:16 PM   #5
para
Programmer
 
Join Date: Dec 2005
Posts: 65
Rep Power: 3 para is on a distinguished road
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.
para is offline   Reply With Quote
Old Jan 4th, 2006, 9:21 PM   #6
B3TA_SCR1PT3R
Hobbyist Programmer
 
B3TA_SCR1PT3R's Avatar
 
Join Date: Jul 2005
Location: Dallas, Texas
Posts: 101
Rep Power: 0 B3TA_SCR1PT3R is an unknown quantity at this point
Send a message via AIM to B3TA_SCR1PT3R
2 words: spaghetti code
__________________
Hoes telling me to calm down but I'm like fuck that shit!
B3TA_SCR1PT3R is offline   Reply With Quote
Old Jan 4th, 2006, 9:49 PM   #7
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 3 Silvanus is on a distinguished road
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.
__________________
:wq
Silvanus is offline   Reply With Quote
Old Jan 5th, 2006, 1:18 AM   #8
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
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.
Rory is offline   Reply With Quote
Old Jan 5th, 2006, 6:48 AM   #9
Indigno
Professional Programmer
 
Indigno's Avatar
 
Join Date: Dec 2005
Location: Anywhere non-productive
Posts: 267
Rep Power: 0 Indigno is an unknown quantity at this point
Send a message via AIM to Indigno Send a message via MSN to Indigno Send a message via Yahoo to Indigno
I use loops when I can, but I find it easier just to "jump" back to the beginning of a program with a goto.
Indigno is offline   Reply With Quote
Old Jan 5th, 2006, 4:14 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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?"
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:38 PM.

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