0292: "Goto"

This forum is for the individual discussion thread that goes with each new comic.

Moderators: Moderators General, Prelates, Magistrates

dmoney
Posts: 14
Joined: Sun Jul 22, 2007 1:50 am UTC

Postby dmoney » Sun Jul 22, 2007 2:14 am UTC

A couple posters mentioned not being able to jump out of nested loops without goto. Java does let you do this with loop labels:

Code: Select all

outer_loop:
for (int i = 0 ; i < 10; i++){
   for(int j = 0; j < 10; j++){
      break outer_loop;
   }
   System.out.println("dmoney is wrong"); // should never get here
}



I think you can do the equivalent in c/c++, but I'm not positive.

I have found myself wishing for goto's in Java a couple times and have had to use the loop/switch pattern in one case where goto would have been really convenient. Then again, if it would cause this much discussion, maybe it's better to have to implement goto covertly with switch statements.

Stickmaster
Posts: 16
Joined: Sun Jul 22, 2007 3:18 am UTC

Postby Stickmaster » Sun Jul 22, 2007 3:22 am UTC

I work for a software company, and I regularly work with DBA's of our many clients. They were having issues with a macro that was set to open an application, run a task, and close the application a number of times. I got a hold of the macro, and it was LITTERED with goto's. I then spoke to the DBA, and said "What's the first rule of programing?" and he said "I know, but they're so useful." and I said "goto: hell."

Life is good.

User avatar
Clumpy
Posts: 1883
Joined: Thu Jul 19, 2007 4:48 am UTC
Contact:

Postby Clumpy » Sun Jul 22, 2007 5:27 am UTC

My first programming experience came from using GWBasic on some old green-and-white monochrome display. I was in fourth grade. I counted my lines one by one, forcing myself to GOTO all over the place. The whole thing was a mess by the end. It was like a Choose Your Own Adventure story just to figure out where the heck it was going next.

On my next project, I overcompensated and staggered my lines fifty at a time.

John DeSavage
Posts: 24
Joined: Sat Feb 17, 2007 12:21 am UTC

Postby John DeSavage » Sun Jul 22, 2007 9:17 am UTC

For anyone still paying attention...

This will break your head:

http://en.wikipedia.org/wiki/Duffs_device

(only marginally related to goto, but more about another huge programming pet peeve: case label fall-through.)

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Postby Rysto » Sun Jul 22, 2007 4:06 pm UTC

dmoney wrote:I think you can do the equivalent in c/c++, but I'm not positive.

You can't. You have to use a goto in C/C++.

dmoney
Posts: 14
Joined: Sun Jul 22, 2007 1:50 am UTC

Postby dmoney » Sun Jul 22, 2007 8:28 pm UTC

Rysto wrote:You can't. You have to use a goto in C/C++.


GCC seems to agree with you.

dmoney
Posts: 14
Joined: Sun Jul 22, 2007 1:50 am UTC

Re: Duff's Device

Postby dmoney » Sun Jul 22, 2007 9:15 pm UTC

Linked from the wikipedia page on Duff's Device:

http://www.lysator.liu.se/c/duffs-device.html

Actually, I have another revolting way to use switches to implement interrupt driven state machines but it's too horrid to go into.


Now my curiosity's piqued... ah, found it:

http://www.chiark.greenend.org.uk/~sgta ... tines.html

User avatar
Alpha Omicron
Posts: 2765
Joined: Thu May 10, 2007 1:07 pm UTC

Postby Alpha Omicron » Sun Jul 22, 2007 10:20 pm UTC

When I read the title (I have the archive bookmarked rather than the main page) I thought of both programming and Cryptonomicon. Can I get some points over here?
Here is a link to a page which leverages aggregation of my tweetbook social blogomedia.

Dark Ragnarok
Posts: 1406
Joined: Sat May 05, 2007 9:12 pm UTC

Postby Dark Ragnarok » Sun Jul 22, 2007 10:50 pm UTC

probably already mentioned, but just for fun:

http://www.velociraptors.info/?#

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Postby Rippy » Sun Jul 22, 2007 11:54 pm UTC

I think the only logical conclusion is that velociraptors are monitoring all our programming activity, and attacking those who use "goto".

