0

In C, C++ and some dialects of BASIC, goto labels are declared with the syntax label:. I'm working on a language that uses name: type as the syntax for variable declarations, so I'd prefer if possible to use something a bit more distinctive for labels. I am or at some point was passingly familiar with many languages that use that syntax for variable declarations, but off the top of my head I can't recall a single one of them that doesn't ban goto (not looking to argue about whether that's a good or bad design decision, just noting it did take place in those cases), so I'm trying to figure out where to look for precedent on labels.

What other forms of syntax has been used for goto labels? Are there any criteria I might be overlooking in choosing one over another?

(I just remembered Ada is an exception. But it uses <<label>>, which doesn't mix well with the use of those symbols as C-style shift operators.)

rwallace
  • 1,198
  • 11
  • 19
  • What is the context of your question. Are you writing a programming language? – mattnz Feb 24 '12 at 00:18
  • I can't recall a single one of them that doesn't ban goto: Steve McConnell, Linus Torvalds, MISRA are just a few that take the view that GOTO is a useful language feature, who am I to argue with that amount of combined wisdom? – mattnz Feb 24 '12 at 00:21
  • 4
    http://xkcd.com/292/ – Zenon Feb 24 '12 at 00:39
  • 3
    "What other syntax has been used for goto labels"? How about just line numbers? – user16764 Feb 24 '12 at 00:43
  • @mattnz, yes. user16764, yes, I remember those, but they would be ambiguous in a grammar that allows an expression as a statement :) – rwallace Feb 24 '12 at 01:00
  • @mattnz: A person with his own independent opinion? Linus is a raging moron, for example. – DeadMG Feb 25 '12 at 00:41
  • There is precedent for using both `name: type` for variables and `label:` for labels in the same language - Pascal. This is somewhat helped by the fact that you can't declare variables inline; you have to do it in a `var` section - hence, it is easy to know if it's one or the other. – Michael Madsen Feb 25 '12 at 01:17
  • 3
    Excuse me, but this is 2012. Dijkstra's original short piece on the problems created by the GOTO statement is over forty (40) years old. Bohm & Jacopini's formal proof of its superfluousness is almost as old. Don Good's GYPSY language (late 1970s to early 1980s) demonstrated that significant applications could be developed, and formally proved correct (proof later confirmed by testing), entirely without GOTO statements. BLISS likewise had no GOTO (although it did have other things.) GOTO statements are UNNECESSARY. Why do you insist on messing up your language with them? (Yes, I rant.) – John R. Strohm Feb 25 '12 at 03:27
  • 3
    "GOTO statements are UNNCESSARY." So is for, while, do-while, if and if-else. Switch and goto is all you need for control flow. :-) – Daniel Lubarov Feb 25 '12 at 07:30
  • 2
    @Daniel: Nah. In "modern" programming languages, you can do flow control with exceptions and try/catch ;) – back2dos Feb 25 '12 at 13:07
  • 1
    @JohnR.Strohm: Anything that can be done with `goto` can be done with flags, and there are cases where flags are the "right" approach, but there are other cases where flags may rightly be derided as "goto in disguise". If the purpose of setting a flag is simply to skip over everything before a certain piece of code, it would be better to simply jump there. – supercat Mar 27 '14 at 18:37

6 Answers6

5

DOS batch scripts use :label.

// I personally would use >label. :)

Fixpoint
  • 742
  • 5
  • 9
  • You know, I actually quite like `>label`. I'm curious, did you get it from anywhere in particular, or was it off-the-cuff? – rwallace Feb 24 '12 at 23:01
  • Nowhere particular, I just feel that `>` invokes thoughts of "start from here". – Fixpoint Feb 24 '12 at 23:56
4

You could make label a keyword and use something like:

label foo;

or

LABEL FOO;

if you want it to stand out more.

But as Mitch says, it really depends on what the rest of your language's syntax looks like.

