Java Coding standard / best practices - naming convention for break/continue labels

22,121

Solution 1

If you have to use them use capitals, this draws attention to them and singles them out from being mistakenly interpreted as "Class" names. Drawing attention to them has the additional benefit of catching someone's eye that will come along and refactor your code and remove them. ;)

Solution 2

I don't understand where this "don't use labels" rule comes from. When doing non-trivial looping logic, the test to break or continue isn't always neatly at the end of the surrounding block.

outer_loop:
for (...) {
  //  some code
  for (...) {
    //  some code
    if (...)
      continue outer_loop;
    //  more code
  }
  //  more code
}

Yes, cases like this do happen all the time. What are people suggesting I use instead? A boolean condition like this?

for (...) {
  //  some code
  boolean continueOuterLoop = false;
  for (...) {
    //  some code
    if (...) {
      continueOuterLoop = true;
      break;
    }
    //  more code
  }
  if (continueOuterLoop)
    continue;
  //  more code
}

Yuck! Refactoring it as a method doesn't alleviate that either:

boolean innerLoop (...) {
  for (...) {
    //  some code
    if (...) {
      return true;
    }
    //  more code
  }
  return false;
}

for (...) {
  //  some code
  if (innerLoop(...))
    continue;
  //  more code
}

Sure it's a little prettier, but it's still passing around a superfluous boolean. And if the inner loop modified local variables, refactoring it into a method isn't always the correct solution.

So why are you all against labels? Give me some solid reasons, and practical alternatives for the above case.

Solution 3

The convention is to avoid labels altogether.

There are very, very few valid reasons to use a label for breaking out of a loop. Breaking out is ok, but you can remove the need to break at all by modifying your design a little. In the example you have given, you would extract the 'Lots of code' sections and put them in individual methods with meaningful names.

for ( ;/*stuff*/; ) 
{
    lotsOfCode();

    if ( !isEnough() )
    {
        moreCode();
    }
}

Edit: having seen the actual code in question (over here), I think the use of labels is probably the best way to make the code readable. In most cases using labels is the wrong approach, in this instance, I think it is fine.

Solution 4

Sun's Java code style seem to prefer naming labels in the same way as variables, meaning camel case with the first letter in lower case.

Solution 5

The convention I've most seen is simply camel case, like a method name...

myLabel:

but I've also seen labels prefixed with an underscore

_myLabel:

or with lab...

labSomething:

You can probably sense though from the other answers that you'll be hard-pushed to find a coding standard that says anything other than 'Don't use labels'. The answer then I guess is that you should use whatever style makes sense to you, as long as it's consistent.

Share:
22,121
Mo.
Author by

Mo.

Berlin native. Bit herder. Code connoisseur. Coffee lover.

Updated on July 20, 2022

Comments

  • Mo.
    Mo. almost 2 years

    Sometimes a labeled break or continue can make code a lot more readable.

    OUTERLOOP: for ( ;/*stuff*/; ) {
        //...lots of code
    
        if ( isEnough() ) break OUTERLOOP;
        //...more code
    }
    

    I was wondering what the common convention for the labels was. All caps? first cap?