The way I see it, it's their way of going for the "weakest members of the herd". They reason that only the weak programmers resort to goto, therefore those victims will offer the least resistance.

And, as programmers, they're probably more likely to be less physically active, and thus much more tender and delicious.


....It's all starting to make sense.

(first post btw :P)

Dark Ragnarok
Posts: 1406
Joined: Sat May 05, 2007 9:12 pm UTC

Postby Dark Ragnarok » Mon Jul 23, 2007 1:17 am UTC

congrats ^^. but the mods will get after you, if you don;t go introduce yourself in the thread in "general". prolly should go do that. but welcome ^^

User avatar
Rippy
Posts: 2101
Joined: Sun Jul 22, 2007 11:27 pm UTC
Location: Ontario, Can o' Duh

Postby Rippy » Mon Jul 23, 2007 2:32 am UTC

well, I'll get right on that. Didn't know that was a requirement :o

kwan3217
Posts: 27
Joined: Wed Jul 11, 2007 1:44 am UTC
Location: Sun-synchronous Earth Orbit

Postby kwan3217 » Mon Jul 23, 2007 7:04 am UTC

John DeSavage wrote:For anyone still paying attention...

This will break your head:

http://en.wikipedia.org/wiki/Duffs_device

(only marginally related to goto, but more about another huge programming pet peeve: case label fall-through.)


Case fall-through is only annoying because it is the default, but less likely case. If instead of putting break wherever we want it to not fall through, we put dont_break were we did want it to fall through, life would be sweet because then I could use it when it is good, but don't have to curse at my program after noticing a missing break after debugging for six hours.

The real problem with Duff's device is that it interleaves a switch and a case. I'm surprised a stupid machine could even figure out what to do with the device. If it takes a human several looks to figure out, how can a one-pass compiler figure it out?
Some people look at what is, and ask "why?"
Some people dream about what isn't, and ask "why not?"
I think about it for a while, then say "Oh, that's why not."

John DeSavage
Posts: 24
Joined: Sat Feb 17, 2007 12:21 am UTC

Re: Duff's Device

Postby John DeSavage » Mon Jul 23, 2007 7:16 am UTC

dmoney wrote:Linked from the wikipedia page on Duff's Device:

http://www.lysator.liu.se/c/duffs-device.html

Actually, I have another revolting way to use switches to implement interrupt driven state machines but it's too horrid to go into.


Now my curiosity's piqued... ah, found it:

http://www.chiark.greenend.org.uk/~sgta ... tines.html


That is the worst thing I have ever seen. Thanks. I am going to have nightmares about that now.

I have seen things nearly as bad as that in production code, but that one takes the cake. The worst thing is that the author actually seems to believe it is a good thing. At least Duff admitted that this was horrid and that his device was questionable. That article, on the other hand, is on par with Scientology.

FinalSin
Posts: 29
Joined: Wed Nov 08, 2006 8:26 am UTC

Postby FinalSin » Tue Jul 24, 2007 9:44 pm UTC

space_raptor wrote:GOTO DENGO! Hah! Ugh.

This is a pretty good comic, but it's kind of an inside joke. You have to know about programming, and the raptor thing, and have read Cryptonomicon.

I laughed, but I feel kinda bad for laughing, since its a coding joke.


I think it works just on the programming level. Gotos are bad - you get attacked by a dinosaur. It's funny.

Leliel
Posts: 43
Joined: Fri Jun 15, 2007 4:31 pm UTC

Postby Leliel » Thu Jul 26, 2007 9:30 pm UTC

I just had a coworker come over to ask (in all seriousness) if GOTO would work in a Python script... And of course it immediately reminded me of this comic.

Hmm, now should I leave a printout of this comic on his desk or no? Decisions, decisions... :P

User avatar
Hench
Porn, hence, Man
Posts: 498
Joined: Wed Mar 28, 2007 4:16 pm UTC
Location: Right between my eyes
Contact:

Postby Hench » Thu Jul 26, 2007 10:43 pm UTC

Dress up like a raptor and tackle him. Or, better yet, have Belial attack him.
Spoiler:
Your perceptions will not change reality, but simply color it.

