Java Coding standard / best practices - labeled break/continue

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?

-------------Problems Reply------------

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

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.

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.

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.

I know, I should not use labels.

But just assume, I have some code, that could gain a lot in readability from labeled breaks, how do I format them.

Mo, your premise is wrong. The question shouldn't be 'how do I format them?'

Your question should be 'I have code that has a large amount of logic inside loops - how do I make it more readable?'

The answer to that question is to move the code into individual, well named functions. Then you don't need to label the breaks at all.

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.

wrt sadie's code example:

You gave

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

As an example. You make a good point. My best guess would be:

public void lookMumNoLabels() {
for (...) {
// some code
doMoreInnerCodeLogic(...);
}
}

private void doMoreInnerCodeLogic(...) {
for (...) {
// some code
if (...) return;
}
}

But there would be examples where that kind of refactoring doesn't sit correctly with whatever logic you're doing.

As labels are so rarely useful, it appears, that there is no clear convention. The Java language specification has one example with labels and they are in non_cap.

But since they are so rare, in my opinion it is best, to think twice whether they are really the right tool.

And if they are the right tool, make them all caps so that other developers (or yourself later on) realize them as something unusual right away. (as Craig already pointed out)

The convetion/best practise would still be not to use them at all and to refactor the code so that is more readable using extract as method.

They are kind of the goto of Java - not sure if C# has them. I have never used them in practice, I can't think of a case where avoiding them wouldn't result in much more readable code.

But if you have to- I think all caps is ok. Most people won't use labelled breaks, so when they see the code, the caps will jump out at them and will force them to realise what is going on.

Mo, your premise is wrong. The question shouldn't be 'how do I format them?'

Your question should be 'I have code that has a large amount of logic inside loops - how do I make it more readable?'

The answer to that question is to move the code into individual, well named functions. Then you don't need to label the breaks at all.

Ok, I'll give up on this one. But I will open a new question with a more sepcific code problem and how to refactor it in order to avoid the labels.

Category:java Views:0 Time:2008-08-19

Related post

  • Are there any suggestions for developing a C# coding standards / best practices document? 2008-08-18

    I'm a recent AI graduate (circa 2 years) working for a modest operation. It has fallen to me (primarily as I'm the first 'adopter' in the department) to create a basic (read useful?) C# coding standards document. I think I should explain that I'm pro

  • Assembly Coding Standards / Best Practices 2010-01-31

    I've know 8086 Assembly and now I'm learning MIPS Assembly by reading the books MIPS Assembly Language Programming and See MIPS Run, but I never stopped to think about the coding standards/best practices of Assembly. I want to turn me in a better dev

  • Correction depends on Java Coding Standard 2010-07-02

    I use CheckStyle Plug-in for make my codes fit Java Coding Standard.The programme has lacking ability as the programme doesn't correct the errors on-time.I want to a programme which make my code correct on-time.Suppose that,I'm writing code then the

  • Is there a Java coding standards? If so, is there a tool that will check for those standards? 2010-11-16

    Are there Java coding standards? If so, is there a tool that implements and checks for those standards? I'm not familiar but hoping that someone has used Sonar, Checkstyle, PMD, Findbugs, Clover, and/or Cobertura that could tell me if any of those to

  • Link to Microsoft coding standard/ best practices documentation? 2010-11-24

    can anyone post the link to the Microsoft development best practices and guidelines? i have searched high and low for this and cant seem to find the resource i am looking for. In particular i am lookign for the development guidelines Microsoft publis

  • Java Coding Standards - all variable names in lower case 2013-01-01

    We are generating Entity Classes from database tables using Netbeans utility, now here we have ability to do it vise-a-verse. Like from Table to Entity POJOs and reverse, so that when we make any change in Entity POJO - we will get updated SQL. Eclip

  • Java coding standards and multiple IF's 2011-02-16

    I have the following lines of code: if( checker.this()==false || checker.that()==false || checker.what()==true|| checker.cool()==false || checker.damm()==true (...) ) { option = Option.FALSE; } With about 20 checks that must be performed. I've found

  • Ext js coding standards/ best practice 2011-08-20

    Can anyone share ext js coding standards? Is it worth enough to have two versions of js files - one during development and another compressed version for production environment? Also can any one suggest on the number of JS files we can have. I read s

  • Labeled-break/continue in C# or Fortran 95 loops? 2012-04-21

    I've been tasked in a homework assignment with converting a loop in C# into Fortran 95. outerLoop: for(row = 0; row < numRows; rows++){ for(col = 0; col < numCols; col++){ if(mat[row][col] == 0) continue outerLoop; sum += mat[row][col]; } } As

  • java coding standard for garbage memory support 2011-03-12

    simpleClass sc = new simpleClass(); sc.getObject(); ... simpleClass.java class simpleClass { static int st = 0; public Integer getObject() { Integer i = 10; Integer j = 10; Integer result = 10; try { result = getSum(i, j); } catch (Exception ex) { ex

  • Do you have any good advice/links to a set of coding standards or best practices to follow? 2009-02-20

    For those of us that have programmed enough I’m sure we have come across many different flavours of coding standards that you can use when it comes to programming. e.g. http://msdn.microsoft.com/en-us/library/ms229042.aspx You might derive your codin

  • Is there a coding standard for Java? 2010-09-01

    I can develop an application/project. But that is not in the correct coding standard. it takes large memory and others can not be understand it easily. What the General Standard to for coding. I have a question like how to name the variables methods

  • Coding standards and best programming practices for C# 2011-05-02

    I am looking for some decent coding standards and best programming practices for C#? The same would go for ASP.NET and ASP.NET MVC. --------------Solutions------------- refer to this article already answered: ) C# Coding standard / Best practices Als

  • Coding standards for VB.NET 2010-10-25

    Possible Duplicate: C# Coding standard / Best practices What VB.NET or C# coding standards are you using for your .NET applications? I was looking at hungarian notation and I don't think it is a good idea for a .NET application. Any suggestions? Than

  • Besides "treat warnings as errors" and fixing memory leaks, what other ideas should we implement as part of our coding standards? 2009-11-05

    First let me say, I am not a coder but I help manage a coding team. No one on the team has more than about 5 years experience, and most of them have only worked for this company.. So we are flying a bit blind, hence the question. We are trying to mak

  • Coding guidelines + Best Practices? 2010-05-07

    I couldn't find any question that directly applies to my query so I am posting this as a new question. If there is any existing discussion that may help me, please point it out and close the question. Question: I am going to do a presentation on C# c

  • Are there any widespread, modern Java coding conventions? 2009-09-01

    Sun's "Code Conventions for the Java Programming Language" was last updated April 1999. Ten years later a lot has changed in the language, as well as general usage patterns. Are there more up to date, widely adopted standards? Most guidelines omit sp

  • What is the best practice/coding standard with regard to the "this" scope is AS3? 2009-03-06

    What is the best practice/coding standard with regard to the "this" scope is AS3? Is there one? I feel it really helps with standardization and my readability, but sometimes it seems like "too much". For instance, is the use of "this" in the followin

  • "Common Sense" or Practical .NET Coding standards 2009-05-08

    I'm about to undertake updating our coding standards documentation and I am thinking of perhaps writing them at a somewhat higher level than is commonly done. We have a fair amount of developers that come and go, and they are generally of an intermed

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.139 (s). 11 q(s)