Loading ...
Sorry, an error occurred while loading the content.

Re: break required at the end of a switch case even when redundant?

Expand Messages
  • Douglas Crockford
    ... JSLint does not currently do the flow analysis to determine that every path through a case returns. So move the break after case 1 to just before case 1.
    Message 1 of 2 , Dec 29, 2010
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "duncan.cross@..." <duncan.cross@...> wrote:
      >
      > If I have a switch case that ends with an if-block, and all of the if-block's child statements end with either 'break' or 'return', like case 0 here:
      >
      > function testCase(i, j) {
      > switch(i) {
      > case 0:
      > // ...
      > if (j < 2) {
      > // ...
      > return 10;
      > }
      > else {
      > // ...
      > return 5;
      > }
      > case 1:
      > // ...
      > break;
      > }
      > }
      >
      > ... I'd expect to be able to omit the break before case 1, because the statement would be unreachable. Adding a redundant one would feel kind of counterproductive to the goal of improving code readability, but that is what JSLint's error recommends. I do understand the point of not allowing fall-through to catch a very common kind of mistake, but in cases like this, there is no logical possibility of fall-through - or is there something I am missing?


      JSLint does not currently do the flow analysis to determine that every path through a case returns. So move the break after case 1 to just before case 1.

      Also, don't use a switch when there are only two cases. Use an if instead. Perhaps JSLint should suggest that.
    Your message has been successfully submitted and would be delivered to recipients shortly.