24 Replies - 2915 Views - Last Post: 03 March 2010 - 03:14 PM
#1
The demerits of goto
Posted 14 October 2009 - 07:26 PM
I was wondering when explaining why goto statments should be avoided; what explainations others use.
Replies To: The demerits of goto
#2
Re: The demerits of goto
Posted 14 October 2009 - 07:30 PM
As for when to use them, .... hmmmm .... I think pretty much never.
#3
Re: The demerits of goto
Posted 14 October 2009 - 07:41 PM
Dogstopper, on 14 Oct, 2009 - 09:30 PM, said:
As for when to use them, .... hmmmm .... I think pretty much never.
I consider what you wrote a given; just was curious if someone had a better explanation I can give besides, "I really can't stand gotos and will not accept any project which uses them"
#4
Re: The demerits of goto
Posted 14 October 2009 - 09:54 PM
Quote
I would not say that was true -- you should never speak in absolutes!
Goto has its uses. For example in recent months I have seen it pop-up in a few meta-programs where I myself could not think of anyway to achieve the end result without them. They can be useful when using inline assembly routines. You will also see it in maintenance code -- This is generally sloppy and indicates that the maintenance developer did not fully understand the code had just had to hack a fix in -- but if you have have ever had to maintain code it can be understandable if not maddening and frustrating for the next guy.
So while they are hardly ever used and MOST of the time when they are the code could probably be re-written without them -- they DO get used on rare occasions.
There are several reasons to avoid them:
#1 Creating a large set of unique and useful labels sucks
#2 Goto's lead to unstructured code and it can be very difficult to figure out the logical flow of the program. -- i.e. spaghetti code, no matter where you look its all a mess of this-way and that.
#3 goto's often lead to dead code blocks.
#4 goto's are often a maintenance nightmare -- looking back on code months after it was developed it can be very very difficult to rebuild the logical structure in your mind.
#5 goto's tend to be the harbingers of tiny logic errors which form hard to spot bugs (usually because the only thing wrong with the code is transposed lines or a mislabeled goto). -- the block structure is much better.
code should "speak" -- you should be able to read it and have it tell the story of what it does. The control structures offer much more structure and meaning to code and allows it to be read. You can tell where a while-loop begins, its easy to spot the beginning and end of an if-else block -- these kinds of structures are HARD to pick out with your eye using goto's.
When I need to figure out a section of assembly language I generally turn to a debugger and hope for the best... when I have to figure out a section of code that uses goto's -- generally I end up at a debugger as well -- but I am pretty sharp at parsing structured code.
#5
Re: The demerits of goto
Posted 28 October 2009 - 10:47 AM
#6
Re: The demerits of goto
Posted 28 October 2009 - 08:47 PM
Linus Torvalds is no amateur.
#7
Re: The demerits of goto
Posted 31 October 2009 - 11:10 AM
#8
Re: The demerits of goto
Posted 03 November 2009 - 10:39 PM
There are other(saner) ways to do the same thing but makes shifting from assembly to c++ easier as both use labels
Bottom line : NOT Recommended for code other people would be reading and changing
Spaghetti code anyone?
#9
Re: The demerits of goto
Posted 08 November 2009 - 12:26 PM
Usually i'll use them when you have a loop which might need to be exited for various reasons. Now, you could have an extra bool and set/reset/set/reset it and use continues, but sometimes i find it a whole heck of a lot easier to:
while(condition) {
do_crap();
if (exit_condition) {
//we have an error!
goto exit_loop;
}
for (blah) {
do_other_crap();
do {
blah;
if (exit_condition_2) {
//we have an error!
goto exit_loop;
}
} while (yet_another_condition);
}
do_yet_more_crap();
//here insert more crap...
}
exit_loop:
you can see how it can get complicated with more exit conditions, and then with more nested loops and saving exit codes it can get complicated. especially if condition happens to be ((c=a = b )-3*a)|0xFF!=e.
So, when you have nested loops and complex code, sometimes a goto can actually simplify things and allow more structure, instead of making things more complicated by having extra code just to handle the structure. Sometimes a single goto looks less like spaghetti then 50,000 if(cond)break; statements and a bunch of bools that nobody can seem to remember the meaning of...
Hey, C++ Standards Committee: Give us some sort of labeled break so we don't have to deal with all of these extraneous iffs and random break!
</schpeel>
This post has been edited by polymath: 08 November 2009 - 12:26 PM
#10
Re: The demerits of goto
Posted 18 November 2009 - 06:21 PM
Seriously, Goto's do NOT create bad code, CODERS create bad code. Let's get it right. Especially when Goto is a perfectly acceptable thing in code when used RIGHT.
#11
Re: The demerits of goto
Posted 18 November 2009 - 06:35 PM
There's just no need.
#12
Re: The demerits of goto
Posted 18 November 2009 - 06:56 PM
This post has been edited by Dogstopper: 18 November 2009 - 07:01 PM
#13
Re: The demerits of goto
Posted 25 November 2009 - 02:02 PM
In short I agree with Linus in the above link...don't let someone's personal preference dissuade you from doing what makes your code more efficient and more readable.
#14
Re: The demerits of goto
Posted 26 November 2009 - 06:54 AM
In other words, they don't want to rewrite a largeish block of code because additional time will then need to go into peer reviewing and testing of code which may otherwise have not needed to change for a minor tweak.
This is a double-edged sword, because the existance of 1 goto in a block of code will often encourage future enhancements to be built in the same vein; Depending how often that function's logic needs to change, it doesn't take too long before a simple quick-fix gets messy. Eventually the day comes when something is so indecipherable and inter-locked with dependencies that rewriting it becomes a major task needing many development and testing hours.
just to illustrate what sometimes happens when several different developers get their hand on some 'goto' code, stealing Polymath's example (I've seen worse examples than this in production code...)
while(condition) {
do_crap();
if (exit_condition) {
//we have an error!
goto exit_loop;
}
else if(exit_condition2) {
//we have a different error!
goto exit_loop2;
}
for (blah) {
do_other_crap();
do {
blah;
if (exit_condition_3) {
//we have an error!
goto exit_loop;
}
} while (yet_another_condition);
}
do_yet_more_crap();
if(exit_condition_4)
goto exit_loop2;
//here insert more crap...
}
exit_loop:
// stuff for exit_loop
goto exit_loop3;
exit_loop2:
//stuff for exit_loop2
exit_loop3:
In an ideal world, I would always opt for re-designing away from 'goto' sooner rather than later (but of course business considerations trump technical ones). I can think of few people who would enjoy the task of tracing through dozens of lines of such code as you can see above.
Again, it comes down to individual programmers and their own judgement - if the boss wants something done 5 minutes ago, it might be a choice between a quick 'goto' fix and the boss having to explain to senior management why an airtight deadline worth £XXXXX has been put back by a day (In which case the boss will probably make that rather easy choice for you)
#15
Re: The demerits of goto
Posted 26 November 2009 - 11:56 AM
Bench, on 26 Nov, 2009 - 09:54 AM, said:
This is one of many areas where the typical short-term business mindset ends up costing more than it saves. Taking the time to do things right is the way to save money and time. Inserting a quick goto now may seem like a cost efficient solution, but the trouble it will cause later far offsets any short term gains.
|
|

New Topic/Question
Reply



MultiQuote












|