dr. evil
dr. evil

Reputation: 27265

Naming conventions for GoTo labels

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'

Upvotes: 9

Views: 3615

Answers (5)

Stefano Borini
Stefano Borini

Reputation: 143795

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.

Upvotes: 2

rlbond
rlbond

Reputation: 67749

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

Upvotes: 0

jonny
jonny

Reputation: 736

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

Upvotes: 2

finnw
finnw

Reputation: 48619

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

Upvotes: 6

Assaf Lavie
Assaf Lavie

Reputation: 75973

In batch files I often use HELL.

Like:

some_command || GOTO HELL

...

HELL: 

echo "Ouch, Hot!"

Upvotes: 6

Related Questions