DEV Community

nourdude
nourdude

Posted on

goto's underrated

goto is absolutely underrated.

it isn't inherently bad. like any control-flow tool, it depends on how exactly you'd use it.

proper usage:

gotoif (error!=0) {do_cleanup}

// complete_operation

label do_cleanup
  sys.unlock_file(descriptor)
  sys.free(buffer)
  return
Enter fullscreen mode Exit fullscreen mode

this is pretty simple: if something goes wrong, go clean up.

messed-up usage:

@label b
goto a
@label c
goto b
@label a
goto c
// a forever do-nothing loop
Enter fullscreen mode Exit fullscreen mode

this is technically valid, but... it's confusing and is absolutely useless.
like- BRO I NEED A GRAPH TO SEE THAT THIS THING IS A LITERAL INFINITE JUMP LOOP!

what i'm saying isn't "always use goto." nor is it "never use goto."

it's that goto itself isn't the problem. bad control flow is.

and when you call it a problem because dijkstra said it? without any defensible argument? dogma.

for example, i could frame while as bad:

while (x) {
 while (y) {
  do z
  while (a) {
  }
  do f
 }
 eat orange
}
Enter fullscreen mode Exit fullscreen mode

or if, or for, or whatever i want to.
it's not the tool. it's the usage.

Top comments (0)