By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,473 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

go_to what's in using them

P: n/a
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?

Thanks in advance,
Manish

Nov 9 '06 #1
Share this Question
Share on Google+
23 Replies


P: n/a
In article <11**********************@h48g2000cwc.googlegroups .com>,
ma****@gmail.com <ma****@gmail.comwrote:
>I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
>Can any one here shed some light?
You may wish to search out the famous article,
"Go To Statement Considered Harmful" by Dijkstra.
--
All is vanity. -- Ecclesiastes
Nov 9 '06 #2

P: n/a

ma****@gmail.com wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?
See the Wirth article referenced elsethread.

To summarize:
a) the biggest cost in development is the cost of the programmer.
b) programmers spend more time debugging and maintaining existing code
than developing new code
c) the use of "goto" complicates code maintenance as it increases the
number of logic paths, while providing little or no visual guidance to
the maintenance programmer
d) thus causing the programmer to spend more time and energy
understanding the existing logic and developing suitable new logic that
will not conflict with the existing goto statements,
e) thus causing an unnecessary inflation of development time and costs
f) Also, the use of "goto" complicates code debugging as it leaves no
trail to backtrace from,
g) thus causing the programmer to spend more time and energy searching
for possible hidden logic paths that lead to the abnormal condition,
h) thus causing an unnecessary inflation of debugging time and costs

On the other hand, gotoless programming leaves the code in an easily
readable state in which all logic paths are clearly delimited. Thus
development and maintenance programmers spend less time trying to
understand the code, which keeps development costs (time and money)
down.

HTH
--
Lew

Nov 9 '06 #3

P: n/a

Lew Pitcher wrote:
ma****@gmail.com wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?

See the Wirth article referenced elsethread.
Sorry, I meant Dyjkstra, not Wirth

Nov 9 '06 #4

P: n/a
Walter Roberson wrote:
>
In article <11**********************@h48g2000cwc.googlegroups .com>,
ma****@gmail.com <ma****@gmail.comwrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
Can any one here shed some light?

You may wish to search out the famous article,
"Go To Statement Considered Harmful" by Dijkstra.
Me thinks he first needs to read "Trolling Considered Harmful".

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
Nov 9 '06 #5

P: n/a
ma****@gmail.com said:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
There isn't any reason not to use go_to in your code. Observe:

#include <stdio.h>

int main(void)
{
int go_to = 6;

printf("%d\n", go_to);

return 0;
}

This is perfectly legal, clear, readable C.
Can any one here shed some light?
More than you can possibly imagine.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 9 '06 #6

P: n/a
Op 9 Nov 2006 11:35:53 -0800 schreef Lew Pitcher:
Lew Pitcher wrote:
>ma****@gmail.com wrote:
>>I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?

See the Wirth article referenced elsethread.

Sorry, I meant Dyjkstra, not Wirth
Dutch spelling is obviously difficult:
Dijkstra, where ij was formerly regarded as one letter, sharing the same
part of the dictionary with y.
See also: IJsselmeer, IJsland. Not in Belgium, there they write Ijsselmeer
and Ijsland.
--
Coos
Nov 10 '06 #7

P: n/a
ma****@gmail.com wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
`goto`, not `go_to`.

Roughly: using goto means that the structure of your code needs
to be unscrambled by connecting the gotos (the goestos?) to the
corresponding label. This is tedious and prone to error when the
code changes [1]. /Usually/, the syntactically obvious control
structures -- if, while, for, switch, the humble semicolon -- will
do a perfectly good job of expressing structure, and with the
addition of the slightly edgy `break`, `continue`, and `return`,
you're covered for most things you'll meet in practice.

/Sometimes/ those control structures won't do the job well
enough -- for example in a error situation when you're faced
with Standard Situation N [2] -- and then a goto or two
may be the clearest way to solve the problem. (Think of this
as hand-implementing the try-catch construct C doesn't have,
more so if you use setjmp/longjmp in place of goto). But if the
situation doesn't require it, don't use it, otherwise you'll
mislead the reader into thinking either that they've missed
something, or that /you/ have.

