how to emulate goto.

R

Roland

...




Haha... nope :)
My original point ( and why I took offense to Mr Weidenfeller ) was, that IF
you answer such questions, do it in a polite way, and do NOT patronize
people just for being newbies...
I fully agree.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
R

Roland

Then you should start answering to these questions so that
other people do not need to get all upset about them.
Your answer makes me think that somehow you missed Birkemose's point
(which I happen to agree with). Let me try to say it in my words. If you
choose to reply to a question which you think is inappropriate for this
group (or any other group) for whatever reason, state this reason in a
non-patronizing way. And if you have been proven wrong, have the
courtesy to acknowledge it. IMHO, that's what netiquette is about.

If people get all upset by questions that have been posted before, or
for which this group isn't appropriate, then, IMO, /they/ have a
*serious* problem. I would appreciate it if they refrain from answering
these questions.

Whenever I feel that a question is better to be answered in another
group, I will direct the poster to it.
--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
B

Birkemose

...
Then you should start answering to these questions so that
other people do not need to get all upset about them.

You get upset about newbies asking ( in your opinion ) stupid questions, but
if someone answers them politely, you DON'T get upset ?
Erhmmm... I simply do not follow you logic here ?
 
S

steve

Frankly, let me be a bit more blunt than most of the (good) replies you
have had showing various means of programming without gotos....

You don't want to emulate gotos, You dont want to use gotos even in
languages that do support them.

Its been known for a very long time, that the goto is a crap mechanism,
used usually at the wrong time, by the less experienced developers.

