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

Re: Unexpected else after return

Expand Messages
  • douglascrockford
    ... Please see http://llvm.org/docs/CodingStandards.html#hl_else_after_return
    Message 1 of 9 , Feb 28, 2012
      --- In jslint_com@yahoogroups.com, Kirk Cerny <kirksemail@...> wrote:
      >
      > Thanks for supporting JsLint. I really enjoy using it.
      >
      > Is this a new rule?
      > I get this error now.
      > Can someone please explain the reason behind the rule?

      Please see http://llvm.org/docs/CodingStandards.html#hl_else_after_return
    • Mark Volkmann
      In case it s not obvious, you can just do this: return num === 1; ... R. Mark Volkmann Object Computing, Inc. On Feb 28, 2012, at 12:30 PM, Kirk Cerny
      Message 2 of 9 , Feb 28, 2012
        In case it's not obvious, you can just do this:

        return num === 1;

        ---
        R. Mark Volkmann
        Object Computing, Inc.

        On Feb 28, 2012, at 12:30 PM, Kirk Cerny <kirksemail@...> wrote:



        Thanks for supporting JsLint. I really enjoy using it.

        Is this a new rule?
        I get this error now.
        Can someone please explain the reason behind the rule?

        Error:
        Problem at line 4 character 9: Unexpected 'else' after 'return'.

        return true;

        Code Example:

        var isOne = function (num) {
        'use strict';
        if (num === 1) {
        return true;
        } else {
        return false;
        }
        };



        [Non-text portions of this message have been removed]
      • Erik Eckhardt
        Down with goto Naziism! I mean, seriously. Go to a particular end point buried in the middle of code? Yes, that should be verboten. But many, many
        Message 3 of 9 , Feb 28, 2012
          Down with "goto" Naziism!

          I mean, seriously. Go to a particular end point buried in the middle of
          code? Yes, that should be verboten.

          But many, many programmers prefer being able to *return* in some
          situations, when they see is a shorter, clearer, or cleaner.

          Why force declaring a separate variable to carry meaning forward to the
          end, if the code is so plain that all the branches from the if statement
          return? There is no reason, it isn't for clarity, it isn't for avoiding
          bugs, it's just for dictatorial adherence to an ill-thought absolute rule.

          Erik

          On Tue, Feb 28, 2012 at 10:47 AM, Doc Emmett Splendid <
          emmett.thesane@...> wrote:

          > **
          >
          >
          > Using multiple returns is equivalent to a goto. It's best to collect your
          > tests and pass the result in a single return.
          >
          > ________________________________
          > From: Kirk Cerny <kirksemail@...>
          > To: jslint_com@yahoogroups.com
          > Sent: Tuesday, 28 February 2012, 10:30
          > Subject: [jslint] Unexpected else after return
          >
          >
          >
          >
          > Thanks for supporting JsLint. I really enjoy using it.
          >
          > Is this a new rule?
          > I get this error now.
          > Can someone please explain the reason behind the rule?
          >
          > Error:
          > Problem at line 4 character 9: Unexpected 'else' after 'return'.
          >
          > return true;
          >
          > Code Example:
          >
          > var isOne = function (num) {
          > 'use strict';
          > if (num === 1) {
          > return true;
          > } else {
          > return false;
          > }
          > };
          >
          > [Non-text portions of this message have been removed]
          >
          >
          >


          [Non-text portions of this message have been removed]
        • spence.randall@ymail.com
          Just in case this was an overly simplified function to illustrate the error message, another solution would be to simply remove the unnecessary else : var
          Message 4 of 9 , Feb 28, 2012
            Just in case this was an overly simplified function to illustrate the error message, another solution would be to simply remove the unnecessary 'else':

            var isOne = function (num) {
            'use strict';
            if (num === 1) {
            return true;
            }
            return false;
            };

            But if it wasn't, Mark's solution is the superior one.

            -Randall

            --- In jslint_com@yahoogroups.com, Mark Volkmann <r.mark.volkmann@...> wrote:
            >
            > In case it's not obvious, you can just do this:
            >
            > return num === 1;
            >
            > ---
            > R. Mark Volkmann
            > Object Computing, Inc.
            >
            > On Feb 28, 2012, at 12:30 PM, Kirk Cerny <kirksemail@...> wrote:
            >
            >
            >
            > Thanks for supporting JsLint. I really enjoy using it.
            >
            > Is this a new rule?
            > I get this error now.
            > Can someone please explain the reason behind the rule?
            >
            > Error:
            > Problem at line 4 character 9: Unexpected 'else' after 'return'.
            >
            > return true;
            >
            > Code Example:
            >
            > var isOne = function (num) {
            > 'use strict';
            > if (num === 1) {
            > return true;
            > } else {
            > return false;
            > }
            > };
            >
            >
            >
            > [Non-text portions of this message have been removed]
            >
          • Erik Eckhardt
            Douglas, You may not wish to use llvm.org as a resource for coding standards, as the same page suggests the use of early
            Message 5 of 9 , Feb 28, 2012
              Douglas,

              You may not wish to use llvm.org as a resource for coding standards, as the
              same page suggests the use of early
              exits<http://llvm.org/docs/CodingStandards.html#hl_earlyexit>
              .

              For example, I know you do not like constructions like the following
              (though I do):

              for (var k in obj) {
              ___if (!obj.hasOwnProperty) { break; }
              ___//rest of long loop;
              }

              On Tue, Feb 28, 2012 at 11:14 AM, douglascrockford <douglas@...>wrote:

              > **
              >
              >
              > --- In jslint_com@yahoogroups.com, Kirk Cerny <kirksemail@...> wrote:
              > >
              > > Thanks for supporting JsLint. I really enjoy using it.
              > >
              > > Is this a new rule?
              > > I get this error now.
              > > Can someone please explain the reason behind the rule?
              >
              > Please see http://llvm.org/docs/CodingStandards.html#hl_else_after_return
              >
              >
              >


              [Non-text portions of this message have been removed]
            • douglascrockford
              ... I adopted the ones that made sense.
              Message 6 of 9 , Feb 28, 2012
                --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:

                > You may not wish to use llvm.org as a resource for coding standards, as the
                > same page suggests the use of early
                > exits<http://llvm.org/docs/CodingStandards.html#hl_earlyexit>
                > .

                I adopted the ones that made sense.
              • Martin Cooper
                ... Where can we find that list? It s important that we can know the rules up front, and not discover them only when JSLint complains. -- Martin Cooper
                Message 7 of 9 , Mar 3, 2012
                  On Tue, Feb 28, 2012 at 6:15 PM, douglascrockford <douglas@...> wrote:
                  > --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
                  >
                  >> You may not wish to use llvm.org as a resource for coding standards, as the
                  >> same page suggests the use of early
                  >> exits<http://llvm.org/docs/CodingStandards.html#hl_earlyexit>
                  >> .
                  >
                  > I adopted the ones that made sense.

                  Where can we find that list? It's important that we can know the rules
                  up front, and not discover them only when JSLint complains.

                  --
                  Martin Cooper


                  > ------------------------------------
                  >
                  > Yahoo! Groups Links
                  >
                  >
                  >
                Your message has been successfully submitted and would be delivered to recipients shortly.