break removal  
Author Message
Roedy Green





PostPosted: 2005-7-3 3:42:00 Top

java-programmer, break removal
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/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
Raymond DeCampo





PostPosted: 2005-7-3 4:55:00 Top

java-programmer >> break removal 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.
 
ChrisWSU





PostPosted: 2005-7-3 5:48:00 Top

java-programmer >> break removal 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 :)

 
 
Roedy Green





PostPosted: 2005-7-3 6:05:00 Top

java-programmer >> break removal On 2 Jul 2005 14:47:40 -0700, "ChrisWSU" <email***@***.com> 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/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
 
Stefan Schulz





PostPosted: 2005-7-3 6:12:00 Top

java-programmer >> break removal On Sat, 02 Jul 2005 22:04:40 +0000, Roedy Green wrote:

> On 2 Jul 2005 14:47:40 -0700, "ChrisWSU" <email***@***.com> 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"


 
 
Roedy Green





PostPosted: 2005-7-3 6:24:00 Top

java-programmer >> break removal On Sat, 02 Jul 2005 22:04:40 GMT, Roedy Green
<email***@***.com> 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/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
 
Larry Barowski





PostPosted: 2005-7-3 6:30:00 Top

java-programmer >> break removal
"Roedy Green" <email***@***.com> wrote in message
news:email***@***.com...
>
> 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.


 
 
ChrisWSU





PostPosted: 2005-7-3 6:46:00 Top

java-programmer >> break removal oh thats kinda neat, thanx for the follow up

 
 
Tim Tyler





PostPosted: 2005-7-9 22:27:00 Top

java-programmer >> break removal Roedy Green <email***@***.com> 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/config_coding.html#FallThrough

...and then visit:

http://eclipse-cs.sourceforge.net/
--
__________
|im |yler http://timtyler.org/ email***@***.com Remove lock to reply.
 
 
Joan





PostPosted: 2005-7-10 6:57:00 Top

java-programmer >> break removal
"Tim Tyler" <email***@***.com> wrote in message news:email***@***.com...
> Roedy Green <email***@***.com> 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.