User avatar
Kawa
Posts: 714
Joined: Wed May 02, 2007 12:24 pm UTC
Location: Melbourne, FL/New York City/xkcdia
Contact:

Postby Kawa » Thu Jul 26, 2007 11:28 pm UTC

Hench wrote:Dress up like a raptor and tackle him. Or, better yet, have Belial attack him.

But Belial isn't a raptor! Don't you see the hat?
Kawa likes these things:
Spanish Ninja Bodyguard
math, anime, more!
Origami, Florida
New York, and xkcd.

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Postby EvanED » Fri Jul 27, 2007 12:05 am UTC

Oh me yarm, how the hell did I miss this comic until now?! This is my favorite for quite some time!

Rangsk wrote:Global variables:
The most obvious argument against using global variables is the inevitable naming conflict. Linker errors anyone? ...

Slightly less obvious is that globals have to be declared two or more times to be really useful. Once in an arbitrary cpp file where it actually resides, and externed in all the other cpp files where it's used. If one of the types changes, you better hope you remember to change it everywhere else that it's externed, because externs are by name only and are not type checked!


Good programming practice (putting the extern declaration into a header which is included in the compilation unit that defines it) and compiler warnings/errors should save you from that.

If you aren't using the global in more than one file, please at least use the "static" keyword, which makes it scoped to just that file.


And in C++, the unnamed namespace.

Code: Select all

namespace {
   int foo;
   void bar();
}

is equivalent to

Code: Select all

static int foo;
static void bar();



However, I think the most compelling reason to not use globals is that they have no specified attachment to any code. Globals are only useful to the code that uses those globals, and yet any code anywhere can use them.


This is sort of saying the same thing, but what I see as the real big problem is that you can't tell who uses globals easily. You can't tell, if you decide to change a global (meaning either a source change, like changing its type, or a runtime change of its value), who will be affected, and you can't tell by looking at a function signature what globals can affect its operation.

Gotos and globals both give 'action at a distance', which is a negative property of a program.

Globals are usually just programmers being too lazy to actually group the data with the actions on that data, such as in structs or classes.


This... is being too strong. There are places where, yes, you could encapsulate everything together, but it doesn't give any benefit. For instance, I work with static code analysis tools. When analyzing a program, we store the call graph of the program in a global variable. This means that, for instance, we can't analyze two programs at once. But the problem is, how are we supposed to factor that out? EVERYTHING looks at the call graph; if we can't make it global, either everything needs to be in one gigantic class, or we need to pass parameters around everywhere.

There are times when having a global variable is fine, just like there are times when using goto is fine. There are even times when both can be the best solution.

warhorse wrote:In my experience, the only valid use for goto is for cleanup work. If I'm in a function that is initializing a bunch of stuff, I'll have all of the cleanup in reverse order at the end of the function. Then, if any of the initialization fails, I'll goto the appropriate part of the cleanup block so that only the stuff that has been initted gets deinitted.

We use this at work in our C code and this is the only situation in which I would use a goto.


I will give two more I think are acceptable.

First is the aforementioned jumping out of a nested loop, where you sort of want to use a break but that won't take you far enough. The second is that gotos are a decent way of implementing small finite state machines.

dskippy wrote:Wow. I didn't know Dijkstra has a velociraptor costume.

-mike


I LOL'd.

Actually, from what I hear, Dijkstra didn't need a costume.

frezik wrote:That's not why they're in the Linux kernel. It's because GCC happens to compile gotos very efficiently, and there are many places where a kernel must be as fast as possible.

I don't think this true anymore, but legacy code remains. It was certainly true around the linux 2.2 days.


For me to even think about believing you, you'll need to cite sources from Linus or Morton or someone who says that it's not the best way to do error handling.

Because new code is still being written that uses gotos for this type of error handling, and in fact it's recommended by books such as Linux Device Drivers.

And there's really no better way to do it in straight C.

entropomorphic wrote:Because writing maintainable code means that you have to be anticipating other people coming in and changing things around. Codeblocks like above are a natural single chunk that might be copied and pasted, refactored, or nested inside a loop in the future.


