6

So I've seen a lot of cases where labels are being used in for loops to break out, especially with doubly nested loops.

Is this the only case where they can be used? or are there other common uses that I just don't know about. I feel like its a java tool I've never used!

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Jay Smith
  • 471
  • 3
  • 17
  • 3
    It's used very sparingly due to programming languages' history of abuse of `goto` statements which can lead to "spaghetti" code, code that is hard to follow and impossible to debug. – Hovercraft Full Of Eels May 15 '13 at 01:38
  • 2
    IMHO labels show poor design and laziness on the part of developer using them. They should be avoid at all costs, particular for all the reasons stated by HFOE – MadProgrammer May 15 '13 at 01:39
  • 1
    Goto is non functional in Java. Maybe they'll add it in Java 10, in memoriam of Dijkstra. – Shrein May 15 '13 at 01:43
  • So besides for loops, they have no use? and even then I probably shouldn't use? – Jay Smith May 15 '13 at 01:45
  • In a couple decades of programming (several years in Java), I can count on 1 hand the number of times I have seen labels used correctly. And in Java they were all to do with breaking out of loops. Even then they made me groan, and I am sure that the code would have been cleaner (easier to debug and to read) without them if I only had the time to clean it up (remove them). They can be used anywhere but they make debugging difficult and you kill 5 kittens whenever you use one. ;) – Shadow Man May 15 '13 at 01:46

2 Answers2

9

It is fair to say that labeling a loop is the only useful way that a labeled statement can be used.

You are allowed to label any statement in Java, but the only way they are used is as a target of a (labeled) break or continue statement.

The usual advice applies though: just because you can do something does not mean you should. Don't use labels as funny "comments" (yes I have seen this done). Don't write doubly nested loops just to show off your ability to break or continue an outer loop. These days we tend to favor operations that apply to a whole sequence, mitigating some uses for labeled breaks and continues. As always, aim for readability and simplicity. That said, tools like parser generators that output Java source are quite likely to make heavy use of labeled statements, even if human programmers rarely do.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • so its more of a useful thing for code generation? I just was wondering why I was missing out on a useful java tool, but it sounds like I was right just not doing anything with them! – Jay Smith May 15 '13 at 01:50
  • That's fair. You _can_ use them in some cases; the classic example is probably Rubin's "find the first row in a matrix that contains all zeros." Here as you are scanning the items within a row, when you find a non-zero element, you continue the outer loop which you have labeled. That IMHO is an acceptable use; maybe a bit contrived, but it gives you the idea. In a functional programming language we have higher-order operators like `every` and `some` and `map` and `filter` so these kinds of things are easier to write. But in Java, once in a great while, it is okay IMHO. – Ray Toal May 15 '13 at 01:55
5

Labelled break and continue are essentially goto statements.

From https://stackoverflow.com/a/6373262/895245: forward goto:

label: {
  // do stuff
  if (check) break label;
  // do more stuff
}

Backward goto

label: do {
  // do stuff
  if (check) continue label;
  // do more stuff
  break label;
} while(true);

So this question comes down to the never ending debate of when goto statements are legitimate, e.g.: Examples of good gotos in C or C++ , with the added fact that the backwards goto idiom above is even uglier than a direct GOTO.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985