Why is it... Thread [goto discussion]

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

addiction
Posts: 27
Joined: Wed Dec 02, 2009 6:22 pm UTC

Why is it... Thread [goto discussion]

Postby addiction » Mon Mar 01, 2010 5:21 pm UTC

Why is it that Go To statements in coding are so taboo? My professor mentioned it last semester when he was teaching us Assembly... and then showed us part of the coding that was essentially a Go To statement. XD Because he's awesome like that.

But heck... I don't even know how to do a go to statement in C++, let alone why it's considered so bad.. what's the deal?

Akujin
Posts: 27
Joined: Sat Feb 28, 2009 4:38 pm UTC

Re: Why is it... Thread

Postby Akujin » Mon Mar 01, 2010 5:30 pm UTC

It's generally considered bad because in most situations, there is a cleaner/better/more readable way of achieving the desired functionality (at levels higher than assembly). In order to prevent new programmers developing dodgy habits, it is taught as being plain 'bad' and left to the programmer to gain enough experience to know when it's actually OK (an example from this forum a little while back was breaking out of arbitary nesting levels in C).

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Why is it... Thread

Postby You, sir, name? » Mon Mar 01, 2010 5:32 pm UTC

addiction wrote:Why is it that Go To statements in coding are so taboo? My professor mentioned it last semester when he was teaching us Assembly... and then showed us part of the coding that was essentially a Go To statement. XD Because he's awesome like that.

But heck... I don't even know how to do a go to statement in C++, let alone why it's considered so bad.. what's the deal?


Because Djikstra says so. In assembly, it's unavoidable, as assembly doesn't really have any other code-flow mechanisms. But in languages that do, it severely impairs readability.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

0rm
Posts: 81
Joined: Wed Feb 17, 2010 2:30 pm UTC

Re: Why is it... Thread

Postby 0rm » Mon Mar 01, 2010 5:46 pm UTC

Leads to spaghetti code.
They say it's unhackable; I think it can be hacked.
They say it's fast; I think it could be faster.
They say it's the best; I think it can be done better.

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Why is it... Thread

Postby lulzfish » Mon Mar 01, 2010 5:47 pm UTC

It's hard to quantify, but it's supposed to be easier to read something like "if-else, while, for, repeat-until" than something like "if goto 1 else goto 2, if running then goto 3, if ! until then goto 4"

With loops and if-else, code is always a neat hierarchical structure, and you even get scoping blocks by default. With gotos, the structure can get messy quickly, and it's hard for someone to read the code and actually understand it.

Sometimes I wish I had them for breaking out of loops early, though. "break;" works, but what if I'm in two nested loops? I can't break twice. Oh well.

And like YS,N? said, goto is necessary in assembly because it can implement all the higher structures like "while" and "repeat-until", so it's the only one CPUs actually implement. It's usually called "jump", though.

User avatar
Xanthir
My HERO!!!
Posts: 5400
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: Why is it... Thread

Postby Xanthir » Mon Mar 01, 2010 5:50 pm UTC

addiction wrote:Why is it that Go To statements in coding are so taboo? My professor mentioned it last semester when he was teaching us Assembly... and then showed us part of the coding that was essentially a Go To statement. XD Because he's awesome like that.

But heck... I don't even know how to do a go to statement in C++, let alone why it's considered so bad.. what's the deal?

When you're programming, you want your code to be clear and predictable. You want it to be easy to read the code and immediately see what's going on. If you're at point A, it should be obvious that next you'll be at point B. If you're at point B, it should be clear you just came from point A. Gotos break that pretty badly. There are a lot of other ways to screw up your code, but a goto is *almost always* bad in this way. It has basically no redeeming quality in a proper language.

Edit: To state it somewhat better, goto is fundamentally a *low-level* construct. It is virtually never a good idea to futz about with registers in a proper high-level language; goto is on that same level. You use it in Assembly because you can't do any better. You use it occasionally in low-level languages like C because it doesn't have a lot of higher structure. In proper languages, though, you should virtually always reach for a more structured alternative, as they'll make your code easier to read.

Sometimes I wish I had them for breaking out of loops early, though. "break;" works, but what if I'm in two nested loops? I can't break twice. Oh well.

Learn a proper language. Chrissakes, even PHP has a multi-break statement.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

