break removal

R

Roedy Green

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
 
R

Raymond DeCampo

Roedy said:
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
 
C

ChrisWSU

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

Roedy Green

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
 
R

Roedy Green

L

Larry Barowski

Roedy Green said:
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.
 
T

Tim Tyler

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top