Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does this usage of break and label make sense to anyone?

 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is from a decompiled application so it's possible something was lost in the decompilation.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15485
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The break statement is essentially a goto (jump) to the label.

This is really ugly code. There's a reason why Java doesn't have a goto statement; it can quickly make your source code a big, unreadable mess (see Wikipedia for details). Please do not use the 'break' statement in your own code like this. The following would have been much better and more readable:

Is this decompiled code?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With that "break" after an "if" I would be surprised if it recompiles.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper Young wrote:
Please do not use the 'break' statement in your own code like this.?


Indeed, you should not: but you actually can't, anyway. A break can only break out of a loop; the label marks the loop that is to be exited. The code you've shown won't actually compile; the decompiler has emitted some kind of weird hybrid Java/C because it couldn't find a valid decompilation.
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks everyone. I've emailed the developer of the decompiler to see if he can help. Trust me when I say I would never write code this way. The lack of Goto is exactly why I like java.

So in short yes this is decompiled code.

The break after the if was one confusion I'm having....but that only scratches the surface. I'm tempted to remove all these label and break statements just so I can compile if possible.

 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't ask whoever developed your application for the code?
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can't ask whoever developed your application for the code?


Asked my boss and he said no. The enhancement I'm putting in as a stop gap is inherently in the next version.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:With that "break" after an "if" I would be surprised if it recompiles.

There's no inherent problem with a break after an if - provided both are inside another structure that allows break. Such as a labeled statement. The problem here is only that the labeled statement is in a position that we can't legally break from, since it doesn't contain the break.

Ernest Friedman-Hill wrote:A break can only break out of a loop; the label marks the loop that is to be exited.

EFH was emphasizing the "out of" part here - but I wanted to add that it's not just loops that you can break out of. An unlabeled break can break from a loop or switch, while a labeled break can break from any labeled statement. Including a labeled block, like this:

That's equivalent in function to Campbell's code above. Not as clear to read, but legal. Whether that's what the code is supposed to do, we don't know, but I think it's plausible.

Matt Kidd wrote:I'm tempted to remove all these label and break statements just so I can compile if possible.

Well, try commenting them out, so you can still see them. Chances are they were trying to do something useful, and until you figure out what that was, you probably shouldn't delete the clues they provide. I don't favor keeping commented-out code around indefinitely, just initially, while you're working out what it's supposed to do.

You may also benefit from using javap, a tool in the JDK for disassembling class files. I'd try something like

to put the output in a file for careful study. It takes some work to make sense of this stuff. I wouldn't make this my primary source for figuring out what the code does (too much work!) but it's good for resolving questions like where, exactly, is the execution supposed to jump to after the break.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt Kidd wrote:
Campbell Ritchie wrote:You can't ask whoever developed your application for the code?


Asked my boss and he said no. The enhancement I'm putting in as a stop gap is inherently in the next version.

Has he also given a good reason for saying no? I doubt you spending hours on this is good for productivity, if you can save those hours by asking for the code. If it's commercial code the price may be too high of course, but is that the case?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There was nothing in the code snippet we were given to show the labelled break was part of a loop; it is unusual to see a labelled break in a switch. That is why I said I would be surprised if it re-compiled.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic