Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: on goto

Reply
Thread Tools

Re: on goto

 
 
Seebs
Guest
Posts: n/a
 
      04-24-2010
On 2010-04-24, Daniel T. <(E-Mail Removed)> wrote:
> I'll give you a reason to ban goto. This is a line of BASIC code:
>
> 90 x = 5
>
> Now, I ask you, where is the line that was previously executed? Is it
> directly above line 90, is it directly below it, or could it be anywhere
> in the program? Because of the goto statement, simply because an
> unstructured "goto" is possible in the language, you have to analyze the
> entire program to know the answer. If we ban the use of "goto", we can
> confidently know exactly what line of code comes before line 90.


That's a great reason, but it's got nothing to do with C. In C, only
other code within the same function can have gone to this line, and that
only if there is a label immediately before this line.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      04-24-2010
"Daniel T." <(E-Mail Removed)> writes:

> Ali Karaali <(E-Mail Removed)> wrote:
>
>> I use goto to break nested for loops and I can't see a
>> reason to ban goto.

>
> I'll give you a reason to ban goto. This is a line of BASIC code:
>
> 90 x = 5
>
> Now, I ask you, where is the line that was previously executed? Is it
> directly above line 90, is it directly below it, or could it be anywhere
> in the program? Because of the goto statement, simply because an
> unstructured "goto" is possible in the language, you have to analyze the
> entire program to know the answer. If we ban the use of "goto", we can
> confidently know exactly what line of code comes before line 90.


You have to EXECUTE the entire program before knowing where you came
from, thanks to computed GOTOs

--
__Pascal Bourguignon__
 
Reply With Quote
 
 
 
 
Tim Streater
Guest
Posts: n/a
 
      04-24-2010
In article <(E-Mail Removed)-september.org>,
Richard <(E-Mail Removed)> wrote:

> Seebs <(E-Mail Removed)> writes:
>
> > On 2010-04-24, Daniel T. <(E-Mail Removed)> wrote:
> >> I'll give you a reason to ban goto. This is a line of BASIC code:
> >>
> >> 90 x = 5
> >>
> >> Now, I ask you, where is the line that was previously executed? Is it
> >> directly above line 90, is it directly below it, or could it be anywhere
> >> in the program? Because of the goto statement, simply because an
> >> unstructured "goto" is possible in the language, you have to analyze the
> >> entire program to know the answer. If we ban the use of "goto", we can
> >> confidently know exactly what line of code comes before line 90.

> >
> > That's a great reason, but it's got nothing to do with C. In C, only

>
> It's a ridiculous reason. The search for the goto LABEL is trivial and a
> goto is often a LOT more obvious that some convoluted condition based
> branch using lots of nested brackets.
>
> > other code within the same function can have gone to this line, and that
> > only if there is a label immediately before this line.
> >
> > -s

>
> There is nothing wrong with Gotos when used in a controlled and
> intelligent manner. Like the bullshit about single return points the
> idea that gotos should not be used is generally just from 3rd rate
> students who think it proves they program "structured" ....


Like Spinny, you mean, with his "structured switch" twaddle.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      04-24-2010
Tim Streater <(E-Mail Removed)> writes:
> In article
> <(E-Mail Removed)>,
> Ali Karaali <(E-Mail Removed)> wrote:
>
>> I use goto to break nested for loops and I can't see a
>> reason to ban goto.
>>
>> Ali
>>
>> On 24 Nisan, 13:13, "io_x" <(E-Mail Removed)> wrote:
>> > [Xpost to: alt.comp.programming, alt.lang.asm,
>> > comp.lang.c, comp.lang.c++, comp.programming]
>> >
>> > the people that speak not good about "goto"
>> > are uttled wrong; their code bug full etc
>> >
>> > more distant is your code from goto
>> > more your code is bug full and incompresible
>> >
>> > the "goto" is the key word for programming;
>> > all the remain, other than goto or jmp or jc or jz,
>> > and the easy cpu layout
>> > is the wrong way for programming
>> >
>> > i advise all you
>> > Good Morning

>
> I haven't used a goto since 1978 (when I stopped writing FORTRAN).


I'm with Knuth. I've reviewed patches and told the submitter to
just convert all his jumble of 'structure' into nice clean goto
statements many times this year. I don't use it for my own code,
but $DAYJOB has a clear coding style that recommends goto in
many situations. If goto has made your function unreadable, it
was probably too big and unreadable anyway.

Phil
--
I find the easiest thing to do is to k/f myself and just troll away
-- David Melville on r.a.s.f1
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-24-2010
On Apr 24, 10:26 pm, Phil Carmody <(E-Mail Removed)>
wrote:
> Tim Streater <(E-Mail Removed)> writes:
> > I haven't used a goto since 1978 (when I stopped writing
> > FORTRAN).


Me neither.

> I'm with Knuth.


In other words, if goto makes the code simpler, it means that
you haven't found the correct expression of the algorithm.

And of course, the problem isn't with goto, per se. It's with
the way it is used. But because it can be abused so badly,
modern languages (including C and C++) provide all of the
necessary structures (and then some) so you don't have to use
it.

> I've reviewed patches and told the submitter to just convert
> all his jumble of 'structure' into nice clean goto statements
> many times this year. I don't use it for my own code, but
> $DAYJOB has a clear coding style that recommends goto in many
> situations. If goto has made your function unreadable, it was
> probably too big and unreadable anyway.


If goto seems necessary, it's because your function is too big
and unreadable.

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-24-2010
On Apr 24, 1:20 pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
> "spinoza1111" <(E-Mail Removed)> wrote in message


> news:(E-Mail Removed)...


> > On Apr 24, 6:06 pm, Ali Karaali <(E-Mail Removed)> wrote:
> >> I use goto to break nested for loops and I can't see a
> >> reason to ban goto.


> > Why not use break instead? Does the same thing, spares you
> > from having to define a label.


> Because break only breaks out of the innermost loop, using
> goto to break out of nested loops is one of the few sensible
> uses of goto.


But when do you have to break out of a nested loop? (And of
course, the only valid use of break in C++ is to end a case in a
switch. Otherwise, it's just a goto in disguise.)

--
James Kanze
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-24-2010
On Apr 24, 5:12 pm, "Daniel T." <(E-Mail Removed)> wrote:
> "Leigh Johnston" <(E-Mail Removed)> wrote:


[...]
> In C and C++, goto is sufficiently restricted that as long as
> your functions are small, it is largely harmless.


In C and C++, if your functions are small enough, goto is
largely harmless. And also useless. All of the examples I've
seen defending goto introduce excessively complex functions in
order to justify it.

--
James Kanze
 
Reply With Quote
 
bartc
Guest
Posts: n/a
 
      04-25-2010
spinoza1111 wrote:
> On Apr 25, 12:27 am, "Daniel T." <(E-Mail Removed)> wrote:
>> "bartc" <(E-Mail Removed)> wrote:
>>> "Daniel T." <(E-Mail Removed)> wrote:
>>>> Ali Karaali <(E-Mail Removed)> wrote:

>>
>>>>> I use goto to break nested for loops and I can't see a reason to
>>>>> ban goto.


>>>> 90 x = 5

>>
>>>> Now, I ask you, where is the line that was previously executed? Is


>>> Goto's at least are usually confined to the same function


>>> Most languages will allow "x=5" inside a function; where is that
>>> function called from?


>> As Dijkstra pointed out in "Go To Statement Considered Harmful"


>> As soon as we include in our language procedures we must admit that
>> a single textual index is no longer sufficient. In the case that a

.....

>> Your comparison between functions and goto has already shown to be
>> unfounded.

>
> Brilliant, couldn't say it any better.


That's a shame, because I didn't understand a word of the Dijkstra extract.

--
Bartc
 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      04-25-2010
On 2010-04-24, James Kanze <(E-Mail Removed)> wrote:
> On Apr 24, 1:20 pm, "Leigh Johnston" <(E-Mail Removed)> wrote:
>> "spinoza1111" <(E-Mail Removed)> wrote in message

>
>> news:(E-Mail Removed)...

>
>> > On Apr 24, 6:06 pm, Ali Karaali <(E-Mail Removed)> wrote:
>> >> I use goto to break nested for loops and I can't see a
>> >> reason to ban goto.

>
>> > Why not use break instead? Does the same thing, spares you
>> > from having to define a label.

>
>> Because break only breaks out of the innermost loop, using
>> goto to break out of nested loops is one of the few sensible
>> uses of goto.

>
> But when do you have to break out of a nested loop? (And of
> course, the only valid use of break in C++ is to end a case in a
> switch. Otherwise, it's just a goto in disguise.)
>


[dropped alt.comp.programming and alt.lang.asm since my
newsserver rejects an egregious number of cross-posts.]

This is just as true in C, according to some people (myself
included). The only exception I can think of is when you are
looping only to set the loop index to something.

ie,

for(i = 0; i < 10; ++i)
if(arr[i].state = READY)
break;
/* now use arr[i], or if i is 10, indicate failure. */

The alternative here, using the loop structure, would be:

for(i = 0; i < 10 && arr[i].state != READY; ++i)
;
/* now use arr[i], or if i is 10, indicate failure. */

To me, the latter option looks horrific, plus if you
swap the operands of &&, you get a very hard-to-spot
overrun.

--
Andrew Poelstra
http://www.wpsoftware.net/andrew

 
Reply With Quote
 
Andrew Poelstra
Guest
Posts: n/a
 
      04-25-2010
On 2010-04-24, James Kanze <(E-Mail Removed)> wrote:
> On Apr 24, 5:12 pm, "Daniel T." <(E-Mail Removed)> wrote:
>> "Leigh Johnston" <(E-Mail Removed)> wrote:

>
> [...]
>> In C and C++, goto is sufficiently restricted that as long as
>> your functions are small, it is largely harmless.

>
> In C and C++, if your functions are small enough, goto is
> largely harmless. And also useless. All of the examples I've
> seen defending goto introduce excessively complex functions in
> order to justify it.
>


[dropped alt.comp.programming and alt.lang.asm since my
newsserver rejects an egregious number of cross-posts.]

I often see code like

if(setup1() == 0)
goto fail;

if(setup2() == 0)
goto fail;

if(setup3() == 0)
goto fail;

/* now do stuff */

return 0;
fail:
/* */
return 1;


Personally, I would do this as:

if(setup1() &&
setup2() &&
setup3()) {
/* now do stuff */
return 0;
}
else {
/* */
return 1;
}

But from a stylistic view, the first version doesn't seem all
that bad. I see it a lot and it doesn't bother me, anyway.

--
Andrew Poelstra
http://www.wpsoftware.net/andrew
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VHDL Goto statement ? Skybuck Flying VHDL 9 08-26-2005 01:46 PM
Re: VHDL Goto statement ? Skybuck Flying VHDL 0 08-08-2005 03:21 AM
where does Console.WriteLine() goto in a web app? Flip ASP .Net 1 04-14-2005 08:01 PM
where does Console.WriteLine() goto? Flip ASP .Net 6 11-18-2004 06:05 PM
goto statement is recommened in systemc? youngsun park VHDL 2 11-18-2003 03:47 PM



Advertisments