Peeking at my interpreter code here, I see that `goto` appears
in:

(a) generated code from flex & bison, so that doesn't count
(any more than the branches and jumps in compiled code do)

(b) in /one/ other module, where it's part of the error recovery
code in a bytecode [3] interpreter: when assorted operations
break (eg trying to add a string to an integer) then control
has to pass to a place where the interpreter can implement
"throw an exception".

Otherwise there's no gotos. (This is about 20K lines of commented
non-header code, not big but hardly trivial.) But I do use
`return` not at the bottom of a function, and `break` to get out
of loops from time to time, although that's mostly for finishing
a switch-case: if I were in a bad mood, I might count those as
gotos. I don't seem to have used `continue` at all ...
Can any one here shed some light?
Heat is, alas, more likely for this subject.

[1] Rule X: the code will have to change, /especially/ the code
for which there are promises signed in blood that it will
never change.

[2] When the only important thing is to get out, and get out NOW.

[3] For values of `byte` that are `short`.

--
Chris "spinning dizzily" Dollin
Scoring, bah. If I want scoring I'll go play /Age of Steam/.

Nov 10 '06 #8

P: n/a
Coos Haak <ch*****@hccnet.nlwrote:
Op 9 Nov 2006 11:35:53 -0800 schreef Lew Pitcher:
Lew Pitcher wrote:
ma****@gmail.com wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?

See the Wirth article referenced elsethread.
Sorry, I meant Dyjkstra, not Wirth
And while you want to read that article, you also want to take it with
the knowledge in mind that it was a polemic, intended as part of a
discussion between two camps of Cleans and Scruffies. Guess which camp
Dijkstra was in? The result is that while he states his case well, he
overstates it. One should be very wary about using goto. Most people
won't need it more than a couple of times in their programming lives.
But if you happen to write state machines, parsers, or what have you, it
may be a regular part of the best solution.
Dutch spelling is obviously difficult:
Dijkstra, where ij was formerly regarded as one letter, sharing the same
part of the dictionary with y.
See also: IJsselmeer, IJsland. Not in Belgium, there they write Ijsselmeer
and Ijsland.
Yeah, but they're Belgians. Dey know nuthin'.

Richard
Nov 10 '06 #9

P: n/a

Lew Pitcher wrote:
ma****@gmail.com wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?

See the Wirth article referenced elsethread.
Wirth was also a Structured programming advocate.
>
To summarize:
a) the biggest cost in development is the cost of the programmer.
b) programmers spend more time debugging and maintaining existing code
than developing new code
c) the use of "goto" complicates code maintenance as it increases the
number of logic paths, while providing little or no visual guidance to
the maintenance programmer
d) thus causing the programmer to spend more time and energy
understanding the existing logic and developing suitable new logic that
will not conflict with the existing goto statements,
e) thus causing an unnecessary inflation of development time and costs
f) Also, the use of "goto" complicates code debugging as it leaves no
trail to backtrace from,
g) thus causing the programmer to spend more time and energy searching
for possible hidden logic paths that lead to the abnormal condition,
h) thus causing an unnecessary inflation of debugging time and costs

On the other hand, gotoless programming leaves the code in an easily
readable state in which all logic paths are clearly delimited. Thus
development and maintenance programmers spend less time trying to
understand the code, which keeps development costs (time and money)
down.

HTH
--
Lew
However even Wirth did not propose complete elimination of GOTO. Wirth
created PASCAL and still included GOTO. His chief use of GOTO was error
handling. The goal of the Structured Programming movement is often
mistakenly described as seaking GOTOless programming. Such a
description is wrong. It was the UNRESTRAINED use of goto that they
sought to eliminate.

Sadly goto might be gone in many modern OO languangues, but also gone
is any sense of readability.

