Is it important to know about GOTOs?

  • (10 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • Last »

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

#46 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 01:06 PM

View PostBBeck, on 11 October 2012 - 03:40 PM, said:

So, before I get into tearing baavgai's code apart


LOL. Dude! You're taking this way too seriously. In the midst of a GOTO jihad you want to throw in a brace style holy war? You smoke when you pump gas, too?

View PostBBeck, on 11 October 2012 - 03:40 PM, said:

Having the braces on their own line makes it easier to match them up visually.


Completely, utterly, fucking, wrong. Why? Because that's not the way I do it! ;)

I can understand my code at a glance. Yours will take reformatting, recapitalizing, and probably a hazmat suit. I will concede you probably feel the same way about my formatting. This is the nature of opinion. There is no best style, no matter how bloody long your post is.

I basically use the 1TBS (the one true brace style) with minor variants. This is pretty close to Java standard, as well. Entire operating systems have been written like this. I don't know what you're sticking with; what style has first letter caps on variables?!?

As long as it makes sense to you and any other poor buggers that have to read it, it does not matter.
Was This Post Helpful? 0
  • +
  • -

#47 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 01:09 PM

Man, there's less trolling on Deadliest Catch.
Was This Post Helpful? 0
  • +
  • -

#48 BBeck  Icon User is offline

  • Here to help.
  • member icon


Reputation: 581
  • View blog
  • Posts: 1,290
  • Joined: 24-April 12

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 01:25 PM

View Postbaavgai, on 11 October 2012 - 02:06 PM, said:

View PostBBeck, on 11 October 2012 - 03:40 PM, said:

So, before I get into tearing baavgai's code apart


LOL. Dude! You're taking this way too seriously. In the midst of a GOTO jihad you want to throw in a brace style holy war? You smoke when you pump gas, too?


Well, if you're not living on the edge, you're not living, right? ;-)

Quote

View PostBBeck, on 11 October 2012 - 03:40 PM, said:

Having the braces on their own line makes it easier to match them up visually.


Completely, utterly, fucking, wrong. Why? Because that's not the way I do it! ;)

I can understand my code at a glance. Yours will take reformatting, recapitalizing, and probably a hazmat suit. I will concede you probably feel the same way about my formatting. This is the nature of opinion. There is no best style, no matter how bloody long your post is.

I basically use the 1TBS (the one true brace style) with minor variants. This is pretty close to Java standard, as well. Entire operating systems have been written like this. I don't know what you're sticking with; what style has first letter caps on variables?!?

As long as it makes sense to you and any other poor buggers that have to read it, it does not matter.



I have to pretty much agree with you. I definately agree there's a lot of "Well, this is the best way because it's my way." I mean, after all, which ever way I think best is the one I will adopt. Therefore, I'm always going to think that what I'm doing is the "best" way.

So, ultimately, it's really "This is the way I do it. If you think it adds to what you are doing then adopt it. If not, enjoy doing it wrong." ;-) "I mean, feel free to do it the way that works best for you." ;-)

C# also starts off variable names lower case and it "drives me crazy". Certainly, some of that is "just what you're used to". CapitalizingTheBeginningOfEveryWord is Pascal case. (It might be showing that I'm still pretty partial to a lot of Pascal ideas.)

So, I totally agree that questions of style are largely opinions, but it's worth at least considering the logic behind the style of others, even if you ultimately don't care.

But not all of that is entirely a question of style. My primary point was that having multiple exit points from a block of code can make it far more difficult to both understand and debug. It costs maybe a couple extra machine instructions to avoid the multiple exit points, and so it's a trade off between making the code easier to read or making it perform better.

That goes back to the problem of GOTO: unstructured branching all over the code results in a nightmare when debugging. Returning from multiple points in the code is a lesser example of the same thing.

However, there are times where a well placed multiple exit might reduce the number of cpu cycles spent and make the code perform better. So, the best thing is to be well versed in the advantages and disadvantages of the trade off.

:-)

This post has been edited by BBeck: 11 October 2012 - 01:35 PM

Was This Post Helpful? 0
  • +
  • -

