Beginner Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOtherBeginner Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 21st, 2011, 10:22 PM
mahaju mahaju is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 120 mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 2 h 17 m 3 sec
Reputation Power: 20
Why is goto a bad programming practice?

Why is goto considered harmful?
I have been thinking about this for some time but can't find any plausible answer. What is the reason, exactly, that a goto statement is considered harmful? Why is it bad programming practice? I have been trained to write programs without goto and also avoid using it, but what is the thing actually hat makes it bad programming anyway? The only reason I have found it to be considered bad programming practice is due to a paper by Djikstra, but I haven't been able to get the paper. Does anyone have a convincing answer?

Reply With Quote
  #2  
Old February 22nd, 2011, 10:24 PM
cueball cueball is offline
Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2011
Posts: 11 cueball User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 26 m 10 sec
Reputation Power: 0
It can easily lead to "spaghetti code". In untrained hands, goto can be used to create a tangled web of code where code can be redirected to anywhere else in the code without consideration for proper constructs like loops and conditional blocks.

eg. a goto statement could redirect the code to a line in the middle of a while or for loop.

Reply With Quote
  #3  
Old February 25th, 2011, 11:30 AM
delnan delnan is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2009
Posts: 383 delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)delnan User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Week 7 h 31 m
Reputation Power: 0
It's not that it's evil per se. But unless you're programming in an ancient, low-level (by today's standards) language like early BASICs and machine code, you have more abstract control structures available than are easier to use, require less code and are more easily followed by the reader. Programmers (except perhaps those constantly working in enviroments that require otherwise) don't think in terms of jumps. A conditional gotos that emulates a for loop "works", and is indeed equivalent from a purely theoretical viewpoint, but it's less obvious to the reader what the code actually does (well, it jumps to X if some condition is met, but that's not what you want to know when you read it - you want to know what it's counting from n to m). This gets worse as control flow gets more sophisticated. An iterator-based loop with a conditional break; and continue; contining a few if statements can easily have more than a dozen gotos, and you have to dig through all of them, make sure the translation from "do this for every number i in n...m" to "count and jump around like this" is correct, etc.

And then there's the abuse potential: Some bat**** insane guy can go and jump in the middle of your code, so strictly speaking you can never assume the line before actually executed. Adding extra checks won't help though, as they too require additional lines that may be jumped over - not to mention that you shouldn't have to concern yourself with such madness. And in fact, many (in particular beginners who don't know better - sign me up for this) lack the discipline not to create this kind of mess where everyone jumps everywhere and mentally tracing execution becomes very hard (if not practically impossible). Those who do have the discipline would (in 99.99% of all cases) only recreate existing control structures, so they just use those. Unless, of course, the language in use lacks them.

Note that all this implies thta goto IS okay when you have no high-level control structure that can be used to solve this problem better. E.g. cleanup in C (which has no exception handling and no RAII) - put cleanup + return in the end. If an error occurs, jump there, and if no error occurs, you get there as well. Prevents deeeeeep nesting of conditionals.

Reply With Quote
  #4  
Old April 14th, 2011, 12:37 AM
mahaju mahaju is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 120 mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 2 h 17 m 3 sec
Reputation Power: 20
It seems that goto is considered harmful only if I use it to create elaborate loops and sub routine calls such as for, while loops and functions. In fact, I have read that after Djikstra published his paper, some other prominent people working in the field of computers at that time published their own counter arguments to Djikstra, which made Djikstra publish further counterarguments to those counterarguments. So it seems, the practice of considering goto a bad habit was itself debatable. So goto can lead to spaghetti code. Considering this true, what if I just use goto with some if then conditions to skip just few lines or blocks of code, never nest goto, never use goto to enter or leave a block and avoid using goto to move back in a program. Would this be a bad practice then?

I always thought it has something to do with the design of compilers which makes goto a bad practice, but now I'm beginning to think I was wrong

Reply With Quote
  #5  
Old April 14th, 2011, 08:28 AM
MBirchmeier's Avatar
MBirchmeier MBirchmeier is offline
I <3 ASCII
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Posts: 2,395 MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 33 m 9 sec
Reputation Power: 1231
Send a message via AIM to MBirchmeier
Quote:
prominent people working in the field of computers at that time published their own counter arguments to Djikstra


This was published in the late 60's. If you want to base rules of modern computers on 40 year old arguments (yes I know the counter arguments were later), a lot has changed. Computers had kilobytes of memory and megabytes of hard drive, kilohertz processors and bandwidth referred to how fast you could read off of disks, not from anywhere in the world.

Quote:
Considering this true, what if I just use goto with some if then conditions to skip just few lines