UMOP
Posts: 54
Joined: Sat Feb 06, 2010 1:08 am UTC

Re: Why is it... Thread

Postby UMOP » Mon Mar 01, 2010 6:42 pm UTC

Xanthir wrote:
Sometimes I wish I had them for breaking out of loops early, though. "break;" works, but what if I'm in two nested loops? I can't break twice. Oh well.

Learn a proper language. Chrissakes, even PHP has a multi-break statement.

I do believe I disagree with your definition of proper. If I can do what I need to get done, that's good enough for me. Lacking a multi-break statement can be worked around with a little logic.

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Why is it... Thread

Postby lulzfish » Mon Mar 01, 2010 6:51 pm UTC

UMOP wrote:
Xanthir wrote:
Sometimes I wish I had them for breaking out of loops early, though. "break;" works, but what if I'm in two nested loops? I can't break twice. Oh well.

Learn a proper language. Chrissakes, even PHP has a multi-break statement.

I do believe I disagree with your definition of proper. If I can do what I need to get done, that's good enough for me. Lacking a multi-break statement can be worked around with a little logic.

I would learn a proper language but it seems that most stuff is written to interface with C++ easily so I'm using that.
:?
And I just usually use a boolean sentinel with some awkwardly placed "if (! userDecidedToQuit)" to make it work.
Come to think of it, I can't remember needing to break out of a nested loop, but there's something unusual about using break to do anything. How do I know which loop to break out of? Sentinels are more descriptive, but a "exit loop descriptiveName" would be excellent.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Why is it... Thread

Postby Berengal » Mon Mar 01, 2010 7:23 pm UTC

If you replaced all uses of if, for, while, break, continue and return with gotos your programs would still be just as readable. What makes it unreadable are all the other uses of goto. After all, all those other statements are just gotos with different names and slightly different syntax, but the structure is the same, only a few lines would change.
lulzfish wrote:And I just usually use a boolean sentinel with some awkwardly placed "if (! userDecidedToQuit)" to make it work.

No. Just no. If you have labeled breaks you should use those. If you don't have those but you do have gotos you should use those instead. Only as a last resort should you use booleans because they will always be harder to read and reason about.

Any proper language allows you to define your own control flows though.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
lulzfish
Posts: 1214
Joined: Tue Dec 16, 2008 8:17 am UTC

Re: Why is it... Thread

Postby lulzfish » Mon Mar 01, 2010 7:52 pm UTC

Labelled break?

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Why is it... Thread

Postby Berengal » Mon Mar 01, 2010 8:04 pm UTC

Yes, it's like an "exit loop descriptiveName" except it looks like "break descriptiveName" instead.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Area Man
Posts: 256
Joined: Thu Dec 25, 2008 8:08 pm UTC
Location: Local

Re: Why is it... Thread

Postby Area Man » Mon Mar 01, 2010 8:45 pm UTC

Goto should be used sparingly; it can be easily abused like pointers or other things.

Nested looping is one of the few legitimate uses of 'goto', and why it exists (think of it as a more general labeled-break).

Though a better solution is to put the loops in a separate function and use 'return'.
Bisquick boxes are a dead medium.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Why is it... Thread

Postby Berengal » Mon Mar 01, 2010 9:03 pm UTC

Area Man wrote:Nested looping is one of the few legitimate uses of 'goto', and why it exists (think of it as a more general labeled-break).
Presumably you mean "why it hasn't been removed entirely", because goto existed long before any of the structured control structures did.

This is one of the reasons I like lazy evaluation, by the way. Need to break out of a nested loop? Just evaluate the entire loop and let whoever uses the result worry about the breaking. Using the result of a nested loop? You can't even tell the difference between that and a hard-coded constant. Values will be evaluated when you need them, in the order you need them and only as much of them as you need.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

addiction
Posts: 27
Joined: Wed Dec 02, 2009 6:22 pm UTC

Re: Why is it... Thread

Postby addiction » Mon Mar 01, 2010 11:04 pm UTC

Area Man wrote:Goto should be used sparingly; it can be easily abused like pointers or other things.

Nested looping is one of the few legitimate uses of 'goto', and why it exists (think of it as a more general labeled-break).

Though a better solution is to put the loops in a separate function and use 'return'.


