0

Possible Duplicate:
GOTO still considered harmful?

Recently I've been reading "The C Programming Language" by K&R. I don't know much C, but I know a bit of Java, and on page page 65 (2nd ed.) the writers talk about Goto and how it is almost always unwarrented. Recently I've been writing a game, where the computer must show some primitive level of AI, where I wrote lots of for, whiles, and ifs in Java to emulate some sort of intelligence. As primitive as it was, it there were four-deep embedded "for loops" with if statements all over the place, and by the end I lost track of where to put the breaks and was breaking some loops while still looping through others making my game unplayable. Eventually, after much stress I sorted it.

My questiuon is: How bad is Goto?

If I had a goto in my game (Java doesnt really provide one) I wouldn't have wasted so much time fiddling with breaks and brackets. Is Goto really so bad? Why are they so hated?

Community
  • 1
  • 1
  • 1
    duplicate: http://stackoverflow.com/questions/46586/goto-still-considered-harmful – Alexandre C. Dec 28 '10 at 14:57
  • Basically for the same reason as you lost yourself in too many nested looks and if conditions, cause it creates spaghetti code. It is really hard to maintain some code that jumps the execution constantly. To avoid having too many nested loops and conditional branches use functions to organise your code better. – Luis Dec 28 '10 at 15:02
  • 1
    You would have ended up with the same problem if you had used goto statements. If you have that many nested loops etc., you should probably structure your code differently to avoid that. That also helps to understand what you actually wrote. :) – hangy Dec 28 '10 at 15:02
  • Really? I built the AI code over time, so my initial plan became maningless after various reconstructions and it just ended up spaghettified. But instead of break}break}break} I could have just jumped out of the loop, I would have been much happier. –  Dec 28 '10 at 15:06
  • related: http://stackoverflow.com/questions/2910017/being-pressured-to-goto-the-dark-side – Alexandre C. Dec 28 '10 at 15:07
  • Also? Is it possible to prevent spaghettification for complicated loops? –  Dec 28 '10 at 15:08
  • @JJG: with multiply nested loops, it's usually better to put the inner loops into their own methods. But you actually *can* jump out of nested loops cleanly in Java by using a labelled break or continue (which actually has a goto-like syntax): http://www.janeg.ca/scjp/flow/labels.html – Michael Borgwardt Dec 28 '10 at 15:12
  • Thank you Michael. Good to know. Its too late to change my code now, but next itme I'll use that. But it looks just as "dirty" as GOTO, which makes me wonder, why the prejudice against GOTO? –  Dec 28 '10 at 15:15
  • The fact that you ended up with break} break} break} tends to indicate that the code is anyway badly structured. If you look at it with fresh eyes you'll probably start to notice places where you could have properly encapsulated in separate methods or even classes. The prejudice against GOTO is simply because it disrupts the sequential flow of structured programming. It is similar to the prejudice against global variables. Both are convenient in the short term, for beginners, but create a mess in the long term. – jbx Dec 28 '10 at 15:17
  • What is wrong with global vairables? (I am a beginner). In Java I have to admit, I probably declare too many, and unnecessary class variables out of laziness. –  Dec 28 '10 at 15:22
  • "What is wrong with global vairables" - Amongst other things, they introduce hard-to-track dependencies, which makes any non-trivial code hard to test, hard to maintain, and hard to debug ("where did *that* change in variable `x` happen? Oh, a completely different class on a different thread *probably* changed it."). – Piskvor left the building Dec 28 '10 at 15:27
  • 1
    Among all the things "Considered Harmful", `goto` is definitely the least harmful. Global variables (especially any objects with constructors, including "singletons"), recursive make, and casting the return value of `malloc` are all much worse in my book, along with plenty other sins. – R.. GitHub STOP HELPING ICE Dec 28 '10 at 19:51
  • @Piskvor, I avoid globals as well, but I disagree with your statement: That exact same dynamic happens anyway, even with private member variables and accessors/mutators. You still need to either search, or have your IDE find out for you where such access/mutation occurs. So you're looking for `setCrazyValue()` instead of `crazyValue`. So what? Now if instead you're talking about how it's nearly impossible to protect such direct access (especially in an MT environment), then that's a different story and reason. –  May 01 '15 at 14:25

1 Answers1

2

more goto statements in your code less readable would be your code, my opinion

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40
  • 3
    Same goes for functions. Proper use with `goto` does not harm readability, and I've seen codes. – Zippo Mar 08 '12 at 16:46