Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unreachable 'break' after 'return'.

    Hello Coders, I am not a coder so this is a problem to me to solve this error. I checked javascript at JSLint and it droped me a error:
    Code:
    Problem at line 29 character 17: Unreachable 'break' after 'return'.
    
    break;
    
    Problem at line 50 character 21: Unreachable 'break' after 'return'.
    
    break;
    and the script is:
    Code:
    <html>
    
    <head>
    
    <title></title>
    
    </head>
    
    <body>
    <script type="text/javascript">
    function keyDown(key) {
        if (currentstep == "1") {
            switch (key) {
            case 17:
                if (key1pressed == "") {
             changeKeyStyle("key1", "down");
             key1pressed = "1";
            showobj("step2");
                }
                break;
            case 67:
                if (key2pressed == "") {
             changeKeyStyle("key2", "down");
             key2pressed = "1";
                }
                if (key1pressed == "1" && key2pressed == "1") {
             step1Complete();
                    return false;
                    break;
                }
                break;
            default:
            }
        } else if (currentstep == "2") {
            switch (key) {
            case 18:
                if (key1pressed == "") {
             changeKeyStyle("key1", "down");
             key1pressed = "1";
             showobj("step2");
                }
                break;
            case 68:
                if (key2pressed == "") {
            changeKeyStyle("key2", "down");
             key2pressed = "1";
                    if (key1pressed == "1" && key2pressed == "1") {
                 step2complete();
                        return false;
                        break;
                    }
                }
                break;
            default:
            }
        }
    }
    
    </script>
        
    
    </body>
    
    </html>
    Thank you for all your help,
    Arnas

  • #2
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    The return statement terminates the currently executing function, so any subsequent statements are unreachable.

    For clarity of operation and ease of debugging, the return statement should never appear anywhere except as the last statement in a function.

  • #3
    New Coder
    Join Date
    May 2010
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post
    Remove all the breaks after return statements.

    <script type="text/javascript">
    function keyDown(key) {
    if (currentstep == "1") {
    switch (key) {
    case 17:
    if (key1pressed == "") {
    changeKeyStyle("key1", "down");
    key1pressed = "1";
    showobj("step2");
    }
    break;
    case 67:
    if (key2pressed == "") {
    changeKeyStyle("key2", "down");
    key2pressed = "1";
    }
    if (key1pressed == "1" && key2pressed == "1") {
    step1Complete();
    return false;
    break;
    }
    break;
    default:
    }
    } else if (currentstep == "2") {
    switch (key) {
    case 18:
    if (key1pressed == "") {
    changeKeyStyle("key1", "down");
    key1pressed = "1";
    showobj("step2");
    }
    break;
    case 68:
    if (key2pressed == "") {
    changeKeyStyle("key2", "down");
    key2pressed = "1";
    if (key1pressed == "1" && key2pressed == "1") {
    step2complete();
    return false;
    break;
    }
    }
    break;
    default:
    }
    }
    }

    </script>


    </body>

    </html>

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    if (key1pressed == "1" && key2pressed == "1") {
    step2complete();
    return false;
    break;
    }
    You don't need those breaks after return statements.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New to the CF scene
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by randomuser773 View Post
    The return statement terminates the currently executing function, so any subsequent statements are unreachable.

    For clarity of operation and ease of debugging, the return statement should never appear anywhere except as the last statement in a function.
    Thanks Randomuser773 for your reply, maby you can post a peace of code how it have to look like that I could chage it.

    Thanks again,
    Arnas

  • #6
    New to the CF scene
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Than you all for your reply!

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,286
    Thanks
    12
    Thanked 344 Times in 340 Posts
    Quote Originally Posted by randomuser773 View Post
    For clarity of operation and ease of debugging, the return statement should never appear anywhere except as the last statement in a function.
    clarity, yes. ease of debugging, well, depends on whom you ask. personally, I like to return where I need (and be it the first control statement, that checks the parameter types), keeps my code a lot more clear.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by arnux View Post
    Thanks Randomuser773 for your reply, maby you can post a peace of code how it have to look like that I could chage it.
    Use a variable as a return value and if necessary use its value to branch as required. That way the function always terminates at the end and it is always clear why it did so:
    Code:
    function keyDown(key) 
    {
     var rv = true;
    
        if (currentstep == "1") 
        {
         switch (key) 
         {
            case 17:
            if (key1pressed == "") 
            {
             changeKeyStyle("key1", "down");
             key1pressed = "1";
             showobj("step2");
            }
            break;
            
            case 67:
            if (key2pressed == "") 
            {
             changeKeyStyle("key2", "down");
             key2pressed = "1";
            }
            
            if (key1pressed == "1" && key2pressed == "1") 
            {
             step1Complete();
             rv = false;
            }        
         }
        } 
        else 
         if (currentstep == "2") 
         {
          switch (key) 
          {
           case 18:
           if (key1pressed == "") 
           {
             changeKeyStyle("key1", "down");
             key1pressed = "1";
             showobj("step2");
           }
           break;
           
           case 68:
           if (key2pressed == "") 
           {
            changeKeyStyle("key2", "down");
            key2pressed = "1";
            if (key1pressed == "1" && key2pressed == "1") 
            {
             step2complete();
             rv = false;
            }
           }
          }
         }
        
      return rv;
    }

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,516
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Purists have been arguing for single point of return for at least 30 years now.

    It's never caught on and I don't think it ever will.

    Like it or not, there *are* times where it's much, much clearer (and easier to debug and--I believe--cleaner code style) to return early than it is to have to have really ugly code just to support single point of return. Not saying always return early. Just use sanity.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,516
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    And, randomuser, true purists would also insist that *every* case have a break terminating it. Even the last in the switch. And they'd also insist that you have a default (even if it does nothing), for clarity of coding.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,346
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by Old Pedant View Post
    they'd also insist that you have a default (even if it does nothing), for clarity of coding.
    doesn't matter, at least in firefox:

    firebug demo:
    Code:
    function t(){
     switch(t){
        case 1: break;
      }
    }
    alert(t)

    which shows this text:
    Code:
    function t() {
        switch (t) {
          case 1:
            break;
          default:;
        }
    }
    this is yet another reason i think jslint is rather silly: actual eval'd code isn't the same code lint examines! if you paste fire-fox generated code into lint, you'll see 90% of the complaints vanish without effort.
    Last edited by rnd me; 05-17-2010 at 09:50 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,516
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    LOL!!! Nice touch, RndMe!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by Old Pedant View Post
    Purists have been arguing for single point of return for at least 30 years now.

    It's never caught on and I don't think it ever will.
    Better ways of doing things often don't; it doesn't mean they're not better.

    Like it or not, there *are* times where it's much, much clearer (and easier to debug and--I believe--cleaner code style) to return early than it is to have to have really ugly code just to support single point of return. Not saying always return early. Just use sanity.
    I'm not claiming to be a purist.
    I think you're just defending a lazy programming style that I have always found hazardous to debug. I can't think of any scenario that makes a single exit point routine harder to debug, conversely if code is interspersed with cop-out return statements, it has to be examined far more closely to discover why it's letting-go inexplicably.
    I'm proud never to have made any other programmer a victim of that problem.

    A function should flow as a pipe not as a colander.

  • #14
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,516
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    Pardon me, I should have said that I was not just reflecting my own coding. What I said applies to the coding standards adopted by many organizations (including the one I currently work for, and long before I arrived on the scene).

    Now, granted, with most of the programming languages we work with we use exception handling that obviates the need for early returns. That is, we throw (or handle) an exception and that serves as our exit(s) from the function.

    And, indeed, I (and many if not most programmers here) tend to use single exit points. But, again, it's not demanded. And I, personally, think that's a sane approach. But you and I will simply have to agree to disagree.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •