-4

I read in the book I am learning C++ from about loops and it taught be goto first, but near the end it said not to use it, as it was bad practice, and said it created 'spaghetti' code. I don't understand what that means and why it is bad. Could someone please explain?

Theroarx
  • 73
  • 2
  • 9
  • 2
    The classic paper on the subject: [Go To Statement Considered Harmful](http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html) by Dijkstra (of shortest-path algorithm fame, among others), 1968 – Igor Tandetnik Oct 31 '17 at 02:00
  • Because there is much more secure and readable way to do things! If you have to do it, it's a red flag for a really bad design! – Maxime Gélinas Oct 31 '17 at 02:01
  • @MaximeGélinas A blanket dismissal of goto is also unhealthy. There are _some_ valid uses of gotos, namely breaking out of n-nested loops. – Carey Oct 31 '17 at 02:07
  • Of course, the n-nested loops can often be moved out to a separate function, and then you can use the return keyword to 'break out' of them; so even then goto isn't the only option. – Jeremy Friesner Oct 31 '17 at 02:18
  • *I don't understand what that means and why it is bad. Could someone please explain?* -- Well one thing, if you post a question about code that doesn't work, and it is full of `goto` statements, you've reduced your chance of persons helping you by about 95 - 99%. An experienced programmer just won't waste their time trying to figure out spaghetti code. – PaulMcKenzie Oct 31 '17 at 02:19

1 Answers1

0

Spaghetti is a code poorly structured and which makes it hard to update because of multiple undocumented and unexpected links or dependencies. You touch it in one place and you see other things get broken or modified in an unexpected way. Just like sticky spaghetti, you pull one end and you see a number of places start moving. GOTO usually violates the principles of structured, procedural programming, hence the term, which suggests the tangled and arbitrary nature of the program flow.

Aderbal Farias
  • 989
  • 10
  • 24