Question

How do you name your GoTo labels? I use rarely often so I'm having a hard time finding good names.

Please refrain from the classical 'goto is evil and eat your code alive discussion'

Was it helpful?

Solution

My label names almost always fall into one of these patterns:

  • Called "restart", for restarting a set of nested loops because a change has invalidated something
  • Called "exit" or "return", right before the return statement, and is only there because of a trace statement that logs the return value for debugging
  • Has the same name as the boolean variable that it replaces

OTHER TIPS

In batch files I often use HELL.

Like:

some_command || GOTO HELL

...

HELL: 

echo "Ouch, Hot!"
  • "cleanup" if it stand before freeing some previosly allocated resources (or similar kind of 'finally' section work)

In fortran, I use goto for rollbacks, and I normally start from 999 backwards (in fortran, goto labels are only numeric)

    call foo(err)
    if (err /= 0) goto 999

    call bar(err)
    if (err /= 0) goto 998

    call baz(err)
    if (err /= 0) goto 997

    ! everything fine
    error = 0
    return

997 call undo_bar()
998 call undo_foo()
999 error = 1
    return

I also use labels greater than 1000 if for some reason I want to skip the rollback part.

In C and other languages, I would use rollbacknumber (eg. rollback1, rollback2), so it's clear from the label that you are going to rollback. This is basically the only good reason for using goto.

I usually only need it for 2 cases. As such, my goto labels are either begin or finally.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top