Writing maintainable code also means that it should be easy to understand. Code is maintained more than it's written, but it's also read more than it's changed. I agree with Rysto that the do{ }while(false) is somewhat confusing. If I saw that I would have to stop and think for a couple moments while I figured out what was going on. The goto isn't perfect, but it is more clear.

User avatar
Hench
Porn, hence, Man
Posts: 498
Joined: Wed Mar 28, 2007 4:16 pm UTC
Location: Right between my eyes
Contact:

Postby Hench » Fri Jul 27, 2007 4:17 am UTC

Kawa wrote:
Hench wrote:Dress up like a raptor and tackle him. Or, better yet, have Belial attack him.

But Belial isn't a raptor! Don't you see the hat?


The Viking hat made out of Vikings? It fools no one!
Spoiler:
Your perceptions will not change reality, but simply color it.

Lester :P
Posts: 129
Joined: Sun Jul 29, 2007 2:04 pm UTC
Contact:

Postby Lester :P » Mon Jul 30, 2007 11:25 am UTC

placeholder wrote:Actually, Goto Dengo had a fairly major role in the Cryptonomicon. Major Spoilers follow hereafter, in white. Select the text to make it easily legible.

He was on a ship in the Japanese navy as it was sunk, got stranded, lived in the jungle for a while, rejoined the Japanese army, eventually got slated to build the vault where the gold was kept, eventually interacted with Shaftoe and became a friend to MacArthur, founded the Dengo construction business that later shows up building the Crypt, then goes on to unite with Randall Waterhouse and his business partner when they decrypt the Arethusa Intercepts and find the location of the vault where the gold was kept.



I seriously think he was one of the most important characters, and showed up as a point-of-view character rather frequently (one of the four total, if I recall correctly, 2x Waterhouses, 1x Shaftoe, 1x Dengo).


Apparently he was the most moral of all the character, according ti stephenson.
`When I use a word it means just what I choose it to mean-Neither more nor less.'
`The question is whether you can make words mean so many different things.'
`The question is,'said he,`Which is to be master-That id all.'

ihope127
Posts: 66
Joined: Mon Jul 30, 2007 3:59 am UTC
Location: 127.0.0.1
Contact:

Postby ihope127 » Mon Jul 30, 2007 2:53 pm UTC

Code: Select all

head [foo x y | x <- [1..9], y <- [1..9], bar x y]


There's one legitimate use of GOTO Haskell doesn't have...

Code: Select all

thirdElem success _ (_:_:x:_) = success x
thirdElem _ failure xs = failure xs


...and there's the other! (Okay, another. I can't think of any more at the moment.)

For the first one: "head" is the "first element" function, and the remaining code is a list comprehension, which goes through every pair of x and y (in the order (x,y) = (0,0), (0,1), etc.) and for each one checks whether bar x y is true and if so adds foo x y to its result. However, Haskell is lazy, so it only does what's required: since head only pays attention to the first element, the comprehension only has to find one case where bar x y is true before returning the appropriate foo x y.

And the second: ":" is the "cons" operator: x:y is the list whose first element is x and the rest of which is y. Since this operator is right-associative, (_:_:x:_) is essentially a fancy way of writing ([_, _, x] ++ _). thirdElem takes two arguments. In the first case, the first argument is named "success" and the second is ignored--"_" essentially means "ignore this". The third argument is broken down into its first three elements and the remainder, and everything is ignored except the third element. "success" is applied to this third element to give the result.

If the list can't be broken down into three elements and a remainder because it contains less than three elements, the first case is skipped and the second one is chosen. This time, the first argument is ignored and the second one is called "failure". The third argument is just called "xs" rather than being broken down at all. Since no breaking down is required here, the second case can never fail. The "failure" function is applied to xs to give the result.

Whew! Much easier done than said--the bulk of my effort here went into explaining the examples, not coming up with them.
There is a significant chance that an artificial intelligence created within the next few decades will not value humanity and therefore will treat us as we treat animals. It would be awesome if xkcd mentioned this.
----
Find me on freenode as uorygl.

iw
Posts: 150
Joined: Tue Jan 30, 2007 3:58 am UTC

Postby iw » Tue Jul 31, 2007 5:07 am UTC

