www.digitalmars.com         C & C++   DMDScript  

D - goto mess

reply Ant <Ant_member pathlink.com> writes:
From a program posted here I could see something like:

if (IAmSmart)
{
  goto itIsTrueIAmSmart
}
else
{
  itIsTrueIAmSmart:
  printf("I am stupid!\n);
}

shouldn't that goto (at least) be illegal?
is it supported like that on the languages we are trying
to keep some compatibility?

Ok, D supports multiple paradigmas,
one of which is the stupid paradigma.
can we revert that?

As you might know java doesn't have goto.
It didn't keep it from becaming popular.

Ant
Dec 02 2003
next sibling parent reply Berin Loritsch <bloritsch d-haven.org> writes:
Ant wrote:

 From a program posted here I could see something like:
 
 if (IAmSmart)
 {
   goto itIsTrueIAmSmart
 }
 else
 {
   itIsTrueIAmSmart:
   printf("I am stupid!\n);
 }
 
 shouldn't that goto (at least) be illegal?
 is it supported like that on the languages we are trying
 to keep some compatibility?
 
 Ok, D supports multiple paradigmas,
 one of which is the stupid paradigma.
 can we revert that?
 
 As you might know java doesn't have goto.
 It didn't keep it from becaming popular.

To be clearer, Java has a bytecode level "goto", but the semantics of enabling proper goto controls made it so that it was only really safe for the compiler to do. Think about this: while ( keepGoing ) { if ( foo() > someBigSize ) goto notSmart; stack.push( bar() ); } notSmart: baz(stack); what about my stack? Using goto instead of break is messy and not good at all. The supplied example of using goto to circumvent conditional processing is also bad. A worse construct is something like this: class MyClass { int foo() { goto barMeth; return 0; } object bar() { barMeth: printf("Where was this called?"); goto bazMeth; return new MyClass(); } void baz(Stack stack) { if (stack == NULL) throw new IllegalStateException(); bazMeth: // can we still trust the stack variable??? foreach (stack; object obj = stack.pop()) { fprintf("%o", obj); } return; } } Each of these represents bad uses of goto. If we can guarantee that a goto is limited to be *inside* of one function, and it *cannot* override conditional logic, then not only do we remove 90% of the program control problems, we also remove many reasons to have a goto in the first place. If we are talking about exiting a loop early, a break will work. If we are talking about exiting an if/else clause early to the larger method context, then maybe you are doing something that will be difficult to maintain later. The general rule of thumb I use is if you really think you need a goto, its time to rethink the problem. They can introduce more harm than good. The only time that they are really useful is when we are close to the metal. For instance, in byte code generation (or machine code generation) we have the equivalent of a compare/branch type of function. Same thing with the exception handling. Under the hood, the compiler might create something like this: compare( foo() > someBigSize ) bne elseClause // if clause work goto conditionalEnd elseClause: // else clause work goto conditionalEnd conditionalEnd: // other stuff Or with exception handling try // different bytecodes for this //do work goto finallyClause catch // different bytecodes for this // do catch work goto finallyClause finallyClause: // do finally work This is all close the the metal type of stuff, where there are no high level constructs to obviate the need for the goto. It is hard for me to imagine a compelling language that has not gotten rid of the need for goto.
Dec 02 2003
parent reply Ant <Ant_member pathlink.com> writes:
In article <bqihkj$2dp4$1 digitaldaemon.com>, Berin Loritsch says...
To be clearer, Java has a bytecode level "goto",

Java in the strick meaning: I was referening to the language.
what about my stack?  Using goto instead of break is messy and
not good at all.

I don't like break either...
A worse construct is something like this:

class MyClass {
     int foo() {
         goto barMeth;

         return 0;
     }

     object bar() {
         barMeth:
         printf("Where was this called?");

iykes! is that legal? did you actually compiled it? if so it *MUST* be removed.
Each of these represents bad uses of goto.  If we can guarantee that
a goto is limited to be *inside* of one function, and it *cannot* override
conditional logic, then not only do we remove 90% of the program control
problems, we also remove many reasons to have a goto in the first place.

true.
If we are talking about exiting a loop early,

As I don't use break there is no such thing. what you call early I just call one of the condition of the loop. If it's too complex perheaps it's again time to rethink the problem (I mean the solution).
The general rule of thumb I use is if you really think you need a goto, its
time to rethink the problem.

very true.
The only time that they are really useful is when we are close to the metal.

Let's not forget that each program branch is realy a conditional goto. The goto in high level languages is called "if" or "while" or... Ant
Dec 02 2003
parent Berin Loritsch <bloritsch d-haven.org> writes:
Ant wrote:

 
A worse construct is something like this:

class MyClass {
    int foo() {
        goto barMeth;

        return 0;
    }

    object bar() {
        barMeth:
        printf("Where was this called?");

iykes! is that legal? did you actually compiled it? if so it *MUST* be removed.

Luckily no. The result I got was $ /dmd/bin/dmd test1.d Error: undefined label barMeth So the label must at least be in the method.
Dec 02 2003
prev sibling next sibling parent reply "Vathix" <vathix dprogramming.com> writes:
"Ant" <Ant_member pathlink.com> wrote in message
news:bqieof$29i2$1 digitaldaemon.com...
 From a program posted here I could see something like:

 if (IAmSmart)
 {
   goto itIsTrueIAmSmart
 }
 else
 {
   itIsTrueIAmSmart:
   printf("I am stupid!\n);
 }

 shouldn't that goto (at least) be illegal?
 is it supported like that on the languages we are trying
 to keep some compatibility?

 Ok, D supports multiple paradigmas,
 one of which is the stupid paradigma.
 can we revert that?

 As you might know java doesn't have goto.
 It didn't keep it from becaming popular.

 Ant

You're probably referring to my code. I noticed that after I posted it here; I realized I didn't need a goto there, could have been: if(!smart) makesmart(); imsmart(); But was it any harder to understand?
Dec 02 2003
parent Ant <Ant_member pathlink.com> writes:
In article <bqis4n$2su3$1 digitaldaemon.com>, Vathix says...
"Ant" <Ant_member pathlink.com> wrote in message
news:bqieof$29i2$1 digitaldaemon.com...
 From a program posted here I could see something like:

 if (IAmSmart)
 {
   goto itIsTrueIAmSmart
 }
 else
 {
   itIsTrueIAmSmart:
   printf("I am stupid!\n);
 }

 shouldn't that goto (at least) be illegal?
 is it supported like that on the languages we are trying
 to keep some compatibility?

 Ok, D supports multiple paradigmas,
 one of which is the stupid paradigma.
 can we revert that?

 As you might know java doesn't have goto.
 It didn't keep it from becaming popular.

 Ant

You're probably referring to my code. I noticed that after I posted it here; I realized I didn't need a goto there, could have been: if(!smart) makesmart(); imsmart();

Yes! see: you where just being lazy and supported by a language that allows you to be lazy. I'm glad you figure that out. no bruised feelings I hope.
But was it any harder to understand?

just a bit, but now image instead of 4 lines of code you had 25... and instead of one program with 1000 lines you had 50 programs with 50000 lines... When dealing with a short program that you code in a couple of days you can get throught with anything. If you have to change the program two weeks later you're doomed :) and BTW thank you for the utilities you posted! I'm still thinking of integrating your string class into my D projects. (maybe I'll remove the goto first, just kiding, just kiding :) Ant
Dec 02 2003
prev sibling parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Oh come on!

Everyone has read the "considered harmful" paper, and i suppose there is 
much more angst on goto than problems. What the paper referred to, was 
unstructured BASIC, where there was *no* elegant way of handling many 
situations, so goto was the only choice. You had the code with a goto 
every 3 lines or so. I also have some experience of this from my 
ZX-Spectrum Era, which was when i was 8, 13 years ago.

When getting back to  coding, i was so frightened of using goto back a 
few years ago, that i made spaghetty code with some thought-up 
conditions, which could be cut down by half by a single goto!

We have talked this to death. If you are so opposed to goto, don't use it.

BTW, i believe JVM supports goto. And the language has it reserved for 
future use, together with assert. :>>>>>>>>>>>>>>>

-eye


Ant wrote:
 From a program posted here I could see something like:
 
 if (IAmSmart)
 {
   goto itIsTrueIAmSmart
 }
 else
 {
   itIsTrueIAmSmart:
   printf("I am stupid!\n);
 }
 
 shouldn't that goto (at least) be illegal?
 is it supported like that on the languages we are trying
 to keep some compatibility?
 
 Ok, D supports multiple paradigmas,
 one of which is the stupid paradigma.
 can we revert that?
 
 As you might know java doesn't have goto.
 It didn't keep it from becaming popular.
 
 Ant
 
 

Dec 03 2003
next sibling parent reply Ant <Ant_member pathlink.com> writes:
goto begin
sign:
Ant
goto exit:
warning:
I sent the guy a PM but he never replyed
got sign
begin:
gosub greating:
(easy joke)
I had to say something because goto right:
so the C++ guy started crying
right:
the posted code was realy bad
goto good:
d rules
greating:
Hi all,
return
good:
and people was saying good things about it.
goto warning:
exit:
Dec 03 2003
parent Ilya Minkov <minkov cs.tum.edu> writes:
LOL ROTFL!!!!!

:D

Ant wrote:
 goto begin
 sign:
 Ant
 goto exit:
 warning:
 I sent the guy a PM but he never replyed
 got sign
 begin:
 gosub greating:
 (easy joke)
 I had to say something because goto right:
 so the C++ guy started crying
 right:
 the posted code was realy bad
 goto good:
 d rules
 greating:
 Hi all,
 return
 good:
 and people was saying good things about it.
 goto warning:
 exit:
 
 

Dec 03 2003
prev sibling next sibling parent Berin Loritsch <bloritsch d-haven.org> writes:
Ilya Minkov wrote:

 BTW, i believe JVM supports goto. And the language has it reserved for 
 future use, together with assert. :>>>>>>>>>>>>>>>

The JVM only uses goto in the sense of the actual bytecode. It is not, nor ever will be, a usable keyword in Java. Assert OTOH is a keyword that is enabled.
Dec 03 2003
prev sibling parent reply "Lars Ivar Igesund" <larsivar igesund.net> writes:
"Ilya Minkov" <minkov cs.tum.edu> wrote in message
news:bqlfme$k7k$1 digitaldaemon.com...
 BTW, i believe JVM supports goto. And the language has it reserved for
 future use, together with assert. :>>>>>>>>>>>>>>>

No, Java has reserved 'goto' so that it can NEVER be used. Lars Ivar Igesund
Dec 03 2003
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Lars Ivar Igesund wrote:
 No, Java has reserved 'goto' so that it can NEVER be used.

Guys, do i really have to mark jokes explicitly ????
Dec 03 2003
parent "Lars Ivar Igesund" <larsivar igesund.net> writes:
"Ilya Minkov" <minkov cs.tum.edu> wrote in message
news:bqlk2l$q74$2 digitaldaemon.com...
 Lars Ivar Igesund wrote:
 No, Java has reserved 'goto' so that it can NEVER be used.

Guys, do i really have to mark jokes explicitly ????

Sure :) Especially in multicultured environment as this. Lars Ivar Igesund
Dec 03 2003