Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using a 'goto' statement bad?

Tags:

c#

loops

break

goto

After doing some reseach on how to break through a secondary loop

while (true) { // Main Loop    for (int I = 0; I < 15; I++) { // Secondary loop        // Do Something        break; // Break main loop?    } } 

most people recommended to call the 'goto' function
Looking as the following example:

while (true) { // Main Loop    for (int I = 0; I < 15; I++) { // Secondary Loop        // Do Something        goto ContinueOn; // Breaks the main loop    } } ContinueOn: 

However; I have often heard that the 'goto' statement is bad practice. The picture below is perfectly illustrating my point: Series found

So

  • How bad is the goto statement really, and why?
  • Is there a more effective way to break the main loop than using the 'goto' statement?
like image 818
Rasmus Søborg Avatar asked Aug 10 '12 16:08

Rasmus Søborg


People also ask

Why we should not use goto statement?

NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.

Why is goto frowned upon?

The reason that many programmers frown upon the use of goto is that with the unrestrained use of goto statements, it is easy to create a program with undefined program flow, which can never be debugged.

Is it bad to use goto in C++?

In modern programming, the goto statement is considered a harmful construct and a bad programming practice. The goto statement can be replaced in most of C++ program with the use of break and continue statements.


1 Answers

EDIT:

How bad is the goto statement really, and why?

It depends on the exact situation. I can't remember any time where I found it made the code more readable than refactoring. It also depends on your personal view of readability - some people dislike it more than others, as is clear from the other answers. (As a point of interest, it's widely used in generated code - all of the async/await code in C# 5 is based on effectively a lot of gotos).

The problem is that situations where goto tends to be used tend to be the kind of situations where refactoring aids things anyway - whereas goto sticks with a solution which becomes harder to follow as the code gets more complicated.

Is there a more effective way to break the main loop than using the 'goto' statement?

Absolutely. Extract your method out into a separate function:

while (ProcessValues(...)) {     // Body left deliberately empty }  ...  private bool ProcessValues() {    for (int i = 0; i < 15; i++)    {        // Do something        return false;    }    return true; } 

I generally prefer doing this over introducing an extra local variable to keep track of "have I finished" - although that will work too, of course.

like image 96
Jon Skeet Avatar answered Sep 23 '22 03:09

Jon Skeet