Should I remove the labels in java Or not?

450    Asked by KudoTaniguchi in Java , Asked on Oct 10, 2022

So I've been writing a program and I have some indented Loops and statements and I need to BREAK multiple on command. Rather than having a load of boolean variables and if(!) BREAK; statements throughout these loops and statements, what is everyone's opinion on using labels to break them using the BREAK

e.g.

 for(...) {
     indented_example_loops:     // Label name
           for(...) {
                for(...) {
                     if(match) break indented_example_loops;
                     // ^ Break statement to break the 2 for loops at once
                }
           }
 }

Perfectly okay? Okay to do it occasionally? Completely avoid?


Answered by Kumar Menon

It's all about readability and ease of understanding in terms of labels in java.


The easiest to understand loops are those with simple end conditions and no breaks at all. So if you can write your loops like that, do it.

while (someCondition) {

  // do stuff
}
Next comes the loop with a simple end condition and a single break:
while (someCondition) {
  // do stuff
  if (otherCondition) {
    break;
  }
  // maybe some more stuff
}

Everything that's more complex than this is a potential problem. A break with a label is always tricky, because you'll have to hunt for the label to find out which statement/block the break escapes from (i.e. the break will continue after the labelled block/statement).

The worst kind (the one you almost always want to avoid) is a nested loop where you break somewhere in the middle.while (someCondition) {

  myLabel:
  for (Foo foo : getAllFoos()) {
    for (Bar bar : foo.getBars()) {
      // do stuff
      if (someCondition(bar)) {
        break myLabel; // BAD!
      }
      // some more stuff
    }
  }
}

The problem here is that the actual flow of processing becomes really hard to follow. If you start having code like this, then you should definitely refactor it.

A common refactoring is to extract one (or more) nested loops into a method and turning the break into a return. This has the added benefit of giving a name to that piece of the code. If the name is chosen well, then that will help greatly in understanding the code.



Your Answer

Interviews

Parent Categories