Behind the scenes an if statement is just a true check and a goto. The same is true with for loops, while loops, function calls, and any other advanced structure you might use, compile down to goto statements (or jumps and page jumps if you want to get technical). Anything you might be saving by doing a goto inside an if statement is actually just costing you two gotos. Instead of using an "If (condition) Goto blah End If do_stuff() declare blah" Try using "If (Not Condition) do)stuff() End If"

Quote:
I always thought it has something to do with the design of compilers which makes goto a bad practice,

Er.... kinda, but it's not for the compiler's sake it's for yours. By avoiding goto's in favor of more complex structures that use goto's the compiler has a better idea what is going on. It can understand the flow of the structures, and do extra checks like making sure you have the variables declared (or hasn't been finalized) before using them, or making sure your call stack stays accurate and appropriate variables get passed back. And that variables leave scope when they're no longer used in the loop. By using these structures you're gaining all of that logic as well, and the compiler now has the ability to check for these things instead of only finding out at runtime.

-MBirchmeier
__________________
My fiancee's transition from accountant to writer
0x4279 7465 204D 6521

Reply With Quote
  #6  
Old April 18th, 2011, 06:41 AM
mahaju mahaju is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 120 mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level)mahaju User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 2 h 17 m 3 sec
Reputation Power: 20
So scarce and controlled use of goto should be legalized (in the academic sector as well I mean)? What do you think?

Reply With Quote
  #7  
Old April 18th, 2011, 08:28 AM
MBirchmeier's Avatar
MBirchmeier MBirchmeier is offline
I <3 ASCII
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Posts: 2,395 MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level)MBirchmeier User rank is General 4th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 33 m 9 sec
Reputation Power: 1231
Send a message via AIM to MBirchmeier
Quote:
Originally Posted by mahaju
So scarce and controlled use of goto should be legalized (in the academic sector as well I mean)? What do you think?
I think you either did not understand or did not read what I posted. And that if you did not understand it and you're truly interested in learning you should be asking questions. Rather than simply re-asserting your statement. If you didn't read it or just have no interest in learning then I'm guessing we're done here anyway.

-MBirchmeier

Reply With Quote
  #8  
Old July 18th, 2011, 02:54 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 618 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 13 h 50 m
Reputation Power: 26
Quote:
Originally Posted by mahaju
So scarce and controlled use of goto should be legalized (in the academic sector as well I mean)? What do you think?


No.

In nearly 2 decades of programming professionally I have never used a GOTO, nor come across a use of GOTO that could not be avoided by better design.
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

Frank Zappa

Reply With Quote
  #9  
Old July 18th, 2011, 10:47 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,811 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
Quote:
Behind the scenes an if statement is just a true check and a goto.
Learn assembly language. Write a program in assembly language. Debug your own bugs in assembly language.

That's the only way to truly appreciate the horror of goto: using a language with ONLY GOTO.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #10  
Old July 25th, 2011, 04:08 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,390 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 22 h 32 m 40 sec
Reputation Power: 4080
^^^
Actually, Knuth proved that it is perfectly possible to write good code with GOTO and in some problem domains, the alternatives are more complicated .

I use goto sparingly in my own C code, mostly for error handling [1]. In fact, it is a pet theory of mine that if there aren't enough goto statements in a non-trivial C program, maybe the programmer isn't doing enough error checking or leaving resources open.

-----------------
[1] Code that I write usually run as daemons for very long periods of time. Using exit() is often not an option and resource deallocation is very important. Incidentally, any decent OS (Windows, Linux, *BSD) code or long running daemon code (such as Apache, various mail servers etc.) contains goto as well. A lot of the Windows C API examples contain goto as well. Here's Linus Torvalds' word on the subject
Comments on this post
ManiacDan agrees: ^^
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #11  
Old August 1st, 2011, 10:42 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
What it really comes down to is that GOTO is a very low-level construct, and a very general and powerful one, but one which lends itself to abuse. The construct itself isn't the problem, for the most part; it is the use to which it can be put. This was a much bigger issue in the past, because the importance of being able to read existing code wasn't fully appreciated, but the debate is one which is for the most part long over.

In the end, the main argument against GOTO (and it's more abstract cousin, continuations) is that there are a large number of less general, but more structured, constructs which can replace it, in a way that makes reading the resulting code generally easier. It is a very rare case when GOTO is more readable than the equivalent in other forms such as for() or while() loops, recursive calls, or simply decomposing the problem further. Such cases do occur, but I doubt most C programmers see more than one or two such cases in a career, and it is even rarer in higher-level languages.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : August 1st, 2011 at 10:44 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOtherBeginner Programming > Why is goto a bad programming practice?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap