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

Problems with default and switch

Expand Messages
  • Luke Page
    Hello, I m getting *Error:* Problem at line 4 character 3: Empty case default: with switch cases with the following code... (this is a hugely simplified
    Message 1 of 3 , Jan 11, 2011
    • 0 Attachment
      Hello,

      I'm getting

      *Error:*

      Problem at line 4 character 3: Empty case

      default:

      with switch cases with the following code... (this is a hugely simplified
      example)

      var func = function (a) {
      switch (a) {
      case this.c:
      default:
      throw new Error("Not supported");
      }
      };

      this works fine

      var func = function (a) {
      switch (a) {
      case this.c:
      throw new Error("Not supported");
      default:
      throw new Error("Not supported");
      }
      };

      as does

      var func = function (a) {
      switch (a) {
      case this.c:
      case this.d:
      throw new Error("Not supported");
      }
      };

      but I'd rather not have to repeat the same code in one of the cases and the
      default.

      Is this an error?

      Regards,

      Luke


      [Non-text portions of this message have been removed]
    • Felix E. Klee
      ... To quote from Douglas Crockford s JavaScript: The Good Parts : I never allow switch cases to fall through to the next case. I once found a bug in my code
      Message 2 of 3 , Jan 11, 2011
      • 0 Attachment
        On Tue, Jan 11, 2011 at 11:56 AM, Luke Page <luke.a.page@...> wrote:
        > Is this an error?

        To quote from Douglas Crockford's "JavaScript: The Good Parts":

        "I never allow switch cases to fall through to the next case. I once
        found a bug in my code caused by an unintended fall through
        immediately after having made a vigorous speech about why fall through
        was sometimes useful. I was fortunate in that I was able to learn from
        the experience. When reviewing the features of a language, I now pay
        special attention to features that are sometimes useful but
        occasionally dangerous. Those are the worst parts because it is
        difficult to tell whether they are being used correctly. That is a
        place where bugs hide."

        Does anyone have a snippet of the source code showing the bug?

        I'm curious to see whether the case statement was entirely empty.
        Because, in my opinion, entirely empty case statements should be OK.
        Only if there is something in a case statement without a terminating
        "break", "return", or "throw", then that should trigger an error. But
        I'm happy to learn from the experience of others.
      • Douglas Crockford
        ... If you delete the empty case, your program will get smaller and faster.
        Message 3 of 3 , Jan 11, 2011
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, Luke Page <luke.a.page@...> wrote:
          >
          > Hello,
          >
          > I'm getting
          >
          > *Error:*
          >
          > Problem at line 4 character 3: Empty case
          >
          > default:
          >
          > with switch cases with the following code... (this is a hugely simplified
          > example)
          >
          > var func = function (a) {
          > switch (a) {
          > case this.c:
          > default:
          > throw new Error("Not supported");
          > }
          > };

          > Is this an error?


          If you delete the empty case, your program will get smaller and faster.
        Your message has been successfully submitted and would be delivered to recipients shortly.