Ed

Nov 10 '06 #10

P: n/a
In article <AM********************@bt.com>,
Richard Heathfield <in*****@invalid.invalidwrote:
>I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
>There isn't any reason not to use go_to in your code.
Likewise, there isn't any reason not to use to_go in your code, but
using togo results in undefined behaviour.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 10 '06 #11

P: n/a
In article <11**********************@i42g2000cwa.googlegroups .com>,
Ed Prochak <ed*******@gmail.comwrote:
>However even Wirth did not propose complete elimination of GOTO. Wirth
created PASCAL and still included GOTO. His chief use of GOTO was error
handling. The goal of the Structured Programming movement is often
mistakenly described as seaking GOTOless programming. Such a
description is wrong. It was the UNRESTRAINED use of goto that they
sought to eliminate.
Your analysis disagrees with Wirth's own published works.

http://www.swissdelphicenter.ch/en/niklauswirth.php

Yet, Pascal also suffered from certain deficiencies, more or
less significant depending on personal perception and
application. [...]

Certain other deficiencies were due to the author's lack of
courage to throw some rules inherited from Algol over board, in
fear of antagonizing influential Algol programmers. The prime
entry in this list is the famed go to statement, retained
although, in principle, always replaceable by an if, while, or
repeat construct
--
Prototypes are supertypes of their clones. -- maplesoft
Nov 10 '06 #12

P: n/a
In article <ej**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>In article <11**********************@i42g2000cwa.googlegroups .com>,
Ed Prochak <ed*******@gmail.comwrote:
>>However even Wirth did not propose complete elimination of GOTO. Wirth
created PASCAL and still included GOTO. His chief use of GOTO was error
handling. The goal of the Structured Programming movement is often
mistakenly described as seaking GOTOless programming. Such a
description is wrong. It was the UNRESTRAINED use of goto that they
sought to eliminate.
>Your analysis disagrees with Wirth's own published works.
No doubt Ed's charitable nature led him to replace Wirth's absurd
view with a more reasonable one.
The prime
entry in this list is the famed go to statement, retained
although, in principle, always replaceable by an if, while, or
repeat construct
The while and repeat constructs can always be replaced by if and goto,
yet remarkably few languages have removed them. Of all the arguments
against goto, this is the most wirthless.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 10 '06 #13

P: n/a
av
On Fri, 10 Nov 2006 09:37:47 +0000, Chris Dollin wrote:
>ma****@gmail.com wrote:
>I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

`goto`, not `go_to`.

Roughly: using goto means that the structure of your code needs
to be unscrambled by connecting the gotos (the goestos?) to the
corresponding label. This is tedious and prone to error when the
code changes [1]. /Usually/, the syntactically obvious control
bla, bla, bla
you and all ng is wrong on "goto"
because you all seems don't know how that instruction (and labels)
have to appear in the page (or in the screen).

in the first it was "goto" instruction :)
Nov 11 '06 #14

P: n/a

av wrote:
On Fri, 10 Nov 2006 09:37:47 +0000, Chris Dollin wrote:
ma****@gmail.com wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
`goto`, not `go_to`.

Roughly: using goto means that the structure of your code needs
to be unscrambled by connecting the gotos (the goestos?) to the
corresponding label. This is tedious and prone to error when the
code changes [1]. /Usually/, the syntactically obvious control

bla, bla, bla
you and all ng is wrong on "goto"
because you all seems don't know how that instruction (and labels)
have to appear in the page (or in the screen).
Huh? Not everyone reading the newsgroup thinks that goto
is horrible. Several people have alluded to that in this thread.
Many people think it's a pointless religious war.

As to your point that having the label on screen makes
code readable, consider:

int
main(void)
{

int x; x = 1; goto a; a:x -= 5; b:goto e; c:x += 4;
d:goto h; e:x -= 3; f:goto c; g:x *= 2; h:goto i;
i:x -= 1; printf("%d\n", x); return 0; goto g;
goto g; goto f; goto b; goto d;

}