#49 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7879
  • View blog
  • Posts: 13,386
  • Joined: 19-March 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 01:32 PM

View Postbaavgai, on 11 October 2012 - 03:06 PM, said:

Completely, utterly, fucking, wrong. Why? Because that's not the way I do it! ;)


baavgai, ours has been a mostly pleasant association until today. We have had pleasant arguments and frightful discussions. We have agreed and disagreed on matters trivial and slightly less trivial. I have considered you a comrade in code - until now.

No more.

From this point forward, it is war to the knives between us.

The Allman brace style shall be all. K&R delenda est.


And while we're at it,

vi roolz, emacs droolz
Was This Post Helpful? 3
  • +
  • -

#50 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,639
  • Joined: 24-September 10

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 02:26 PM

I see a lot of statements about how breaks aren't gotos because a goto allows you to go ANYWHERE. And that this going ANYWHERE is the problem, and as long as there is a restriction, like how breaks are restricted on where they can go, that they become legitimized.

Well in VB.Net and C# there are gotos, and they don't allow you to go anywhere. You must stay with in the scope of the goto declaration (usually the method you're in).

We now have a restriction, I can't 'goto' another class or function... but I can goto the end of the current imperative scope.



Furthermore, if it's the restriction of it that makes it fine... then why can't I design a restriction? Some language A may not restrict the goto, but if I as the programmer decide my design of using goto is restricted to some controllable scenario... then does this absoluteness of evil get reconsidered?



Furthermore what is wrong with understanding the design patterns that come from gotos. Of which there is quite a bit. Once down at the assembly level, which isn't unreasonable in working with, the act that can be described as 'goto' becomes rather useful if not necessary. And now it's not a always evil, it's conditionally evil.

I may not need it when up high in C#, but I might not always be writing C#. I may be writing assembly, or maintaining COBOL from the 70's... and I may need some experience with the designs implemented by people at that time to understand the code. I'm sorry but the 'goto this label' is a gross simplification of it... I maintain old software for a living, and I run into some WEIRD as shit, the very same stuff that makes you scream goto's are bad (in the same respect, they didn't have any better method to accomplish the task at the time). And I need to unravel the oddities of their design to fix and refactor it into .NET. I've only gathered said knowledge and experience over the couple years of reading this slop. OP is wondering if this experience gathering should be considered useful.



And that's what I think the OP is heading towards.

In the case of high-level paradigms like functional and object-oriented, the goto becomes obsolete and a sand-trap for problems. But that is a conditional evil, not an absolute evil.

But in the end, the dogma of 'goto is absolutely evil' has arose to thwart any newbs' idea of even considering looking into new possibilities of goto. We assure you novices... we've checked... it's not of use, move along.

This post has been edited by lordofduct: 11 October 2012 - 02:33 PM

Was This Post Helpful? 2
  • +
  • -

#51 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 03:27 PM

View Postlordofduct, on 11 October 2012 - 10:26 PM, said:

In the case of high-level paradigms like functional and object-oriented, the goto becomes obsolete and a sand-trap for problems. But that is a conditional evil, not an absolute evil.


You saying it's like "Using GoTo is Capitulation with The Devil".

This post has been edited by AdamSpeight2008: 11 October 2012 - 03:28 PM

Was This Post Helpful? 0
  • +
  • -

#52 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 04:19 PM

View PostBBeck, on 11 October 2012 - 04:25 PM, said:

But not all of that is entirely a question of style. My primary point was that having multiple exit points from a block of code can make it far more difficult to both understand and debug. It costs maybe a couple extra machine instructions to avoid the multiple exit points


Nope, still style. I understand some of the complaint against the multiple exit points but, you know, it's not like you don't know where you're going.

I'm dead seriously when I say I found your code hard to follow. The caps thing is murder, but the while loop where you could use a for loop added confusion and visual noise. Mine was far more clear to me.

I do agree that clarity is absolutely worth of few more operations. This is one of the reasons OO exists. However, I don't believe your vision of clarity and mine of are the same.

I wasn't going to point this out but, um, you missed resetting your inner counter; the code executes one inner loop and then never does so again. You know, if you can't get the thing right...


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

