Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > break removal

Reply
Thread Tools

break removal

 
 
Roedy Green
Guest
Posts: n/a
 
      07-02-2005

Let's say you have some code like this:


case 1:
if ( x )
if ( y )
return a;
else return b;
else return c;

break;

Jva will insist you take out the break because it is not needed.

then later you inadvertently meddle with the code say like this:

case 1:
if ( x )
if ( y )
return a;
else return c;

now you need the break back in there again, but of course Java wont't
tell you.

What is the best way to handle this to make sure you have covered all
possibilites and that you truly can't ever fall through?

You can't even put debug code in there to catch the problem. Java
will make you take it out if the prgram is CURRENTLY working.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
 
 
 
Raymond DeCampo
Guest
Posts: n/a
 
      07-02-2005
Roedy Green wrote:
> Let's say you have some code like this:
>
>
> case 1:
> if ( x )
> if ( y )
> return a;
> else return b;
> else return c;
>
> break;
>
> Jva will insist you take out the break because it is not needed.
>
> then later you inadvertently meddle with the code say like this:
>
> case 1:
> if ( x )
> if ( y )
> return a;
> else return c;
>
> now you need the break back in there again, but of course Java wont't
> tell you.
>
> What is the best way to handle this to make sure you have covered all
> possibilites and that you truly can't ever fall through?


In this case, probably enforcing a one return statement rule.

>
> You can't even put debug code in there to catch the problem. Java
> will make you take it out if the prgram is CURRENTLY working.
>


Ray

--
XML is the programmer's duct tape.
 
Reply With Quote
 
 
 
 
ChrisWSU
Guest
Posts: n/a
 
      07-02-2005
with case 1:
if ( x )
if ( y )
return a;
else
return b;
else
return c;
break;

the break; is unreachable... its logically impossible to get to that
line. but with

case 1:
if ( x ) {
if ( y ){
return a;
} else {
return c;
}
}

the case of x being false is not covered so you need a break to keep
from going into next case.

>now you need the break back in there again, >but of course Java wont't
>tell you.


thats because its not supposed to, its part of the switch statement to
allow you to overlap cases, its a feature.

>What is the best way to handle this to make >sure you have covered all
>possibilites and that you truly can't ever fall >through?


truth tables. planning. if switch statements cause to much problems
dont use them. There are alternatives such as if/else ifs. and a few
others that although fun most would consider a kludge

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-02-2005
On 2 Jul 2005 14:47:40 -0700, "ChrisWSU" <(E-Mail Removed)> wrote or
quoted :

>thats because its not supposed to, its part of the switch statement to
>allow you to overlap cases, its a feature.


IN my case it as always an error, and one I would like to be informed
of.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Stefan Schulz
Guest
Posts: n/a
 
      07-02-2005
On Sat, 02 Jul 2005 22:04:40 +0000, Roedy Green wrote:

> On 2 Jul 2005 14:47:40 -0700, "ChrisWSU" <(E-Mail Removed)> wrote or
> quoted :
>
>>thats because its not supposed to, its part of the switch statement to
>>allow you to overlap cases, its a feature.

>
> IN my case it as always an error, and one I would like to be informed
> of.


Why not use

assert false: "Unreachable";

in such a case?

--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-02-2005
On Sat, 02 Jul 2005 22:04:40 GMT, Roedy Green
<(E-Mail Removed)> wrote or quoted :

>
>>thats because its not supposed to, its part of the switch statement to
>>allow you to overlap cases, its a feature.

>
>IN my case it as always an error, and one I would like to be informed
>of.


I found what I am looking for a javac.exe switch -Xlint:fallthrough

now to try it out.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/...s_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Reply With Quote
 
Larry Barowski
Guest
Posts: n/a
 
      07-02-2005

"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Let's say you have some code like this:
>
>
> case 1:
> if ( x )
> if ( y )
> return a;
> else return b;
> else return c;
>
> break;
>
> Jva will insist you take out the break because it is not needed.
>
> then later you inadvertently meddle with the code say like this:
>
> case 1:
> if ( x )
> if ( y )
> return a;
> else return c;
>
> now you need the break back in there again, but of course Java wont't
> tell you.
>
> What is the best way to handle this to make sure you have covered all
> possibilites and that you truly can't ever fall through?
>
> You can't even put debug code in there to catch the problem. Java
> will make you take it out if the prgram is CURRENTLY working.


It's not pretty, but you can always do
if(true)
return x;

I use that all the time to temporarily short-circuit the remainder of a
method for debugging purposes.


 
Reply With Quote
 
ChrisWSU
Guest
Posts: n/a
 
      07-02-2005
oh thats kinda neat, thanx for the follow up

 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      07-09-2005
Roedy Green <(E-Mail Removed)> wrote or quoted:

> What is the best way to handle this to make sure you have covered all
> possibilites and that you truly can't ever fall through?


See:

``FallThrough [...]

Checks for fall through in switch statements Finds locations where a
case contains Java code - but lacks a break, return, throw or continue
statement.''

- http://checkstyle.sourceforge.net/co...ml#FallThrough

....and then visit:

http://eclipse-cs.sourceforge.net/
--
__________
|im |yler http://timtyler.org/ http://www.velocityreviews.com/forums/(E-Mail Removed) Remove lock to reply.
 
Reply With Quote
 
Joan
Guest
Posts: n/a
 
      07-09-2005

"Tim Tyler" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
> Roedy Green <(E-Mail Removed)> wrote or quoted:
>
> > What is the best way to handle this to make sure you have covered all
> > possibilites and that you truly can't ever fall through?

>

Javac version 5.0 has a -X switch that will tell you exactly this.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
Duplicate bookmark removal in firefox tmt Firefox 2 05-12-2005 02:46 PM
Removal from host of a Wireless network place (other computer) =?Utf-8?B?c3N0YW5qYg==?= Wireless Networking 2 04-08-2005 02:52 PM
Search engine removal Trev67 Firefox 4 02-15-2005 07:55 PM
Removal of plugins from Mozilla's browser Adam Smith Firefox 2 12-11-2004 10:31 PM



Advertisments