Oh gawd... don't mention pointers... we're working with nodes and pointers and references and... s-stuffs that's making my head hurt... Ugh... need a tutor... >.o

Any way... I suppose the general idea is that goto makes reading and understanding the code so difficult that it's just better to not use them if possible?

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Why is it... Thread

Postby Berengal » Mon Mar 01, 2010 11:19 pm UTC

addiction wrote:Any way... I suppose the general idea is that goto makes reading and understanding the code so difficult that it's just better to not use them if possible?
It's always possible to not use them if you've got loops and ifs, and by adding more control structures such as functions and exceptions you've got even less use for them. Many modern languages don't even have support for goto at all.

The real answer, as with many other things is "don't use it if you shouldn't use it. If you don't know if you should use it, don't."
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: Why is it... Thread

Postby Sc4Freak » Mon Mar 01, 2010 11:26 pm UTC

In general I hate these sorts of rules, like "never use goto". Goto has its uses. If you just want to go somewhere, goto is the best way to do that. A very common usage is error handling in C - there's really no better way to handle it so goto is fine.

In C++, there's much less reason to use goto except for breaking out of a deeply nested loop, but it doesn't mean you should resort to even less readable hacks for the sake of avoiding goto.

addiction
Posts: 27
Joined: Wed Dec 02, 2009 6:22 pm UTC

Re: Why is it... Thread

Postby addiction » Mon Mar 01, 2010 11:53 pm UTC

A decent argument for goto... except either way, I still have no idea how to even call a goto statement and use it... really, I'd like to use it, but at the moment, since it's so taboo in my classes, it'd be more for kicks.

But kicking fun... soooo... can someone give me a crash course in using it? Or is it a bit too complicated for a post?

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: Why is it... Thread

Postby Berengal » Tue Mar 02, 2010 12:11 am UTC

Code: Select all

int i = 0;
int j = 0
foo: i++;
bar: j++;
if (i % 10 != 0)
  goto baz; // When/if execution reaches this line it will jump to the line that starts with "baz:" below and continue execution from there
if (i % 5 == 0)
  goto bar; // When/if execution reaches this line it will jump to the bar line above.
goto foo; // And this jumps to the foo line.
baz: printf("i = %d, j = %d\n",i, j);


In all honesty though, you can't teach someone to use goto beyond the semantics. It's a tool used in accomplishing a goal, not a goal in itself. Frequently it's the wrong tool, but occationally it's the right one. When and how to use it only comes with experience.
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
phlip
Restorer of Worlds
Posts: 7569
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: Why is it... Thread [goto discussion]

Postby phlip » Tue Mar 02, 2010 12:25 am UTC

addiction wrote:But kicking fun... soooo... can someone give me a crash course in using it? Or is it a bit too complicated for a post?

It's not that complicated

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define BUFFER_LEN 128

int main()
{
   long n;
   char buffer[BUFFER_LEN];
getinput:
   printf("Enter a number: ");
   if (!fgets(buffer, BUFFER_LEN, stdin))
      return EXIT_FAILURE;
   char *p = NULL;
   n = strtol(buffer, &p, 10);
   /* check the input was just a number */
   if (!p || p == buffer)
      goto inputerror;
checkinput:
   if (!p || p >= (buffer + BUFFER_LEN)) /* sanity check */
      goto inputerror;
   if (*p == '\0')
      goto inputsuccess;
   if (!isspace(*p))
      goto inputerror;
   p++;
   goto checkinput;
inputerror:
   printf("That isn't a number, try again.\n");
   goto getinput;
inputsuccess:
   printf("%ld! = ", n);
   long acc = 1;
   if (n <= 1)
      goto endfacloop;
facloop:
   acc *= n;
   n--;
   if (n > 1)
      goto facloop;
endfacloop:
   printf("%ld\n", acc);

   return EXIT_SUCCESS;
}
And, as a bonus, many of those sections could even be completely rearranged, and the code would still work! But it would be so much easier to read with a couple of loops instead.

Also, I fixed your thread subject line.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

User avatar
thoughtfully
Posts: 2253
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN
Contact:

Re: Why is it... Thread [goto discussion]

Postby thoughtfully » Tue Mar 02, 2010 12:26 am UTC

Ahem.. I'm reminded of another thread..
But now I feel as though I've contributed to the delinquency of a minor.
I'll just go have a shower now.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 24 guests