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'
A label should have an uppercase first letter and all the other internal words should begins with lowercase. Labels should not end with a period unless they end with three periods, for example: “New…”, “Add…”. You should not use text constants (for example “%1 - %2”) to format labels.
Explanation. A label is an identifier followed by a colon ( : ) and a statement (until C23). Labels are the only identifiers that have function scope: they can be used (in a goto statement) anywhere in the same function in which they appear.
In C a label identifies a statement in the code. A single statement can have multiple labels. Labels just indicate locations in the code and reaching a label has no effect on the actual execution.
In batch files I often use HELL.
Like:
some_command || GOTO HELL
...
HELL:
echo "Ouch, Hot!"
My label names almost always fall into one of these patterns:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With