Nov 11 '06 #15

P: n/a
av
On 11 Nov 2006 02:31:51 -0800, Bill Pursell wrote:
>av wrote:
>On Fri, 10 Nov 2006 09:37:47 +0000, Chris Dollin wrote:
>ma****@gmail.com wrote:

I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

`goto`, not `go_to`.

Roughly: using goto means that the structure of your code needs
to be unscrambled by connecting the gotos (the goestos?) to the
corresponding label. This is tedious and prone to error when the
code changes [1]. /Usually/, the syntactically obvious control

bla, bla, bla
you and all ng is wrong on "goto"
because you all seems don't know how that instruction (and labels)
have to appear in the page (or in the screen).

Huh? Not everyone reading the newsgroup thinks that goto
is horrible. Several people have alluded to that in this thread.
Many people think it's a pointless religious war.

As to your point that having the label on screen makes
code readable, consider:

int
main(void)
{

int x; x = 1; goto a; a:x -= 5; b:goto e; c:x += 4;
d:goto h; e:x -= 3; f:goto c; g:x *= 2; h:goto i;
i:x -= 1; printf("%d\n", x); return 0; goto g;
goto g; goto f; goto b; goto d;

}
the above is the classical example on how not to use goto

if you understand me,
and you want i have some difficulties just post some c code
that not use goto and claim that solution is the best possible
and no goto can make better that code

possobily i rewrite that code better with only(no if, while, for)
use of goto
Nov 11 '06 #16

P: n/a
av said:

<snip>
>
if you understand me,
Alas, no. Sorry.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
Nov 11 '06 #17

P: n/a
av wrote:
On 11 Nov 2006 02:31:51 -0800, Bill Pursell wrote:
av wrote:
On Fri, 10 Nov 2006 09:37:47 +0000, Chris Dollin wrote:
ma****@gmail.com wrote:

I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

`goto`, not `go_to`.

Roughly: using goto means that the structure of your code needs
to be unscrambled by connecting the gotos (the goestos?) to the
corresponding label. This is tedious and prone to error when the
code changes [1]. /Usually/, the syntactically obvious control

bla, bla, bla
you and all ng is wrong on "goto"
because you all seems don't know how that instruction (and labels)
have to appear in the page (or in the screen).
Huh? Not everyone reading the newsgroup thinks that goto
is horrible. Several people have alluded to that in this thread.
Many people think it's a pointless religious war.

As to your point that having the label on screen makes
code readable, consider:

int
main(void)
{

int x; x = 1; goto a; a:x -= 5; b:goto e; c:x += 4;
d:goto h; e:x -= 3; f:goto c; g:x *= 2; h:goto i;
i:x -= 1; printf("%d\n", x); return 0; goto g;
goto g; goto f; goto b; goto d;

}

the above is the classical example on how not to use goto

if you understand me,
and you want i have some difficulties just post some c code
that not use goto and claim that solution is the best possible
and no goto can make better that code

possobily i rewrite that code better with only(no if, while, for)
use of goto
And very likely, your rewrite of that code would also be a classical
example on how not to use goto.

Nov 11 '06 #18

P: n/a
av
On Thu, 9 Nov 2006 19:11:22 +0000 (UTC), Walter Roberson wrote:
>In article <11**********************@h48g2000cwc.googlegroups .com>,
ma****@gmail.com <ma****@gmail.comwrote:
>>I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
>>Can any one here shed some light?

You may wish to search out the famous article,
"Go To Statement Considered Harmful" by Dijkstra.
"while, for, ifs Statements Considered Harmful" by me :)
Nov 13 '06 #19

P: n/a
av wrote:
On Thu, 9 Nov 2006 19:11:22 +0000 (UTC), Walter Roberson wrote:
In article <11**********************@h48g2000cwc.googlegroups .com>,
ma****@gmail.com <ma****@gmail.comwrote:
>I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?
>Can any one here shed some light?
You may wish to search out the famous article,
"Go To Statement Considered Harmful" by Dijkstra.

