Why is not advisable to use ‘goto’




















Control can be transferred out of or within a compound statement, and control can be transferred to the beginning of a compound statement. However the control cannot be transferred into a compound statement. Thus one must take care not to use too much of goto statements in their program or in other words use it only when needed. This is because C being a highly structured language one must take care not to use too much of these unconditional goto branching statements which makes the program a poor way of coding.

The goto statement is discouraged in C, because it alters the sequential flow of logic that is the characteristic of C language. Additional errors will not be handled. Unless you have a different catch block somewhere in the routine, or maybe one somewhere at a higher scope level. When that happens, good luck debugging see "Confusing Code" above. A catch block should usually perform a recovery and then return out of the routine rather than try to jump somewhere else in the routine.

In addition to the examples above, there are a handful of scenarios where using GOTO might sound like the best or easiest thing to do. The good news is that in programming there is almost always more than one way to do the same thing, so there is a good chance that there is an alternative to using GOTO.

Unfortunately, though, it only exits the current loop, which isn't helpful in the case where you have multiple loops nested within each other. This is what it looks like with and without a goto. Note that the second example contains a special flag, just to indicate that you need to break from each loop.

A better solution: Why not just move the looping code into its own subroutine? You may need to pass a handful of arguments, such as the contents of the loop, but it makes the code simpler. Now, to exit the loops, simply return from the subroutine. A common use of GOTO is to perform a "common exit," or in other words always call a certain section of code before returning from a routine. Perhaps this is done to reset a variable or to close any file units opened in the routine.

Because they lead to spaghetti code. In the past, programming languages didn't have while loops, if statements, etc. It lead to an unmaintainable mess. That's why the CS gods created methods, conditionals and loops. Structured programming was a revolution at the time. Nothing is wrong with goto if it is used properly. The reason it is "taboo" is because in the early days of C, programmers often coming from an assembly background would use goto to create incredibly hard-to-understand code.

Most of the time, you can live without goto and be fine. There are a few instances, however, where goto can be useful. The prime example is a case like:. Using a goto to jump out of a deeply-nested loop can often be cleaner than using a condition variable and checking it on every level.

Using goto to implement subroutines is the main way it is abused. This creates so-called "spaghetti code" that is unnecessarily difficult to read and maintain.

There is nothing wrong with goto in itself. It's a very useful construct in programming and has many valid uses. The best that comes to mind is structured resource freeing in C programs. Where goto's go wrong is when they are abused. Abuse of gotos can lead to thoroughly unreadable and unmaintainable code. In , Edsger Dijkstra wrote a famous letter to the editor of Communications of the ACM GOTO is considered harmful in which he laid out the case for structured programming with while loops and if When GOTO is used to substitute for these control structures, the result is very often spaghetti code.

Pretty much every programming language in use to day is a structured programming language, and use of GOTOs has been pretty much eliminated. In fact, Java, Scala, Ruby, and Python don't have a goto command at all.

But generally its use should be restricted to specific design patterns that call for it in a controlled and recognized way. The founder of the anti-goto movement is Edsger Dijskstra with his legendary "Goto Considered Harmful". To get you started you can goto ha ha!

Whether you should or shouldn't use it is long-standing religious war. Including goto was part of the effort to reach that goal. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?



0コメント

  • 1000 / 1000