Is it important to know about GOTOs?

  • (10 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »

143 Replies - 6657 Views - Last Post: 13 October 2012 - 01:05 PM

#16 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,433
  • Joined: 29-May 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 08:30 AM

View PostG0rman, on 11 October 2012 - 04:03 PM, said:

Multiple return statements are the fruit of the devil after all!
They are right up there with globals!


No they ain't.
They are useful for parameter checks (sentinels) at the state of your methods.
Also in the area of parameter checks is Code Contracts, which is use alongside automated testing frameworks.

Also useful in Case Select statements.

In vb you don't even need the return statement to return the result, and I'm not talking about ByRef parameters. Start your head scratching now.
Spoiler

This post has been edited by AdamSpeight2008: 11 October 2012 - 08:31 AM

Was This Post Helpful? 0
  • +
  • -

#17 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 08:34 AM

View PostG0rman, on 11 October 2012 - 10:03 AM, said:

I've not heard of a language that doesn't have GOTOs (those I assume they do exist), and up until the late 70s there was no real opposition against them.


No modern language has gotos that I know of. Goto is a keyword in many languages, but it's generally not implemented.

Quote

Nice work, but aren't you substituting one academic phobia for another? Multiple return statements are the fruit of the devil after all!
They are right up there with globals!


There are some people who object to multiple returns, but this seems like a piece of dogma to me.
Globals are closer to gotos in my mind. Compartmentalize, encapsulate, restrict access - these are tools that we've found lead to better code with fewer bugs. The fewer interactions you have to understand, the fewer bugs you can let through. At any point, if you can restrict the interactions of your code or your data, you reduce the number of bugs, all other things being equal.
Was This Post Helpful? 1
  • +
  • -

#18 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,433
  • Joined: 29-May 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 08:37 AM

C# has GoTo and is fully implemented.
Though it is restrained to be method local.

This post has been edited by AdamSpeight2008: 11 October 2012 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

#19 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6048
  • View blog
  • Posts: 23,473
  • Joined: 23-August 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 08:39 AM

There are few absolutes in programming, more a set of "avoid if at all possible"s. Like globals, gotos, premature optimization, and reliance on undefined behavior.
Was This Post Helpful? 1
  • +
  • -

#20 G0rman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 16-October 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 08:41 AM

View Postjon.kiparsky, on 11 October 2012 - 11:34 PM, said:

No modern language has gotos that I know of. Goto is a keyword in many languages, but it's generally not implemented.

Hm, not too sure about that.
Let's go through the list of programming subforums:
C and C++ yes
VB yes
Java yes
VB.NET yes
C# yes
ASP.NET no idea
PHP yes
Ruby yes
Python NO!!!
ColdFusion no idea
SQL yes
Other languages? Perl yes, assmebly yes, fortran yes, ada yes, COBOL not sure, basic yes

So far the only no is Python.

View PostAdamSpeight2008, on 11 October 2012 - 11:30 PM, said:

In vb you don't even need the return statement to return the result, and I'm not talking about ByRef parameters. Start your head scratching now.
Spoiler

That's a pretty interesting piece of code. A lot of scripting languages work like that, but didn't know VB could do it!

This post has been edited by G0rman: 11 October 2012 - 08:44 AM

Was This Post Helpful? 1
  • +
  • -

#21 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 08:58 AM

There's no goto in java. It's a keyword, but it's not implemented.
I just checked the documentation, and I see that perl has a goto, but I've literally never heard of it before, and I've been using perl for close to twenty years.
C and basic are hardly modern languages.

I don't do Ruby, but from a quick look it doesn't look like the goto is part of the language there. It's not in the list of control flow operators, and it's not even a reserved word. I see that someone's implemented it, but it looks more like it's revealing a bug in the language than for actual use.

PHP's goto is pretty limited - looks like it's mostly a glorified break instruction.

Some other languages:

Lisp, scheme - no. (not exactly modern, I admit)
Scala - no.
Clojure - no.
Was This Post Helpful? 0
  • +
  • -

#22 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,433
  • Joined: 29-May 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:03 AM

View PostG0rman, on 11 October 2012 - 04:03 PM, said:

How should we deal with structures that cannot be dealt with cleanly by if,for,while,switch,sub()?


I'd be interested in seeing those structures. Can you provide examples?
Was This Post Helpful? 1
  • +
  • -

#23 G0rman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 16-October 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:06 AM

Addimitedly in Ruby you need to recompile RUBY with #define SUPPORT_JOKE 1
:P

Java's break is really just a restrained goto.

So looks like it's probably 50/50 with regards to modern languages? Though the trend certainly is towards removal.

This post has been edited by G0rman: 11 October 2012 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#24 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7621
  • View blog
  • Posts: 12,849
  • Joined: 19-March 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:12 AM

View PostG0rman, on 11 October 2012 - 11:06 AM, said:

Addimitedly in Ruby you need to recompile RUBY with #define SUPPORT_JOKE 1
:P

Java's break is really just a restrained goto.

So looks like it's probably 50/50 with regards to modern languages? Though the trend certainly is towards removal.



Java's break is a break. It's definitively not a goto - what makes a goto a goto, and what makes it dangerous, is the unrestricted transfer of flow to an arbitrary location. A function call is not a goto, because it always returns to the point of call. A loop is not a goto because it can only terminate where it began. Returning from either of these is not a goto because they return you to the existing flow of control.

The fact that something generates a jump instruction in the underlying (virtual) machine instructions does not imply that it is equivalent to a goto!
Was This Post Helpful? 1
  • +
  • -

#25 G0rman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 16-October 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:21 AM

View PostAdamSpeight2008, on 12 October 2012 - 12:03 AM, said:

View PostG0rman, on 11 October 2012 - 04:03 PM, said:

How should we deal with structures that cannot be dealt with cleanly by if,for,while,switch,sub()?


I'd be interested in seeing those structures. Can you provide examples?

The previously discussed nested loop is the most commonly described "acceptable use of GOTO".

The two reasons not to use GOTO are readability and structure.
baavgai's solution was the best so far - and we can of course always just use sub() to break things down to acceptable levels. But there can come a time when you have to have several different conditions and that sub() won't be usable. In that kind of situation you are going to have to introduce an enum or remove the sub and plop in a case - and things just keep getting more messy from there!

a=randomInt();
b=randomInt();
c=randomInt();
for(int i=0;i<10;i++) {
	for(int j=0;j<10;j++) {
		switch(i*j) {
			case a: goto a;
			case b: goto b;
			case c: goto c;
		}
	}
}
printf(“didn’t find %d!”,k);
goto end;
a: ;
	printf("Found A!");
	goto end;
b: ;
	printf("Found B!");
	goto end;
c: ;
	printf("Found C!");
	goto end;
end: ;


You can see how the switch statement is inside the loop and outside it too. You could of course move the printf's inside the switch then break, but I chose this simpler example instead of thinking up something really convoluted that would be "impossible" to resolve to a conditional or loop.

There comes a stage where you are making so many conditionals and loops to emulate gotos that is just doesn't make much sense. The point of conditionals and loops are to make things esaier to read and maintain structure. If you are loosing readability then I don't see a problem making the switch. So long as you have discipline you won't cause any problems - and as previously pointed out some languages restrict what you can and can't do with gotos anyway.

View Postjon.kiparsky, on 12 October 2012 - 12:12 AM, said:

ava's break is a break. It's definitively not a goto - what makes a goto a goto, and what makes it dangerous, is the unrestricted transfer of flow to an arbitrary location. A function call is not a goto, because it always returns to the point of call. A loop is not a goto because it can only terminate where it began. Returning from either of these is not a goto because they return you to the existing flow of control.

Well it is a named break, and seeing as the main "accepted use of gotos" is what Java's break is made to do, it is very safe to call it "basically a goto".

It seems like you imply that the role of a language is to "enforce discipline on a programmer". It's not like I go around not indenting in Java because I don't have to. But on the other hand I don't love Python just because it forces me to indent.

This post has been edited by G0rman: 11 October 2012 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#26 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,433
  • Joined: 29-May 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:22 AM

View Postjon.kiparsky, on 11 October 2012 - 05:12 PM, said:

A function call is not a goto, because it always returns to the point of call.


That be a GoSub.

What about ComeFrom?
Was This Post Helpful? 1
  • +
  • -

#27 Curtis Rutland  Icon User is online

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 4451
  • View blog
  • Posts: 7,751
  • Joined: 08-June 10

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:25 AM

View PostG0rman, on 11 October 2012 - 10:41 AM, said:

Let's go through the list of programming subforums:
C and C++ yes
VB yes
Java yes
VB.NET yes
C# yes
ASP.NET no idea
PHP yes
Ruby yes
Python NO!!!
ColdFusion no idea
SQL yes
Other languages? Perl yes, assmebly yes, fortran yes, ada yes, COBOL not sure, basic yes


Misleading. For instance, goto is a keyword in Java, but is not actually implemented. ASP.NET isn't a language, so I don't even know why you included it there. It uses VB.NET or C# as its guts. SQL? That's a very different paradigm, and it's also not what I'd call a modern language. Assembly? Well, no shit. But it's neither high level or modern.

Also, a break is not the same as a goto. Just because something is rendered as a jump in machine language does not mean it's the same as a goto. We use higher level languages because they obfuscate the lower level constructs for us; otherwise we'd all still be using assembly for everything.

Quote

I don't know why anyone was writting VB in 2000


VB was the shit in 2000. .NET didn't even launch until early 2002. VB was a mature, relatively stable platform that was easy to pick up and get stuff done with. Sure, we've come a long way in the past 12 years, but I think you're misremembering if you think VB classic was this horrible thing that people already shunned in 2000.

View PostG0rman, on 11 October 2012 - 11:21 AM, said:

View PostAdamSpeight2008, on 12 October 2012 - 12:03 AM, said:

View PostG0rman, on 11 October 2012 - 04:03 PM, said:

How should we deal with structures that cannot be dealt with cleanly by if,for,while,switch,sub()?


I'd be interested in seeing those structures. Can you provide examples?

The previously discussed nested loop is the most commonly described "acceptable use of GOTO".

The two reasons not to use GOTO are readability and structure.
baavgai's solution was the best so far - and we can of course always just use sub() to break things down to acceptable levels. But there can come a time when you have to have several different conditions and that sub() won't be usable. In that kind of situation you are going to have to introduce an enum or remove the sub and plop in a case - and things just keep getting more messy from there!

a=randomInt();
b=randomInt();
c=randomInt();
for(int i=0;i<10;i++) {
	for(int j=0;j<10;j++) {
		switch(i*j) {
			case a: goto a;
			case b: goto b;
			case c: goto c;
		}
	}
}
printf(“didn’t find %d!”,k);
goto end;
a: ;
	printf("Found A!");
	goto end;
b: ;
	printf("Found B!");
	goto end;
c: ;
	printf("Found C!");
	goto end;
end: ;


You can see how the switch statement is inside the loop and outside it too. You could of course move the printf's inside the switch then break, but I chose this simpler example instead of thinking up something really convoluted that would be "impossible" to resolve to a conditional or loop.

There comes a stage where you are making so many conditionals and loops to emulate gotos that is just doesn't make much sense. The point of conditionals and loops are to make things esaier to read and maintain structure. If you are loosing readability then I don't see a problem making the switch. So long as you have discipline you won't cause any problems - and as previously pointed out some languages restrict what you can and can't do with gotos anyway.


That's a bullshit example, because you could completely replace your gotos with subs/methods without any difficulty or confusion at all.
Was This Post Helpful? 1
  • +
  • -

#28 G0rman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 16-October 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:28 AM

View PostAdamSpeight2008, on 12 October 2012 - 12:22 AM, said:

View Postjon.kiparsky, on 11 October 2012 - 05:12 PM, said:

A function call is not a goto, because it always returns to the point of call.


That be a GoSub.

What about ComeFrom?

Isn't the convention to store your dynamic link in a global stack?

bar() {
dynamicStack.push(&&FuncName_LabelXYZ);
goto Foo_Label33;
FunName_LabelXYZ: ;
}

foo() {
Foo_label133;
DoSomething();
goto dynamicStack.pop();
}


This post has been edited by G0rman: 11 October 2012 - 09:29 AM

Was This Post Helpful? 0
  • +
  • -

#29 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,433
  • Joined: 29-May 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:30 AM

GoTo was also use a lot for error handling.

Sub Example()
  On Error GoTo error_handler
  REM -- Skip over error handling code to exit --
  GoTo end_of_method

error_handler:
  If ERR = {some error code} Then
    {code}
    Rem -- Not Fatal error.  --
    Resume
  Else
    {code}
  End If
end_of_method:
End Sub 



Was This Post Helpful? 0
  • +
  • -

#30 G0rman  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 16-October 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 09:33 AM

View PostCurtis Rutland, on 12 October 2012 - 12:25 AM, said:

Misleading. For instance, goto is a keyword in Java, but is not actually implemented. ASP.NET isn't a language, so I don't even know why you included it there. It uses VB.NET or C# as its guts. SQL? That's a very different paradigm, and it's also not what I'd call a modern language. Assembly? Well, no shit. But it's neither high level or modern.

So in other words
"No one should learn GOTO because it isn't used in some modern languages and in particular paradigms and because it is evil"?

View PostCurtis Rutland, on 12 October 2012 - 12:25 AM, said:

I think you're misremembering if you think VB classic was this horrible thing that people already shunned in 2000.

I wasn't working back then, so I have nothing to remember.
You can slate it down to me just not liking VB.

View PostCurtis Rutland, on 12 October 2012 - 12:25 AM, said:

That's a bullshit example, because you could completely replace your gotos with subs/methods without any difficulty or confusion at all.

Of course. I said that didn't I? It's hard to think of an example on the spot.

View PostAdamSpeight2008, on 12 October 2012 - 12:30 AM, said:

GoTo was also use a lot for error handling.

Sub Example()
  On Error GoTo error_handler
  REM -- Skip over error handling code to exit --
  GoTo end_of_method

error_handler:
  If ERR = {some error code} Then
    {code}
    Rem -- Not Fatal error.  --
    Resume
  Else
    {code}
  End If
end_of_method:
End Sub 



Aren't you glad we have clumsy "conventional" error handling instead of DIY?

This post has been edited by G0rman: 11 October 2012 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

  • (10 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • Last »