K&R on goto:
C provides the infinitely-abusable goto statement, and labels to branch to. Formally, the goto is never necessary, and in practice it is almost always easy to code without it. We have not used goto in this book.
Nevertheless, there are a few situations where gotos may find a place. The most common is to abandon processing in some deeply nested structure, such as breaking out of two or more loops at once. The break statement cannot be used directly since it only exits from the innermost loop. Thus:

Code: Select all

    for( ... )
      for ( ... ) {
        ...
        if(disaster)
          goto dengo;
      }
    ...
dengo:
  clean_up_the_mess

This organization is handy if the error-handling code is non-trivial, and if errors can occur in several places.


Then it goes on to say that sometimes you can get rid of gotos using flags or other bookkeeping.
Last edited by iw on Wed Aug 01, 2007 7:59 pm UTC, edited 1 time in total.

dosboot
Posts: 143
Joined: Sun Jul 01, 2007 5:26 am UTC

Postby dosboot » Tue Jul 31, 2007 8:17 am UTC

Discussions on using GOTO mirror discussions on accepting the Axiom of Choice in mathematics. At first it appears that everyone thinks that either everyone doesn't like it or just that everyone else doesn't like it. After a while it appears that everyone thinks that either everyone thinks that everyone doesn't like it or that everyone thinks that just everyone else doesn't like it.

Also: Goto considered awesome.

User avatar
Chase Watkins
Posts: 48
Joined: Wed Aug 01, 2007 2:58 pm UTC
Contact:

Postby Chase Watkins » Wed Aug 01, 2007 3:07 pm UTC

If you think that's bad, you probably haven't posted invalid information on wikipedia!

Sriad
Posts: 24
Joined: Fri Aug 03, 2007 8:13 am UTC

Postby Sriad » Fri Aug 03, 2007 8:23 am UTC

frezik wrote:
placeholder wrote:I seriously think he was one of the most important characters, and showed up as a point-of-view character rather frequently (one of the four total, if I recall correctly, 2x Waterhouses, 1x Shaftoe, 1x Dengo).


Yeah, Stephenson wrote enough on just Dengo to fill a normal book. But this is Stephenson we're talking about. The last time he wrote a normal-sized book, Reagon was president.

Yamamoto gets to be a POV character briefly, too.