"while, for, ifs Statements Considered Harmful" by me :)
Are you 'ab' of alt.lang.asm? If so, seemingly, you don't like
traditional assembler nor do you like C! Maybe you should start a group
for your private language, tons of whose source you regularly spam
a.l.a with...

Nov 13 '06 #20

P: n/a
av
On 13 Nov 2006 04:01:50 -0800, santosh wrote:
>av wrote:
>On Thu, 9 Nov 2006 19:11:22 +0000 (UTC), Walter Roberson wrote:
>In article <11**********************@h48g2000cwc.googlegroups .com>,
ma****@gmail.com <ma****@gmail.comwrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?

You may wish to search out the famous article,
"Go To Statement Considered Harmful" by Dijkstra.

"while, for, ifs Statements Considered Harmful" by me :)

Are you 'ab' of alt.lang.asm? If so, seemingly, you don't like
traditional assembler nor do you like C! Maybe you should start a group
for your private language, tons of whose source you regularly spam
a.l.a with...
tons of tons
Nov 13 '06 #21

P: n/a

ma****@gmail.com wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?

Thanks in advance,
Manish
Undisciplined use of goto (not go_to) can result in code that is
difficult to read and maintain. It can also inhibit certain compiler
optimizations, at least according to one of my references.

The main problem is that since a goto can branch either direction in a
function, it's possible to write code that doesn't "read" in a linear
fashion, making the code difficult to debug by inspection. It can also
lead to code that's "brittle", or difficult to modify. I saw one
abomination that had 13 or so gotos branching in random directions; it
literally took weeks to puzzle out the flow of control (granted, this
code had other problems, but the gotos made a bad situation
impossible).

There are times when a goto is useful, such as for breaking out of a
deeply nested loop, but more often than not it's better to stick with
the standard control structures (if, while, for, do) and unconditional
branch statements (break and continue).

Nov 13 '06 #22

P: n/a
ma****@gmail.com wrote:
I keep hearing about not to use 'go_to' in your code. However, I don't
know what's the reasoning behind it?

Can any one here shed some light?
See C FAQ 17.10

--
Tor <torust AT online DOT no>

Nov 13 '06 #23

P: n/a

Walter Roberson wrote:
In article <11**********************@i42g2000cwa.googlegroups .com>,
Ed Prochak <ed*******@gmail.comwrote:
However even Wirth did not propose complete elimination of GOTO. Wirth
created PASCAL and still included GOTO. His chief use of GOTO was error
handling. The goal of the Structured Programming movement is often
mistakenly described as seaking GOTOless programming. Such a
description is wrong. It was the UNRESTRAINED use of goto that they
sought to eliminate.

Your analysis disagrees with Wirth's own published works.

http://www.swissdelphicenter.ch/en/niklauswirth.php

Yet, Pascal also suffered from certain deficiencies, more or
less significant depending on personal perception and
application. [...]

Certain other deficiencies were due to the author's lack of
courage to throw some rules inherited from Algol over board, in
fear of antagonizing influential Algol programmers. The prime
entry in this list is the famed go to statement, retained
although, in principle, always replaceable by an if, while, or
repeat construct
--
Prototypes are supertypes of their clones. -- maplesoft
Interesting. I alway thought he was not of that radical camp. I based
my judgement on his textbook: Algorithms+DataStructures=Programs
I remember at least one of the programs in the book used goto. And
while goto is "in principle, always replaceable by an if, while, or
repeat construct", without an error handling mechanism (like EXCEPTION)
in the language, goto can be very useful. Otherwise the nesting of
error checking IF statements gets tedious and nearly as error prone as
the goto.

Ed

Nov 14 '06 #24

This discussion thread is closed

Replies have been disabled for this discussion.