Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 413
dr. evil Avatar asked Mar 28 '09 19:03

dr. evil


People also ask

What is the naming conventions of label?

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.

Are GOTO labels scoped?

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.

How do you define a label in C?

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.


4 Answers

In batch files I often use HELL.

Like:

some_command || GOTO HELL

...

HELL: 

echo "Ouch, Hot!"
like image 172
Assaf Lavie Avatar answered Oct 05 '22 01:10

Assaf Lavie


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
like image 38
finnw Avatar answered Oct 05 '22 01:10

finnw


  • "cleanup" if it stand before freeing some previosly allocated resources (or similar kind of 'finally' section work)
like image 38
jonny Avatar answered Oct 05 '22 02:10

jonny


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.

like image 28
Stefano Borini Avatar answered Oct 05 '22 03:10

Stefano Borini