While looking around at Stephenson books, I caught something on Amazon that disturbs me greatly. "Zodiac" has nothing to do with astrology (it's named after the inflatable boat), but the publisher sticks that right on the cover. What was wrong with the old fish-with-a-gas-mask cover?


Because it's Cancer the Lobster (really Crab). Eh, eh? :wink:

Maybe a little too cute, but it's more plot-y than the gas mask fish.

User avatar
Unforgiven
Posts: 162
Joined: Wed Jul 25, 2007 8:48 am UTC
Location: Tokyo, Japan

Postby Unforgiven » Fri Aug 03, 2007 8:51 am UTC

dmoney wrote:A couple posters mentioned not being able to jump out of nested loops without goto. Java does let you do this with loop labels:

Code: Select all

outer_loop:
for (int i = 0 ; i < 10; i++){
   for(int j = 0; j < 10; j++){
      break outer_loop;
   }
   System.out.println("dmoney is wrong"); // should never get here
}


I find the cleanest way to deal with nested loops is to put them in a seperate function. That way, you can cleanly tell the outer loop when it needs to break by the return value of the function of the inner loop.

Code: Select all

bool InnerLoop()
{
  for( int j = 0; j < 10; ++j )
  {
    if( some_condition )
      return false;
  }
  return true;
}

void Foo()
{
  for( int i = 0; i < 10; ++i )
  {
    if( !InnerLoop() )
      break;
  }
}


Of course I'd use better function names in real code.

Moving nested loops to functions has other advantages too. It frequently means you don't need to worry about the name of your iterator variables. It reduces the length, the nesting level, and the cyclomatic complexity of the individual functions. And it's frequently more logical (except when you're dealing with a matrix, then it might make more sense to just leave the nested loops together).
"Now we're at the museum. Do you think they'll have DDR in there too?"
"It's Japan. Of course they will."

royalfire
Posts: 9
Joined: Wed Sep 05, 2007 11:56 pm UTC

Re:

Postby royalfire » Mon Oct 06, 2008 4:21 am UTC

Lester \":P\" wrote:
placeholder wrote:Actually, Goto Dengo had a fairly major role in the Cryptonomicon. Major Spoilers follow hereafter, in white. Select the text to make it easily legible.

He was on a ship in the Japanese navy as it was sunk, got stranded, lived in the jungle for a while, rejoined the Japanese army, eventually got slated to build the vault where the gold was kept, eventually interacted with Shaftoe and became a friend to MacArthur, founded the Dengo construction business that later shows up building the Crypt, then goes on to unite with Randall Waterhouse and his business partner when they decrypt the Arethusa Intercepts and find the location of the vault where the gold was kept.



I seriously think he was one of the most important characters, and showed up as a point-of-view character rather frequently (one of the four total, if I recall correctly, 2x Waterhouses, 1x Shaftoe, 1x Dengo).


Apparently he was the most moral of all the character, according ti stephenson.


I just had to resurrect this thread for GOTO: dengo.

Also, yes, Goto Dengo is a moral person who struggles against the societal pressure to be "honorable" which usually involves doing things he instinctively feels are immoral. My second-favorite main character, third-favorite POV character. The piece from Yamamoto's POV was fairly awesome. Especially with his realization that the Americans had broken the Japanese codes about 10 seconds before he dies. The bit abut poker was also awesome.

But then the whole Cryptonomicon was awesome; it took me a while to get into it the first time i read it, but when I came back to it I couldn't put it down.

ehva
Posts: 1
Joined: Sun Oct 12, 2008 2:26 am UTC

Re:

Postby ehva » Sun Oct 12, 2008 2:29 am UTC

gompers wrote:That's an appropriate punishment for using a GOTO.

Multiple raptors would be necessary for GOTO DENGO.


What about GOTO OTOG?

Tolchok
Posts: 21
Joined: Mon Oct 06, 2008 1:42 am UTC

Re: Re:

Postby Tolchok » Sun Oct 12, 2008 4:43 pm UTC

Lester wrote:I seriously think he was one of the most important characters, and showed up as a point-of-view character rather frequently (one of the four total, if I recall correctly, 2x Waterhouses, 1x Shaftoe, 1x Dengo).


And then there's the Baroque Cycle...one more Waterhouse, one more Shaftoe, and some random Eliza...

And in the non-POV's there's two more Shaftoes, another Waterhouse, and one Gabriel Goto.

Cryptonomicon also mentions Goto Dengo's son Goto Furudendo too.
Don't you hate signatures like this:

If you have no originality, copy this into your signature...and have a signature no different from lots of other people! Kind of defeats the point of sigs.

báhdom
Posts: 2
Joined: Sat Mar 14, 2009 9:34 pm UTC

Re: "Goto" Discussion

Postby báhdom » Sat Mar 14, 2009 9:50 pm UTC

Hi =],

I made an animated gif from the goto comic. Its just a slide, but I thought someone might like it.
Its on my website [which is pretty barebone so sorry =p ]: bahdom [dot] com



P.S. No, I'm not a spambot =p

User avatar
Alpha Omicron
Posts: 2765
Joined: Thu May 10, 2007 1:07 pm UTC

Re: "Goto" Discussion

Postby Alpha Omicron » Sun Mar 15, 2009 6:43 am UTC

báhdom wrote:P.S. No, I'm not a spambot =p
They're getting smarter.
Here is a link to a page which leverages aggregation of my tweetbook social blogomedia.

báhdom
Posts: 2
Joined: Sat Mar 14, 2009 9:34 pm UTC

Re: "Goto" Discussion

Postby báhdom » Sun Mar 15, 2009 4:15 pm UTC

haha =p

shadyraptors
Posts: 13
Joined: Fri Mar 27, 2009 3:09 pm UTC

Re: "Goto" Discussion

Postby shadyraptors » Wed Apr 01, 2009 9:15 pm UTC

I code mostly on my calculator, and goto is one of my top ten commands what's wrong wi-[veloiraptor attack]-th it?
Image
HARDCORE PORNOGRAPHY
Image
I kinda started a blog. It is really wimpy.

User avatar
Spoom
Posts: 109
Joined: Fri Feb 02, 2007 7:45 pm UTC

Re: "Goto" Discussion

Postby Spoom » Wed Jul 01, 2009 5:52 am UTC

So, apparently the goto operator was added to PHP recently (who knows how that came into discussion). They, of course, added documentation on the new operator.

Scroll to the bottom. :D
All other things being equal, the simplest solution is ale.

User avatar
Troy Martin
Posts: 44
Joined: Fri Nov 28, 2008 4:08 am UTC
Location: Langley, BC, Canada
Contact:

Re: "Goto" Discussion

Postby Troy Martin » Thu Jul 02, 2009 5:14 am UTC

The only program I've used goto in is an unfinished text adventure in C. As such, the source is freakin' huge just for ten rooms and the code is fugly.
Howdy.

ackan
Posts: 3
Joined: Tue Jan 31, 2012 1:59 am UTC

Re: 0292: "Goto"

Postby ackan » Wed Oct 03, 2012 12:15 am UTC

This is the problem with mixing low-level code with high-level code.
Low level code can often be faster, smaller, better... but high level code is "pretty."
Unfortunately, high level code is far easier to optimize than low-level code... hence the whole point of getting rid of and putting the whole "EVIL" tag on goto.

Thing is... our high-level (well, mid-level) code has not radically evolved where as the hardware has.
We want more complex code running on more powerful hardware... but don't have "acceptable" replacements for low-level commands. (ie, continue, break, goto).

But all this is rather moot... "mainstream programming" is no longer populated by people who care about how well their software runs... only that it compiles. Like the "64-bit revolution," --- which I consider to only be about people being too lazy to optimize their memory footprint [in the domestic context, of course] --- "common practice" creates programmers that, while not violating the sacred "goto" sacrament, create quite horrific code.


I mean, goto is NOT BAD... we replaced many of the instances where people would normally use goto... but so long as we stick with this structure... we will still have instances were people feel goto could make things so much easier. Hence the structure is the greater fault, having not progressed to fill the voids removing goto created.

J Thomas
Everyone's a jerk. You. Me. This Jerk.^
Posts: 1190
Joined: Fri Sep 23, 2011 3:18 pm UTC

Re: 0292: "Goto"

Postby J Thomas » Wed Oct 03, 2012 2:31 am UTC

ackan wrote:This is the problem with mixing low-level code with high-level code.
Low level code can often be faster, smaller, better... but high level code is "pretty."
Unfortunately, high level code is far easier to optimize than low-level code... hence the whole point of getting rid of and putting the whole "EVIL" tag on goto.


You want to argue about GOTO? I'm game, although it's a dead issue. Nobody cares anymore.

Thing is... our high-level (well, mid-level) code has not radically evolved where as the hardware has.
We want more complex code running on more powerful hardware... but don't have "acceptable" replacements for low-level commands. (ie, continue, break, goto).

But all this is rather moot... "mainstream programming" is no longer populated by people who care about how well their software runs... only that it compiles. Like the "64-bit revolution," --- which I consider to only be about people being too lazy to optimize their memory footprint [in the domestic context, of course] --- "common practice" creates programmers that, while not violating the sacred "goto" sacrament, create quite horrific code.


The way I see, it, they had problems which were very hard to solve. So instead of trying to solve those hard problems, they redefined the problem to give themselves problems which were not only very hard to solve but also very hard to tell how good their solutions were!

So for example, in the old days you had people who wanted to use their special skills to make things faster. But if you use special tricks to make your code faster, in a few short years there will be faster hardware available that your code will no longer be so fast on. So how much were your special skills worth? To keep the speed advantage somebody has to rewrite your code every few years.

Instead, they decided that the most important thing was to make it easy for somebody else to rewrite your code in a few years. The more expensive it is to maintain your code, the less valuable it is. Worse, if it's hard to understand when you're done, how bad will it be after a few rounds of revision by other people? So the important thing is to make it easy to maintain.

So here you are writing your code, and how will they find out how hard it is to maintain it? They'll find that out a few years down the road when you are gone and somebody else is maintaining it. A lot of good that does you! But when they ask why it's taking you so long to produce working code, you can tell them that you're doing it a special way that will be easier to maintain, and see if they agree to that. More likely they'll want it to work first, and then they'll want a maintainable version later. But languages and skills are often oriented around that....

I mean, goto is NOT BAD... we replaced many of the instances where people would normally use goto... but so long as we stick with this structure... we will still have instances were people feel goto could make things so much easier. Hence the structure is the greater fault, having not progressed to fill the voids removing goto created.


Put it this way -- it's typically easier if you have your high level code organized into a series of low-level pieces. Call a low-level routine that does one thing. Then call a low-level routine that does a second thing. Etc. Each time you return to the place in the source code that you are looking at right now, so it makes sense to you. If you are using a routine exactly once, you might as well just put it into your code at the point you'd GOTO it rather than GOTO it. But if you use it from two places, then you can GOTO it from both of them, and then you need to decide which of them to continue running, which is likely to result in a conditional GOTO there. Often easier if you just call the routine and return.

But sometimes that's less efficient. You return when it's the last routine you call, and the calling routine itself is about to end. So you have two returns when just one would do. OK, no big deal. If your optimizing compiler sees that it's the last call from this routine with nothing after it, then the compiler can substitute a GOTO for that last call. Very simple rule. If there's something to do in the current routine after calling, then call-and-return. If there's nothing left to do here, GOTO. That gets most of the advantages of GOTO without the programmer having to pay any attention.

Another time that GOTO can be useful is when people just want to bail out of some complicated situation, for example when there has been an error. Just GOTO the code that handles that error, and let it sort out the mess. Various languages have a catch-throw pair of commands that handles this circumstance. It handles a lot of the mess inside its own simple structure, and you can handle as much more as you want to. It works better than GOTO for that circumstance.

So I don't think that GOTO would solve a lot of problems. The issue for me is more that people try to hide complexity in half-baked ways. Ideally you would handle low-level stuff in ways that are very simple, that simply map what actually goes on, onto something that's conceptually simple. Hide the details that high-level code can ignore completely, keep the high level calls to it simple, and still have the high level calls match up well to what's actually going on so that people who use the routines will instinctively have a feel for everything that could cause problems.

Instead often the calls to low-level stuff matches up better with the preconceptions of high-level programmers than with what is actually going on. So they have it nice and easy as long as things work, but when there's a problem they're clueless.

I don't know how to solve that. It would help if all the code except the lowest-level stuff was written in the same language. But in reality it's often considered necessary to glue together some sort of Frankenstein's monster out of C, perl, TCL, scheme, and python. You just have to live with it. If it seems easier in the short run to use code that works from a variety of sources than to reinvent wheels hoping to make them easier to use.

The various gimmicks that were supposed to make it better, probably have not. For example, OO had some promise when the idea was that you hired a few geniuses to build the objects and then you could hire programmers of average intelligence to use those objects to do mundane maintenance. But in practice it seems like you get programmers of average intelligence who make and change objects whenever they see the need, and the things which were supposed to impose structure etc just add overhead while the complexity grows....

I don't expect this to get sorted out any time soon. When I was a kid listening to computer science students talk, they argued about how COBOL was the dominant programming language because so many companies had such a big investment in it. And their view was usually along the lines of "That is really a negative investment. It costs them more to maintain it in COBOL than it would cost to rewrite it in C." Because C was then considered the perfect language to do anything. Since then there have been lots of different best languages to replace COBOL, and yet how will a manager know that a new approach will work better than what he's doing until he has already committed to something that can get him fired? If he keeps doing what he's doing the results are at least predictable.

It's a puzzle. If you try to be conservative you'll probably be using something that's been obsolete for decades, that costs a lot to run. If you try to keep up with the times, there's no point working hard to get stuff right because it will all be replaced in a few years anyway. You can't win for losing.
The Law of Fives is true. I see it everywhere I look for it.


Return to “Individual XKCD Comic Threads”

Who is online

Users browsing this forum: AndrewGPaul, Majestic-12 [Bot] and 41 guests