Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

break removal

Thread view: 
Roedy Green - 02 Jul 2005 20:41 GMT
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.

Signature

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 - 02 Jul 2005 21:54 GMT
> Let's say you have some code like this:
>
[quoted text clipped - 22 lines]
> 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

Signature

XML is the programmer's duct tape.

Larry Barowski - 02 Jul 2005 22:34 GMT
> Let's say you have some code like this:
>
[quoted text clipped - 25 lines]
> 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 - 02 Jul 2005 22:47 GMT
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 - 02 Jul 2005 23:04 GMT
>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.

Signature

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 - 02 Jul 2005 23:12 GMT
>>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?

Signature

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 - 02 Jul 2005 23:24 GMT
>>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.

Signature

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

ChrisWSU - 02 Jul 2005 23:46 GMT
oh thats kinda neat, thanx for the follow up
Tim Tyler - 09 Jul 2005 15:27 GMT
Roedy Green <look-on@mindprod.com.invalid> 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/
Signature

__________
|im |yler  http://timtyler.org/  tim@tt1lock.org  Remove lock to reply.

Joan - 09 Jul 2005 23:57 GMT
> Roedy Green <look-on@mindprod.com.invalid> 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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.