If you were programming in a procedural language then I could just about
understand why you may think about using them (regardless of the fact
that I'd still give you the same advice as above), yet you are
developing in an OO language. Gotos have even less right to be used in
a OO language.

A better question you could have asked would be...." I have this code
which uses gotos, how can I rewrite it without gotos?"

you need to take a closer look at java , because it is full of goto's.
you just don't have the experience to be able to see them.
 
T

Thomas Schodt

Steve said:
Actually, I thought you had as well

I think it happened to many.
The question was asked only a day or two
ago, with almost identical wording.

What's more, if you follow the link I gave earlier in the thread
you will find that the knobhead
that Mr. Agarwal was unfortunate enough to get mistaken for
had posted the question to no less than 10 different Java forums
just one day earlier.

Makes you wonder if this is a new kind of trolling...
 
A

Andrew McDonagh

steve said:
you need to take a closer look at java , because it is full of goto's.
you just don't have the experience to be able to see them.

Could you enlighten me then?

Seeing as even the 'continue' and 'break' are not actually gotos in the
sense that gotos are assembler, etc. they just happen to use the same
construct of jumping to a labeled position.

There is no jumping occurring, its just a way of terminating an iteration.
 
A

Andrew McDonagh

Dimitri said:
Andrew McDonagh sez:




Frankly, you haven't a clue (blunt, eh?).

Blunt but not accurate
The problem with goto
is that 40 year ago (that's 500BC in computer years) it was the
only means to reuse code, and the resulting code was horrible.
So they invented subroutines and started preaching "goto bad,
subroutines good" mantra.

Today nobody's ever seen a language without subroutines, so the
"subroutines good" part got forgotten and nobody's ever seen the
original spaghetti code, so they don't remember what the "goto
bad" part was all about.

I'm sure I'm not the only one to have seen code that used gotos, so I
think some people must have. Granted new developers may not have.
Meanwhile, goto changed its name to "continue break label" and
everyone's using it in the happy ever after.

yes, but 'continue break label' is not really a goto. It looks like one,
but it isn't.
 
T

Tor Iver Wilhelmsen

Andrew McDonagh said:
Seeing as even the 'continue' and 'break' are not actually gotos in
the sense that gotos are assembler, etc. they just happen to use the
same construct of jumping to a labeled position.

There is no jumping occurring, its just a way of terminating an iteration.

Yes it is: All those flow control structures in Java are turned into
jump instructions in the VM: Consequently, it is a form of "readable
goto".

C# supports goto, making it easier to write ugly code in that
language.
 
O

opalpa

Hello Gaurav, I recognize the C goto pattern from several projects
where I've utilized robotic devices connected to a computer. Robotic
devices can go into an error state at any time/or better after any
function call and I frequently would have many an if statement followed
by goto (I would usually macro this logic) to the end of a function.

I've also recoded two such tasks in Java and in this language I've
found it better to let the errors propogagte outside the function, that
is declare foo to to throw Exception instances or maybe even Error
instances.
 
D

Dimitri Maziuk

Andrew McDonagh sez:
Dimitri Maziuk wrote: ....

yes, but 'continue break label' is not really a goto. It looks like one,
but it isn't.

Riight. Looks like a duck, quacks like a duck (executes a jump
to labelled instruction), must be a Belgian paratrooper.

I've seen enough crappy code without gotos and good code with
gotos to know that the real problem is "programmers" who
shouldn't be allowed within a nucular blast radius of a computer.

Dima
 
A

Andrew McDonagh

Dimitri said:
Andrew McDonagh sez:



Riight. Looks like a duck, quacks like a duck (executes a jump
to labelled instruction), must be a Belgian paratrooper.

take a look at the Thinking in Java book by Bruce Eckel

http://www.faqs.org/docs/think_java/TIJ305.htm#Heading3009
I've seen enough crappy code without gotos and good code with
gotos to know that the real problem is "programmers" who
shouldn't be allowed within a nucular blast radius of a computer.

On this I completely agree. Most problems are down to inexperienced
developers rather than poor tools.

Having said that, if the tool makers find that certain things can be
improved to make them less likely to be abused by these developers, then
it may be worth it. This is the case with Java's break & continue, in
that they are 'like' a goto, but have tight constraints upon how and
when they can be used.
 
D

Dimitri Maziuk

Andrew McDonagh sez:
....
Having said that, if the tool makers find that certain things can be
improved to make them less likely to be abused by these developers, then
it may be worth it. This is the case with Java's break & continue, in
that they are 'like' a goto, but have tight constraints upon how and
when they can be used.

FSVO 'like'.

Break label, specifically, is limited in that it can transfer
control to enclosing statement only. Which in practice means
you have to get creative with curly braces to use it as a
plain goto.

Continue is more restricted, it requires a loop.

Dima
 
A

Andrew Thompson

...


You get upset about newbies asking ( in your opinion ) stupid questions, but
if someone answers them politely, you DON'T get upset ?
Erhmmm... I simply do not follow you logic here ?

Perhaps if you had answered the next '100 c.l.j.help is thataway'
questions you might have understood. I see you ducked out on that.
 
S

steve

Could you enlighten me then?

Seeing as even the 'continue' and 'break' are not actually gotos in the
sense that gotos are assembler, etc. they just happen to use the same
construct of jumping to a labeled position.

There is no jumping occurring, its just a way of terminating an iteration.

take a look at the byte code
 
D

Dimitri Maziuk

(e-mail address removed) sez:
....
....
How many people have told him to use exceptions?

What they should've told him is "use /dev/brain" (aka "get a clue").
That code can work perfectly well without exception overhead:

boolean foo() {
do_stuff();
if( error ) return false;
do_more_stuff();
if( error ) return false;
...
}
If you /must/ use gotos, indeed, go fetch Objectweb ASM and have at it.

If you must use gotos:

void foo() {
exit: {
do_stuff();
if( error ) break exit;
do_more_stuff();
if( error ) break exit;
...
}
return some value;
}


Dima
 
A

alan

(e-mail address removed) sez:
...


If you must use gotos:

void foo() {
exit: {
do_stuff();
if( error ) break exit;
do_more_stuff();
if( error ) break exit;
...
}
return some value;
}

Cute. Learn something new every day.
 
C

Chris Smith

Dimitri Maziuk said:
If you must use gotos:

void foo() {
exit: {
do_stuff();
if( error ) break exit;
do_more_stuff();
if( error ) break exit;
...
}
return some value;
}

Obviously, that's not the same as goto. It's labeled break. Labeled
break (and continue) differ from goto in one important way, which
eliminates the bulk of really serious goto problems. They can only be
used to exit a statement, and never to enter one. That is, the
following does NOT work:


break someLabel;

{
prepare();
someLabel: doStuff();
}

As for whether labeled break and continue are still a good idea, I doubt
it. I have really found myself using them only in one situation:
namely, when I'm playing around at www.topcoder.com, and the important
thing is whether it takes four minutes or six minutes to write some
piece of code. Then labeled break and continue are quite useful.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
D

Dimitri Maziuk

Chris Smith sez:
....
As for whether labeled break and continue are still a good idea, I doubt
it. I have really found myself using them only in one situation:
namely, when I'm playing around at www.topcoder.com, and the important
thing is whether it takes four minutes or six minutes to write some
piece of code. Then labeled break and continue are quite useful.

About the only time you need a goto in a structured high-level
language is when structured constructs don't do what you need.
Most common by far is a loop with condition in the middle --
structured programming offers only loop with a counter and
pre- and post-condition loops. Hence break and continue.

Labelled versions allow breaking out of several levels of
nested loops. That does come up occasionally, but not nearly
as often as breaking out of innermost loop. I almost never
use it, either, but off the top of my head:
out: for( row=0; row<a.length; row++ )
for( col=0; col<a[row].length; col++ )
if( a[row][col] == search_value )
break out;

As for the other usage of Java labelled break (quite
different from breaking out of a nested loop):
end: {
...
break end;
}
somestatement;
and
...
goto end;
end: somestatement;

both jump to somestatement. The former version limits the
mess you can create (makes it harder to jump back and forth
all over the place), that's all. Still a goto, only castrated
and chained to a pair of curly braces.

Dima
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top