Java and labels...

So I just realized that in my whole game, I used a label only once.

Does anyone actually use them? Is it a sign of bad code to use them?

Well I think it’s a matter of opinion. I don’t really use them in games because I prefer drawing text with drawString(). I really only use Labels for non-game GUI, what are you using them for.

he means:


outer: while(....)
{
    while(...)
    {
        ...

        if(...)
           break outer;

        ...
    }
}

or


outer: if(...)
{
   ...

   if(...)
      break outer;

   ...
}

‘outer’ is a label.

xD my bad, I totally misunderstood that. Thanks for pointing that out Riven… Well I don’t really use those kind of labels at all so I’ll let someone else answer…

I never used them. I was teached to not them… But if labels exit, they must not be that bad ;D

In general, you should avoid them but in some case they come in handy (in my opinion).

I use break and continue occasionally, but no labels

keep everything simple.

You can turn complex functionality into simple code, using labels.

can also have the reverse effect. Some features of Java I just consciously avoid.

Anyway continue and even more so break are bad style (or so the general consent is)
let alone goto, which I haven’t even seen in java code before

maybe because it doesn’t compile? :persecutioncomplex:

well that’s good =D

I use them infrequently, but they’re useful for switch statements inside a while loop amongst other things.

ie.



loop: while (true) {
    ...
    switch (var) {
        case x:
             // do something
             break;
        case y:
             // do something else
             break;
        case z:
            // do something incredibly different
             break;
        case EXIT:
              break loop;
    }
   ...
}


Obviously, that’s pseudo-code! :slight_smile:

yes, that’s actually the only place i used it, in a for-loop sort of the same concept as nesting a switch statement in a while-loop (if the array is full, it would move onto the code that would grow the array):

		if (drawing.clipped) {
			label: synchronized (this) {
				objects++;
				for (int i = 0; i < map.length; i++) {
					if (map[i] == null) {
						map[i] = drawing;
						break label;
					}
				}
				int len = map.length;
				Drawing[] temp = new Drawing[len * 2];
				System.arraycopy(map, 0, temp, 0, len);
				map = temp;
				map[len] = drawing;
			}
		}

My Java professor disapproved of labels because of “bad style”. However my rule is “if it works, it’s good”.

Alot of the “bad style” mentality stems from pre 80s compiler design issues where labels (and goto statements) totally mucked with what the compiler could do…much of this is no longer the case. This is not to say either should be overused, but if you’d need to jump-through-hoops or introduce some (otherwise useless) state variables to avoid coding something what would be cleaner with label/gotos…you’re probably doing it wrong.

I never use label, duo of break and continue is enough. And if I ever have to choose, I’ll take “break” cause it’s useful and always sounds good :slight_smile:

It can also make programs hard to follow since your jumping around a lot, hence why it is sometimes called spaghetti code.

To be honest I wouldn’t use labels unless you can’t find a way to do what you want to do without them (at least not easily), the trick is to not use a lot of them because then that becomes spaghetti like code :wink:

Re-read what I wrote…it should be easy since you quoted it.

I did read what you wrote, I was adding the information about spaghetti code, and then giving my opinion on the use of labels… Only the first sentence was in reply to your post, I didn’t separate it correctly from my opinion. I was agreeing with you and adding information on where the “bad style” mentality comes from, so why do I need to re-read your post.

Duely noted. :wink: Ultimately pretty much any programming construct can be misused and produce bad and/or hard to read code. An unfortunate side-effect of this is that some very useful constructs get a bad-rap. The main place where labels are very useful (that I can think of off the top of my head) is state-machine like processing. Examples include core stream input/output and interpreters.

I didn’t even now this was possible… I’ll try it out the next time I need to break a nested loop. :slight_smile: