Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.)

share|improve this question
3  
xkcd.com/292 –  Zenon Feb 24 '12 at 0:39
3  
"What other syntax has been used for goto labels"? How about just line numbers? –  user16764 Feb 24 '12 at 0:43
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 3:27
2  
"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 Feb 25 '12 at 7: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
show 6 more comments

6 Answers

up vote 5 down vote accepted

DOS batch scripts use :label.

// I personally would use >label. :)

share|improve this answer
    
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
    
Makes sense. I'll probably go with that. Thanks! –  rwallace Feb 25 '12 at 14:04
add comment

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.

share|improve this answer
    
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 at 18:40
add comment

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.)

share|improve this answer
add comment

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.

share|improve this answer
    
Yeah, I'm using @foo for attributes, Java style, otherwise I'd give that serious consideration. –  rwallace Feb 24 '12 at 1:10
add comment

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.

share|improve this answer
1  
True. But if the language allows declarations without var/val/auto, there could be an ambiguity: local: type; vs label: local;. –  Daniel Feb 25 '12 at 8:06
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.