Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java and labels...  (Read 2034 times)
0 Members and 1 Guest are viewing this topic.
Offline counterp

Senior Duke


Medals: 11



« Posted 2011-08-13 07:23:02 »

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?
Offline Z-Man
« Reply #1 - Posted 2011-08-13 13:41:42 »

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.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2011-08-13 13:43:50 »

he means:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
outer: while(....)
{
    while(...)
    {
        ...

        if(...)
           break outer;

        ...
    }
}


or

1  
2  
3  
4  
5  
6  
7  
8  
9  
outer: if(...)
{
   ...

   if(...)
      break outer;

   ...
}


'outer' is a label.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Z-Man
« Reply #3 - Posted 2011-08-13 13:47:28 »

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...
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #4 - Posted 2011-08-13 15:17:39 »

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

In general, you should avoid them but in some case they come in handy (in my opinion).
Offline Cero
« Reply #5 - Posted 2011-08-13 15:31:08 »

I use break and continue occasionally, but no labels

keep everything simple.

Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2011-08-13 15:33:03 »

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

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Cero
« Reply #7 - Posted 2011-08-13 16:23:38 »

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

Online Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2011-08-13 16:26:58 »

let alone goto, which I haven't even seen in java code before
maybe because it doesn't compile? persecutioncomplex

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Cero
« Reply #9 - Posted 2011-08-13 16:55:32 »

well that's good =D

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online nsigma
« Reply #10 - Posted 2011-08-13 17:53:50 »

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

ie.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
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!  Smiley

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline counterp

Senior Duke


Medals: 11



« Reply #11 - Posted 2011-08-13 19:52:57 »

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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
      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;
         }
      }
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #12 - Posted 2011-08-13 22:41:16 »

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

Offline Roquen
« Reply #13 - Posted 2011-08-14 00:00:38 »

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.
Offline ReBirth
« Reply #14 - Posted 2011-08-14 03:52:48 »

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 Smiley

Offline Z-Man
« Reply #15 - Posted 2011-08-14 04:02:40 »

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.
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
Offline Roquen
« Reply #16 - Posted 2011-08-14 05:42:29 »

Re-read what I wrote...it should be easy since you quoted it.
Offline Z-Man
« Reply #17 - Posted 2011-08-14 06:54:25 »

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.
Offline Roquen
« Reply #18 - Posted 2011-08-14 07:41:19 »

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. 
Offline theagentd
« Reply #19 - Posted 2011-08-14 09:55:14 »

I didn't even now this was possible... I'll try it out the next time I need to break a nested loop.  Smiley

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (39 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (66 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (55 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!