Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I avoid using Java Label Statements?

Tags:

java

loops

Today I had a coworker suggest I refactor my code to use a label statement to control flow through 2 nested for loops I had created. I've never used them before because personally I think they decrease the readability of a program. I am willing to change my mind about using them if the argument is solid enough however. What are people's opinions on label statements?

like image 463
Aaron Avatar asked Sep 05 '08 18:09

Aaron


People also ask

Why do we use label in Java?

A Label object is a component for placing text in a container. A label displays a single line of read-only text. The text can be changed by the application, but a user cannot edit it directly.

What is label statement in Java?

Java labeled blocks are logically similar to goto statements in C/C++. A label is any valid Java identifier followed by a colon. For example, outer: , inner: , customLabel: , label_ :. Table Of Contents.

Can loops have labels in Java?

What is a labeled loop in Java? A label is a valid variable name that denotes the name of the loop to where the control of execution should jump. To label a loop, place the label before the loop with a colon at the end. Therefore, a loop with the label is called a labeled loop.


2 Answers

Many algorithms are expressed more easily if you can jump across two loops (or a loop containing a switch statement). Don't feel bad about it. On the other hand, it may indicate an overly complex solution. So stand back and look at the problem.

Some people prefer a "single entry, single exit" approach to all loops. That is to say avoiding break (and continue) and early return for loops altogether. This may result in some duplicate code.

What I would strongly avoid doing is introducing auxilary variables. Hiding control-flow within state adds to confusion.

Splitting labeled loops into two methods may well be difficult. Exceptions are probably too heavyweight. Try a single entry, single exit approach.

like image 182
Tom Hawtin - tackline Avatar answered Oct 17 '22 12:10

Tom Hawtin - tackline


Labels are like goto's: Use them sparingly, and only when they make your code faster and more importantly, more understandable,

e.g., If you are in big loops six levels deep and you encounter a condition that makes the rest of the loop pointless to complete, there's no sense in having 6 extra trap doors in your condition statements to exit out the loop early.

Labels (and goto's) aren't evil, it's just that sometimes people use them in bad ways. Most of the time we are actually trying to write our code so it is understandable for you and the next programmer who comes along. Making it uber-fast is a secondary concern (be wary of premature optimization).

When Labels (and goto's) are misused they make the code less readable, which causes grief for you and the next developer. The compiler doesn't care.

like image 24
BIBD Avatar answered Oct 17 '22 11:10

BIBD