vi roolz, emacs droolz


LOL. I do, actually, prefer vi. Never got the hang of the mighty emacs. Though, given a graphic desktop, I'll usually just go with gedit or kedit or scite when I can get it.
Was This Post Helpful? 1
  • +
  • -

#53 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 04:37 PM

What it dislike about the C/C# style For-Loop, to its not really a For-Loop but a thin clock over a While Loop.

For-Loop has the signature of
 
for ('T i = init; bool cond; 'T step;)
{
 /* code */
}


when it should really be
 
for ('T i = init; 'T end; 'T step;)
{
 /* code */
}


Note: Where 'T is a numeric value type.

If you want a conditional ending of a loop, use while.

This post has been edited by AdamSpeight2008: 11 October 2012 - 04:38 PM

Was This Post Helpful? 0
  • +
  • -

#54 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5882
  • View blog
  • Posts: 12,760
  • Joined: 16-October 07

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 06:13 PM

Oh, it's worse than that. More like:
for (
   thingy at top of block, but not repeated;
   bool check that will leave block; 
   stuff to do at bottom of block)
{
 /* code */
}



A for loop is a convention as much as anything else. The three fundamental steps in a loop are basically [init, check, iter]. It puts those at the top, in theory. In reality, it gives three boxes where stuff happens.

I'm talking C here. Most other languages get a little more picky about it.
Was This Post Helpful? 0
  • +
  • -

#55 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:54 PM

View Postbaavgai, on 12 October 2012 - 02:02 AM, said:

Using gotos, no doubt. No thanks.

I'll do it without gotos and loops if it makes you feel better :P
Heck, I'll throw in "no sub()" as a a free bonus!

View Postbaavgai, on 12 October 2012 - 02:02 AM, said:

It's about destination.

All the programming structures that have jump well wrapped define very precisely where the execution pointer will wind up. A goto just sends that pointer to a label somewhere. Up or down, in or out of at a block, there's no way to tell. This is arbitrary and inconsistent.

So in other words, you are concerned about the dynamic stack. That's understandable. I don't know exactly how goto's interact with the links.

As for "it's hard to know where a goto will end up", it will end up at the label. It's a different way of thinking, granted, but it's not that difficult to understand is it?

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

On the other hand, you've had programmers explaining why goto is bad for almost fifty years. Read the whitepapers if you really care. We're not making this stuff up.

Like I said previously, I have read Knuth's papers and letters on the subject. I reiterated his objections and you are free to discuss whether my reiteration was correct or not.

To say so again;
1) They are hard to understand
2) They are not needed

I don't think I've gotten a satisfactory answer as to why this constitutes treating GOTOs like black magic. Knuth's objections were largely mathematical and academic rather than pragmatic, it seems like most people are jumping on the academic bandwagon.


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

nobody is going to want to work with your code.

This is what I mean, treating people like lepers just because they know what a GOTO is.
"Oh know he just uttered the phrase 'computed goto', he must be trying to summon Satan himself!"

I don't see why we should shun ASM and even handicap people against learning it. Seems pretty mean. I think machine language is quite important, I don't like focusing only on the latest and greatest glossy languages.

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

Goto makes it harder.

I disagree with the assertion that goto's are somehow super hard to use (dare I say, because they were missing from your education?). I said before that none of the points against goto apply to using goto to jump within the same block (apart from - you could replace it with something else, which can be used to argue anything really). No one has came up with anything against that except "gotos bad".

View PostBBeck, on 12 October 2012 - 03:40 AM, said:

So ultimately, it comes down to a trade off between readability and performance.

