Based on a Case-Study done in Code Complete 2ed, by MSPress... This is what they concluded about the use of goto's:
( pg. 408 )
Quote:
The Argument Against gotos
The general argument against gotos is that code without gotos is higher-quality code.
The famous letter that sparked the original controversy was Edsger Dijkstra's "Go To
Statement Considered Harmful" in the March 1968 Communications of the ACM.
Dijkstra observed that the quality of code was inversely proportional to the number of
gotos the programmer used. In subsequent work, Dijkstra has argued that code that
doesn't contain gotos can more easily be proven correct.
Code containing gotos is hard to format. Indentation should be used to show logical
structure, and gotos have an effect on logical structure. Using indentation to show the
logical structure of a goto and its target, however, is difficult or impossible.
Use of gotos defeats compiler optimizations. Some optimizations depend on a program's
flow of control residing within a few statements. An unconditional goto makes the flow
harder to analyze and reduces the ability of the compiler to optimize the code. Thus,
even if introducing a goto produces an efficiency at the source-language level, it may
well reduce overall efficiency by thwarting compiler optimizations.
Proponents of gotos sometimes argue that they make code faster or smaller. But code
containing gotos is rarely the fastest or smallest possible. Donald Knuth's marvelous,
classic article "Structured Programming with go to Statements" gives several examples
of cases in which using gotos makes for slower and larger code (Knuth 1974).
In practice, the use of gotos leads to the violation of the principle that code should flow
strictly from top to bottom. Even if gotos aren't confusing when used carefully, once
gotos are introduced, they spread through the code like termites through a rotting house.
If any gotos are allowed, the bad creep in with the good, so it's better not to allow any of
them.
Overall, experience in the two decades that followed the publication of Dijkstra's letter
showed the folly of producing goto-laden code. In a survey of the literature, Ben
Shneiderman concluded that the evidence supports Dijkstra's view that we're better off
without the goto (1980), and many modern languages, including Java, don't even have
gotos.
The Argument for gotos
The argument for the goto is characterized by an advocacy of its careful use in specific
circumstances rather than its indiscriminate use. Most arguments against gotos speak
against indiscriminate use. The goto controversy erupted when Fortran was the most
popular language. Fortran had no presentable loop structures, and in the absence of
good advice on programming loops with gotos, programmers wrote a lot of spaghetti
code. Such code was undoubtedly correlated with the production of low-quality
programs, but it has little to do with the careful use of a goto to make up for a gap in a
modern language's capabilities.
A well-placed goto can eliminate the need for duplicate code. Duplicate code leads to
problems if the two sets of code are modified differently. Duplicate code increases the
size of source and executable files. The bad effects of the goto are outweighed in such a
case by the risks of duplicate code.
The goto is useful in a routine that allocates resources, performs operations on those
resources, and then deallocates the resources. With a goto, you can clean up in one
section of code. The goto reduces the likelihood of your forgetting to deallocate the
resources in each place you detect an error.
In some cases, the goto can result in faster and smaller code. Knuth's 1974 article cited a
few cases in which the goto produced a legitimate gain.
Good programming doesn't mean eliminating gotos. Methodical decomposition,
refinement, and selection of control structures automatically lead to goto-free programs
in most cases. Achieving goto-less code is not the aim but the outcome, and putting the
focus on avoiding gotos isn't helpful.
Decades' worth of research with gotos failed to demonstrate their harmfulness. In a
survey of the literature, B. A. Sheil concluded that unrealistic test conditions, poor data
analysis, and inconclusive results failed to support the claim of Shneiderman and others
that the number of bugs in code was proportional to the number of gotos (1981). Sheil
didn't go so far as to conclude that using gotos is a good idea—rather, that experimental
evidence against them was not conclusive.
The evidence suggests only that deliberately chaotic control structure degrades
[programmer] performance. These experiments provide virtually no evidence for the
beneficial effect of any specific method of structuring control flow.
—B. A. Sheil
Finally, the goto has been incorporated into many modern languages, including Visual
Basic, C++, and the Ada language, the most carefully engineered programming
language in history. Ada was developed long after the arguments on both sides of the
goto debate had been fully developed, and after considering all sides of the issue, Ada's
engineers decided to include the goto.
|
I hope that gives a premise of debate on the topic.
If you wish to read more on the topic, you could read it, if you register for free, using the http://cc2e.com/ website.
|