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

Unexpected else after return (UNCLASSIFIED)

Expand Messages
  • Cheney, Edward A SSG RES USAR USARC
    Classification: UNCLASSIFIED I cannot help but think this rule may be harmful. I understand the intention behind this rule and how that intention may apply to
    Message 1 of 3 , Mar 4 9:00 AM
    • 0 Attachment
      Classification: UNCLASSIFIED
      I cannot help but think this rule may be harmful. I understand the
      intention behind this rule and how that intention may apply to more than
      90% of use cases, but consider the following example:

      var a = function (x) {
      "use strict";
      if (typeof x === "undefined") {
      return;
      } else {
      //complex and time consuming algorithmic logic here
      }
      };

      In this case I am running a test. If the test fails I immediately exit
      the function. The result of this early exit is that a whole host of
      potential bugs and failures is avoided. Furthermore, the potential and
      timely execution necessary to generate those potential bugs is also
      avoided. If an error code is to be escaped from the function, such as
      a try/catch scenario, then simply return a string explaining the error
      instead of returning nothing.

      In my mind the most valuable use of the return keyword is the immediate
      exit of a function. The fact that you can actually return a value using
      this keyword is, at least my mind, merely serendipity. As a result I
      view the return keyword as similar to the break keyword, but far more
      powerful and without any contextual confusion.

      Is this style of expression invalid or harmful for any practical reason?

      Austin


      > I expect this is a common question, but searching for "this" isn't getting me anywhere.
      >
      > "use strict";
      >
      > function foo()
      > {
      > var a = this;
      > }
      >
      > var bar = function()
      > {
      > var a = this;
      > };
      >
      > jslint likes bar, but not foo. Why?
      >
      >

      Classification: UNCLASSIFIED
    • spence.randall@ymail.com
      You could simply write the example function as follows: var a = function (x) { use strict ; if (typeof x === undefined ) { return; } //complex and time
      Message 2 of 3 , Mar 4 9:19 AM
      • 0 Attachment
        You could simply write the example function as follows:

        var a = function (x) {
        "use strict";
        if (typeof x === "undefined") {
        return;
        }
        //complex and time consuming algorithmic logic here
        };

        It still utilizes the early exit, eliminates the unnecessary else bracket and passes jslint.

        - Randall

        --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:
        >
        > Classification: UNCLASSIFIED
        > I cannot help but think this rule may be harmful. I understand the
        > intention behind this rule and how that intention may apply to more than
        > 90% of use cases, but consider the following example:
        >
        > var a = function (x) {
        > "use strict";
        > if (typeof x === "undefined") {
        > return;
        > } else {
        > //complex and time consuming algorithmic logic here
        > }
        > };
        >
        > In this case I am running a test. If the test fails I immediately exit
        > the function. The result of this early exit is that a whole host of
        > potential bugs and failures is avoided. Furthermore, the potential and
        > timely execution necessary to generate those potential bugs is also
        > avoided. If an error code is to be escaped from the function, such as
        > a try/catch scenario, then simply return a string explaining the error
        > instead of returning nothing.
        >
        > In my mind the most valuable use of the return keyword is the immediate
        > exit of a function. The fact that you can actually return a value using
        > this keyword is, at least my mind, merely serendipity. As a result I
        > view the return keyword as similar to the break keyword, but far more
        > powerful and without any contextual confusion.
        >
        > Is this style of expression invalid or harmful for any practical reason?
        >
        > Austin
        >
        >
        > > I expect this is a common question, but searching for "this" isn't getting me anywhere.
        > >
        > > "use strict";
        > >
        > > function foo()
        > > {
        > > var a = this;
        > > }
        > >
        > > var bar = function()
        > > {
        > > var a = this;
        > > };
        > >
        > > jslint likes bar, but not foo. Why?
        > >
        > >
        >
        > Classification: UNCLASSIFIED
        >
      • Ben White
        I would suggest there is no need for the else statement. This would suffice, and JSLint throws no errors. var a = function (x) { use strict ; if (typeof x ===
        Message 3 of 3 , Mar 4 10:16 AM
        • 0 Attachment
          I would suggest there is no need for the else statement.
          This would suffice, and JSLint throws no errors.

          var a = function (x) {
          "use strict";
          if (typeof x === "undefined") {
          return;
          }
          //complex and time consuming algorithmic logic here
          };

          Ben

          On Sun, Mar 4, 2012 at 12:00 PM, Cheney, Edward A SSG RES USAR USARC <
          austin.cheney@...> wrote:

          > **
          >
          >
          > Classification: UNCLASSIFIED
          > I cannot help but think this rule may be harmful. I understand the
          > intention behind this rule and how that intention may apply to more than
          > 90% of use cases, but consider the following example:
          >
          > var a = function (x) {
          > "use strict";
          > if (typeof x === "undefined") {
          > return;
          > } else {
          > //complex and time consuming algorithmic logic here
          > }
          > };
          >
          > In this case I am running a test. If the test fails I immediately exit
          > the function. The result of this early exit is that a whole host of
          > potential bugs and failures is avoided. Furthermore, the potential and
          > timely execution necessary to generate those potential bugs is also
          > avoided. If an error code is to be escaped from the function, such as
          > a try/catch scenario, then simply return a string explaining the error
          > instead of returning nothing.
          >
          > In my mind the most valuable use of the return keyword is the immediate
          > exit of a function. The fact that you can actually return a value using
          > this keyword is, at least my mind, merely serendipity. As a result I
          > view the return keyword as similar to the break keyword, but far more
          > powerful and without any contextual confusion.
          >
          > Is this style of expression invalid or harmful for any practical reason?
          >
          > Austin
          >
          > > I expect this is a common question, but searching for "this" isn't
          > getting me anywhere.
          > >
          > > "use strict";
          > >
          > > function foo()
          > > {
          > > var a = this;
          > > }
          > >
          > > var bar = function()
          > > {
          > > var a = this;
          > > };
          > >
          > > jslint likes bar, but not foo. Why?
          > >
          > >
          >
          > Classification: UNCLASSIFIED
          >
          >



          --
          Ben White


          [Non-text portions of this message have been removed]
        Your message has been successfully submitted and would be delivered to recipients shortly.