DEV Community

Discussion on: Is goto all that bad?

Collapse
 
polterguy profile image
Thomas Hansen

Goto results in more complex code where it becomes more difficult to determine the potential execution paths of your code. Consider the following pseudo code

var foo = 0;
while(foo++ < 20) { /* ... Do stuff ... */}
Enter fullscreen mode Exit fullscreen mode

And compare it to the following.

var foo = 0;
start:
/* ... do stuff ... */
if (++foo < 20) { goto start; }
Enter fullscreen mode Exit fullscreen mode

Semantically they do the same thing, but one is more declarative in style. Then imagine having multiple layers of nested goto statements, etc - The thing pretty rapidly turns in "spaghetti" ...