There's an argument to be made that the label syntax shouldn't fit in cleanly with the rest of the language, so that goto labels stand out more. (That was the rationale for Ada's <<LABEL>> syntax.)

Keith Thompson
  • 6,402
  • 2
  • 29
  • 35
3

If you must use GOTO statements, even knowing that they are unnecessary and an open invitation to mess up your programs, then do what Ichbiah et al (the original designers of Ada) did: MAKE THE SYNTAX UGLY.

Make it stick out like a giant thumb with a really hideous fungal infection that has been smashed with a hammer. Make it EASY for the maintenance programmers to see it, so they will know that the author of the code they are asked to maintain was a total cretin, who needed to be forcibly transferred to the business school, rather than be allowed to matriculate in computer science, so they will be on the lookout for the not-quite-as-obvious idiot mistakes that are bound to be festering all through his alleged code.

Ichbiah admitted they did this, in so many words. The DoD specification for the language explicitly required that it contain a GOTO statement, because the DoD believed (wrongly, in my personal opinion) that the case against it had not been made solidly enough. They couldn't NOT put in a GOTO, but they could and did make it UGLY, to discourage its use. Most places that used Ada outlawed its use immediately, or at least required management signoff on each and every GOTO, to encourage programmer to find a GOTO-less alternative.

Something like ###--->>>LABEL<<<---### would work. The label must be on a line, with massive "decoration", all the way up against the left margin, and ALL BY ITSELF.

Or maybe something like --->>>LABEL ... HEREBEDRAGONS, where "HEREBEDRAGONS" is a reserved word in the language, and any variation on HEREBEDRAGONS that is not all uppper case is defined to be a fatal syntax error. Make it HARD and PAINFUL for the programmer to use it.

John R. Strohm
  • 18,043
  • 5
  • 46
  • 56
  • 1
    I actually like this philosophy not because I don't think `goto` is ever appropriate, but rather because I regard it as shouting **CONTROL FLOW DOES NOT MATCH CONVENTIONAL PATTERNS**. Wanting `goto` targets to appear nice and sedate would be like wanting a fire alarm to play pleasant music. Fire alarms should be unpleasant not because they shouldn't exist, but because on those rare occasions when their use is appropriate, they deserve attention. – supercat Mar 27 '14 at 18:40
0

I am not sure where I am getting this from, but somehoew @<label>: feels natural to me.

But I also suspect the 'right' answer depends on what else is going on in your new language, syntax-wise.

Mitch Haile
  • 161
  • 3
0

How about [label]? For example,

Stack<Solution> partialSolutions = new Stack<Solution>();
partialSolutions.add(initialSolution);

[weirdSearch]
if (partialSolutions.isEmpty()) {
    acceptedSolution = null;
    goto cleanup;
}

[weirdSearchSkipEmptyCheck]
Solution sol = partialSolutions.pop();
switch (rand() % 3) {
    [0] // accept this
    acceptedSolution = sol;
    goto cleanup;

    [1] // reject this and try the next solution
    goto weirdSearch;

    [2] // try a derived solution
    partialSolutions.push(sol.deriveAnotherSolution());
    goto weirdSearchSkipEmptyCheck;
}

[cleanup]
free(stack);

I don't know of any language that uses this syntax, though it reminds me of sections in INI files.

Some other bad ideas:

<label>
.label.
:label:
*label*
>label<

These should all be easy to parse if your language is Java-like, since no other statement can begin with an infix operator.

Daniel Lubarov
  • 1,226
  • 8
  • 12
0

JavaScript does not have goto but it has labels:

outer:for(var i=0;i<10;++i) {...}

and yet it has:

{ outer:"value" }

and yet you can find there:

some? outer:"value";

In JavaScript 2.0 typed variables were added:

var some:int;

All this does not create any conflicts as ':' appears in different contexts.

  • 1
    True. But if the language allows declarations without `var`/`val`/`auto`, there could be an ambiguity: `local: type;` vs `label: local;`. – Daniel Lubarov Feb 25 '12 at 08:06