While I disagree that - discounting the variable and function naming which do not contribute to execution - your code is more readable (I don't think anyone had trouble understanding the code with multiple returns or gotos). I do agree that it is ultimately a choice that you make, and you need to make that choice pragmatically, not dogmatically.

View PostAdamSpeight2008, on 12 October 2012 - 07:37 AM, said:

What it dislike about the C/C# style For-Loop, to its not really a For-Loop but a thin clock over a While Loop.

for(; scanf("%d",&x) > 0 && x != 42; printf("%d\n", x));
for(i=0;i++<10&&doSomething();)/>;
for(doThisOnce();doThisLotsOfTimesFirst();ThenDoThisToo();)/>;


Is it wrong to code like this?

n=7;
i=true;
while(n==7&&i&&(i=!i)) {
// n==7
}



This is fine though, right?

if((predicate()&&then())||else());

This is definitely fine right? Right...?

EDIT: SO why is it parsing smilies inside my [il] blocks? Geez.

This post has been edited by G0rman: 11 October 2012 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#56 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7879
  • View blog
  • Posts: 13,386
  • Joined: 19-March 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 10:04 PM

Quote

I disagree with the assertion that goto's are somehow super hard to use (dare I say, because they were missing from your education?)


You don't seem to be following the argument here. It's not that they're hard to use. They're simple to use: goto label. Done.

They're incredibly powerful and incredibly easy to use. And they make a lot of things really easy.

The trouble is, what they make easy is stuff you don't want, and their power is in their ability to create confusion.


In everything you've said so far, I haven't seen one example of something that makes me wish I had a goto in any of the languages I use. Can you come up with one?
Was This Post Helpful? 0
  • +
  • -

#57 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 - 10:30 PM

View Postjon.kiparsky, on 12 October 2012 - 01:04 PM, said:

Can you come up with one?

Probably not;
Firstly because languages you use probably have a number of constructs made to emulate patterns created with gotos. For example the if/then/else mechanism, and various loops such as for, while, do-while, and mechanisms such as gosub and sub() and switches. There is no situation using gotos that cannot be refactored in to these statements alone - so no matter what I give you, you can simply reply "I can do that without gotos", which is of course true.
Secondly, the things that gotos excel at - namely power and flexibility - are exactly what you are trying to avoid. If someone doesn't like apple, then they aren't going to be impressed with just how appley something tastes.
Thirdly due to confirmation bias, even if I gave you an amazing example you would not acknowledge it.

It's a fruitless task for me to attempt to amaze you with gotos, when you don't want to be amazed and you believe it would be impossible to amaze you.

Needless to say, I have no such examples.

Besides that you are an "Expert In: Java", so naturally the escaping from nested loops is nothing impressive to you, since Java has named breaks specifically to perform the behaviour that gotos otherwise would.

This post has been edited by G0rman: 11 October 2012 - 10:31 PM

Was This Post Helpful? 1
  • +
  • -

#58 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7879
  • View blog
  • Posts: 13,386
  • Joined: 19-March 11

Re: Is it important to know about GOTOs?

Posted 11 October 2012 - 11:11 PM

Quote

Secondly, the things that gotos excel at - namely power and flexibility - are exactly what you are trying to avoid.


Sigh. Okay, this is going nowhere.

If you haven't got an example of this power and flexibility - and I'm honestly interested in seeing it - I think there's not much point in continuing with this. If you think you can come up with some reason why the existing control structures in Java or python or perl are not sufficient - we'll leave aside scala and scheme and common lisp, since they tend to stay away from "control structures" as you'd know them - then you know where to find me.

By the way, if you think Knuth is anti-GOTO, then you obviously haven't actually read him. It's Dijkstra who wrote "GOTO considered harmful". Knuth is pretty pro-goto - one of the only things he ever got wrong, as far as I know. Look for his Literate implementation of Adventure, for example.

Dijkstra's objections to goto were pretty practical: it screws up your code. He was able to provide mathematical arguments why this would be so, but his argument is pretty squarely set on practical grounds. You should probably read it some time before you try to rebut it.
Was This Post Helpful? 0
  • +
  • -

#59 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 - 11:34 PM

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

If you haven't got an example of this power and flexibility - and I'm honestly interested in seeing it - I think there's not much point in continuing with this.

Uh sure. It's just kind of strange to see someone claiming that if and loops are more powerful than gotos, that's all...

// If/then/else with goto
if (predicate) goto THEN;
goto ELSE;
THEN: ;
doSomething();
goto FI;
ELSE: ;
doSomethingElse();
FI: ;



// switch with goto
if (i==1) goto SWITCH_1;
if (i==2) goto SWITCH_2;
if (i==3) goto SWITCH_3;
// DEFAULT
doDefault();
goto END;
SWITCH_1: ;
doOne();
goto END;
SWITCH_2: ;
doTwo();
goto END;
SWITCH_3: ;
doThree();
END: ;



// for loops with goto
i=0
START: ;
if (!i<10) goto END;
doSomething();
i++;
goto START;
END: ;



// while loops with goto
START: ;
if (predicate()) goto END;
doSomething();
goto START;
END: ;



// do-while loops with goto
START: ;
doSomething();
if (predicate()) goto START;



// sub() with goto
stack dynamiclinks;
stack args;

// foo {
foo: ;
dynamicLink.push(&&foo_1);
args.push(7);
goto bar;
foo_1: ;
printf("%d",args.pop());
// }

// bar(int i) {
bar: ;
i=args.pop();
i++;
args.push(i);
goto dynamicLink.pop();
// }


If the ability to emulate any other control structure doesn't convince you that it is more powerful and more flexible than any of them, then I don't know what will.

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

By the way, if you think Knuth is anti-GOTO, then you obviously haven't actually read him. It's Dijkstra who wrote "GOTO considered harmful".

That was my bad o*\

View Postmancecherlg, on 12 October 2012 - 02:33 PM, said:

Think through the logic of this. For every operator
Posted Image
Posted Image
Posted Image
Posted Image
Posted Image

Images don't work at all :/

This post has been edited by G0rman: 12 October 2012 - 12:16 AM

Was This Post Helpful? 0
  • +
  • -

#60 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,233
  • Joined: 11-December 07

Re: Is it important to know about GOTOs?

Posted 12 October 2012 - 02:40 AM

*
POPULAR

Quote

Uh sure. It's just kind of strange to see someone claiming that if and loops are more powerful than gotos, that's all...


I don't think anyone is claiming that loops are more powerful that gotos. That would be absurd since anything you can do with a loop, you can do with a goto. However, what has happened over the years is that common uses of goto that have proven time and again to be useful have been isolated and reformulated. That is where you get your loops, methods, functions, lambdas and polymorphism. These are generally considered good ways of controlling flow in your programs. Then there are cases like break and continue that are a bit more controversial but considered useful by a lot of people so it makes sense to make those concepts concrete.

What is left is all the crap, the antipatterns that obfuscate the intention of the code. I remember writing in languages with line numbers where it was easier to jump all over the place with gotos than it was to move or insert large blocks. We have all experienced code rot of some form or another but what can seem like a rational and sensible goto when you write it can end up as part of a tangled mess six months or six years down the line.

Is goto intrinsically bad? No, but in a modern high level language it represents the rotten apple that nobody wants to eat after the nice fruit has been taken.

Should goto be removed from these languages altogether?

Again, no. If you follow red, green, refactor it doesn't matter what sort of a mess you make while getting your tests to work (or debugging for that matter). Public instance variables, deeply nested loops, monolithic walls of code, copy/pasted code. Why not a goto if it helps? All this is OK as long as you refactor right after getting those tests to work, before you commit to version control. None of those antipatterns are acceptable in production code but they are ok transiently while you are getting things to work.

Should goto be taught in universities?

Also no, at least not in programming 101. I've helped to teach in universities and programming is something people struggle to learn. A few manage to pick up enough skills to solve simple programming problems in the first year. Even fewer are able to craft elegant solutions. That's a skill a lot of people graduate without having picked up. When you are learning to program, you are given a whole bunch of unfamiliar building blocks by a teacher who can't remember a time when programming wasn't second nature. What most people do is fumble with the blocks until they get something that seems to work OK. Can you imagine having access to a block that
seems like it can get them out of any mess created with the other blocks? It's going to be too seductive and eventually it'll become a crutch. Better to learn about goto once you have the skills to not need it.
Was This Post Helpful? 5
  • +
  • -

  • (10 Pages)
  • +
  • « First
  • 2
  • 3
  • 4
  • 5
  • 6
  • Last »