break removal

Discussion in 'Java' started by Roedy Green, Jul 2, 2005.

  1. Roedy Green

    Roedy Green Guest

    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
    Roedy Green, Jul 2, 2005
    #1
    1. Advertising

  2. 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.
    Raymond DeCampo, Jul 2, 2005
    #2
    1. Advertising

  3. Roedy Green

    ChrisWSU Guest

    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 :)
    ChrisWSU, Jul 2, 2005
    #3
  4. Roedy Green

    Roedy Green Guest

    On 2 Jul 2005 14:47:40 -0700, "ChrisWSU" <> 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
    Roedy Green, Jul 2, 2005
    #4
  5. On Sat, 02 Jul 2005 22:04:40 +0000, Roedy Green wrote:

    > On 2 Jul 2005 14:47:40 -0700, "ChrisWSU" <> 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"
    Stefan Schulz, Jul 2, 2005
    #5
  6. Roedy Green

    Roedy Green Guest

    On Sat, 02 Jul 2005 22:04:40 GMT, Roedy Green
    <> 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
    Roedy Green, Jul 2, 2005
    #6
  7. "Roedy Green" <> wrote in message
    news:...
    >
    > 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.
    Larry Barowski, Jul 2, 2005
    #7
  8. Roedy Green

    ChrisWSU Guest

    oh thats kinda neat, thanx for the follow up
    ChrisWSU, Jul 2, 2005
    #8
  9. Roedy Green

    Tim Tyler Guest

    Roedy Green <> 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/ Remove lock to reply.
    Tim Tyler, Jul 9, 2005
    #9
  10. Roedy Green

    Joan Guest

    "Tim Tyler" <> wrote in message news:...
    > Roedy Green <> 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.
    Joan, Jul 9, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    557
  2. Guest

    Gridview Duplicate removal.

    Guest, Feb 7, 2006, in forum: ASP .Net
    Replies:
    12
    Views:
    6,748
    Yuan Ren[MSFT]
    Feb 15, 2006
  3. lucy
    Replies:
    6
    Views:
    1,284
    Carl Howells
    Sep 2, 2004
  4. Octopus0
    Replies:
    2
    Views:
    639
    dorayme
    Mar 30, 2006
  5. Replies:
    12
    Views:
    960
Loading...

Share This Page