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

Switch Case Style

Expand Messages
  • paperlampshade
    Hi, I have a question about Switch Case style as verified in jslint.A switch case written as below (case statement starting on the line after the switch
    Message 1 of 6 , Jan 31, 2013
    • 0 Attachment
      Hi,
      I have a question about Switch Case style as verified in jslint.A switch
      case written as below (case statement starting on the line after the
      switch statement) will give many errors regarding the expected position
      from line "case 1:" to line "output = "";".
      var test = function (input) { 'use strict'; var output; switch
      (input) { case 1: output = "a"; break;
      case 2: output = "b"; break; case 3:
      output = "c"; break; default: output = "";
      } return output;};
      However when the same statement is written with the "case 1:" statement
      beginning one space after the switch "{" and the closing "}" one space
      from the "output = "";" line, there is no errors found.Example below:
      var test = function (input) { 'use strict'; var output; switch
      (input) { case 1: output = "a"; break; case
      2: output = "b"; break; case 3:
      output = "c"; break; default: output = "";
      } return output;};
      Why is it important to have switch case syntax as in the second example?
      Thanks very much!
      Steve Hansen


      [Non-text portions of this message have been removed]
    • benquarmby
      Hi Steve, I think I know what you re talking about, but the whitespace didn t come through properly in your examples. You may wan to retype your example with
      Message 2 of 6 , Feb 1 1:15 PM
      • 0 Attachment
        Hi Steve,

        I think I know what you're talking about, but the whitespace didn't come through properly in your examples. You may wan to retype your example with tilde (~) for spaces and ensure the line breaks stick.


        Cheers
        Ben

        --- In jslint_com@yahoogroups.com, "paperlampshade" wrote:
        >
        > Hi,
        > I have a question about Switch Case style as verified in jslint.A switch
        > case written as below (case statement starting on the line after the
        > switch statement) will give many errors regarding the expected position
        > from line "case 1:" to line "output = "";".
        > var test = function (input) { 'use strict'; var output; switch
        > (input) { case 1: output = "a"; break;
        > case 2: output = "b"; break; case 3:
        > output = "c"; break; default: output = "";
        > } return output;};
        > However when the same statement is written with the "case 1:" statement
        > beginning one space after the switch "{" and the closing "}" one space
        > from the "output = "";" line, there is no errors found.Example below:
        > var test = function (input) { 'use strict'; var output; switch
        > (input) { case 1: output = "a"; break; case
        > 2: output = "b"; break; case 3:
        > output = "c"; break; default: output = "";
        > } return output;};
        > Why is it important to have switch case syntax as in the second example?
        > Thanks very much!
        > Steve Hansen
        >
        >
        > [Non-text portions of this message have been removed]
        >
      • paperlampshade
        Hi Ben, Oh shoot, sorry about that. Hopefully this will come through better: I have a question about Switch Case style as verified in jslint. A switch case
        Message 3 of 6 , Feb 2 7:33 AM
        • 0 Attachment
          Hi Ben,

          Oh shoot, sorry about that. Hopefully this will come through better:

          I have a question about Switch Case style as verified in jslint.

          A switch case written as below (case statement starting on the line after the switch statement) will give many errors regarding the expected position from line "case 1:" to line "output = "";".

          var test = function (input) {
          ~~~~'use strict';
          ~~~~var output;
          ~~~~switch (input) {
          ~~~~~~~~case 1:
          ~~~~~~~~~~~~output = "a";
          ~~~~~~~~~~~~break;
          ~~~~~~~~case 2:
          ~~~~~~~~~~~~output = "b";
          ~~~~~~~~~~~~break;
          ~~~~~~~~case 3:
          ~~~~~~~~~~~~output = "c";
          ~~~~~~~~~~~~break;
          ~~~~~~~~default:
          ~~~~~~~~~~~~output = "";
          ~~~~}
          ~~~~return output;
          };

          However when the same statement is written with the "case 1:" statement beginning one space after the switch "{" and the closing "}" one space from the "output = "";" line, there is no errors found.Example below:

          var test = function (input) {
          ~~~~'use strict';
          ~~~~var output;
          ~~~~switch (input) { case 1:
          ~~~~~~~~~~~~output = "a";
          ~~~~~~~~~~~~break;
          ~~~~~~~~case 2:
          ~~~~~~~~~~~~output = "b";
          ~~~~~~~~~~~~break;
          ~~~~~~~~case 3:
          ~~~~~~~~~~~~output = "c";
          ~~~~~~~~~~~~break;
          ~~~~~~~~default:
          ~~~~~~~~~~~~output = ""; }
          ~~~~return output;
          };

          Why is it important to have switch case syntax as in the second example?

          Thanks very much!

          Steve Hansen

          --- In jslint_com@yahoogroups.com, "benquarmby" wrote:
          >
          > Hi Steve,
          >
          > I think I know what you're talking about, but the whitespace didn't come through properly in your examples. You may wan to retype your example with tilde (~) for spaces and ensure the line breaks stick.
          >
          >
          > Cheers
          > Ben
          >
          > --- In jslint_com@yahoogroups.com, "paperlampshade" wrote:
          > >
          > > Hi,
          > > I have a question about Switch Case style as verified in jslint.A switch
          > > case written as below (case statement starting on the line after the
          > > switch statement) will give many errors regarding the expected position
          > > from line "case 1:" to line "output = "";".
          > > var test = function (input) { 'use strict'; var output; switch
          > > (input) { case 1: output = "a"; break;
          > > case 2: output = "b"; break; case 3:
          > > output = "c"; break; default: output = "";
          > > } return output;};
          > > However when the same statement is written with the "case 1:" statement
          > > beginning one space after the switch "{" and the closing "}" one space
          > > from the "output = "";" line, there is no errors found.Example below:
          > > var test = function (input) { 'use strict'; var output; switch
          > > (input) { case 1: output = "a"; break; case
          > > 2: output = "b"; break; case 3:
          > > output = "c"; break; default: output = "";
          > > } return output;};
          > > Why is it important to have switch case syntax as in the second example?
          > > Thanks very much!
          > > Steve Hansen
          > >
          > >
          > > [Non-text portions of this message have been removed]
          > >
          >
        • Felix E. Klee
          On Sat, Feb 2, 2013 at 4:33 PM, paperlampshade ... Looks strange. The following formatting is IMHO easy to read and it is accepted by
          Message 4 of 6 , Feb 2 8:35 AM
          • 0 Attachment
            On Sat, Feb 2, 2013 at 4:33 PM, paperlampshade <beefo@...>
            wrote:
            > ~~~~switch (input) { case 1:
            >
            > [...]
            >
            > Why is it important to have switch case syntax as in the second example?

            Looks strange. The following formatting is IMHO easy to read and it
            is accepted by JSLint:

            var test = function (input) {
            'use strict';
            var output;
            switch (input) {
            case 1:
            output = "a";
            break;
            case 2:
            output = "b";
            break;
            case 3:
            output = "c";
            break;
            default:
            output = "";
            }
            return output;
            };

            BTW, just post emails in plain-text and no need to insert these ugly
            `~`.
          • douglascrockford
            ... Thanks. Please try it now.
            Message 5 of 6 , Feb 2 10:03 AM
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, "paperlampshade" wrote:
              >
              > Hi,
              > I have a question about Switch Case style as verified in jslint.A switch
              > case written as below (case statement starting on the line after the
              > switch statement) will give many errors regarding the expected position
              > from line "case 1:" to line "output = "";".
              > var test = function (input) { 'use strict'; var output; switch
              > (input) { case 1: output = "a"; break;
              > case 2: output = "b"; break; case 3:
              > output = "c"; break; default: output = "";
              > } return output;};
              > However when the same statement is written with the "case 1:" statement
              > beginning one space after the switch "{" and the closing "}" one space
              > from the "output = "";" line, there is no errors found.Example below:
              > var test = function (input) { 'use strict'; var output; switch
              > (input) { case 1: output = "a"; break; case
              > 2: output = "b"; break; case 3:
              > output = "c"; break; default: output = "";
              > } return output;};
              > Why is it important to have switch case syntax as in the second example?


              Thanks. Please try it now.
            • paperlampshade
              Works great! Thanks very much.
              Message 6 of 6 , Feb 4 7:36 PM
              • 0 Attachment
                Works great! Thanks very much.

                --- In jslint_com@yahoogroups.com, "douglascrockford" wrote:
                >
                > --- In jslint_com@yahoogroups.com, "paperlampshade" wrote:
                > >
                > > Hi,
                > > I have a question about Switch Case style as verified in jslint.A switch
                > > case written as below (case statement starting on the line after the
                > > switch statement) will give many errors regarding the expected position
                > > from line "case 1:" to line "output = "";".
                > > var test = function (input) { 'use strict'; var output; switch
                > > (input) { case 1: output = "a"; break;
                > > case 2: output = "b"; break; case 3:
                > > output = "c"; break; default: output = "";
                > > } return output;};
                > > However when the same statement is written with the "case 1:" statement
                > > beginning one space after the switch "{" and the closing "}" one space
                > > from the "output = "";" line, there is no errors found.Example below:
                > > var test = function (input) { 'use strict'; var output; switch
                > > (input) { case 1: output = "a"; break; case
                > > 2: output = "b"; break; case 3:
                > > output = "c"; break; default: output = "";
                > > } return output;};
                > > Why is it important to have switch case syntax as in the second example?
                >
                >
                > Thanks. Please try it now.
                >
              Your message has been successfully submitted and would be delivered to recipients shortly.