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

indexOf in an If clause

Expand Messages
  • Simon Kenyon Shepard
    I was recently cleaning up some legacy code, looking for an intermittent bug and I encountered the following code: var data = someData ; if
    Message 1 of 7 , Apr 23, 2011
    • 0 Attachment
      I was recently cleaning up some legacy code, looking for an intermittent bug
      and I encountered the following code:

      var data = "someData";
      if (data.indexOf("e")) {
      //do something here
      }

      Now, in 99.9% of cases the string was the same so the �e� was present and
      the code entered into the condition and did the right thing.
      However, in the remaining 0.1% the code looked like this:

      var data = "enterSomeData";
      if(data.indexOf("e")) {
      //do something here
      }

      This scenario would fail, but it�s not at first glance obvious why. Until I
      remembered that indexOf returns the position of the match, which in this
      case was 0 and evaluated to false.

      The bug was primarily hiding because there were never any cases where the
      �e� was not present at all, and so arguably, you could remove the whole if
      cause as it�s unnecessary. However, the person who wrote the code must have
      assumed that indexOf returns a falsey value if the �e� is not present and a
      truthy value if it is, which is a not so unimaginable leap and indeed, the
      same mistake that I first made when I looked at the code.

      So for my book, having a indexOf in an if condition without a comparison
      i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
      not paying attention.



      --
      "We are the music makers, And we are the dreamers of dreams"


      [Non-text portions of this message have been removed]
    • Douglas Crockford
      ... Please try it now. Are there any other standard functions with similar hazards?
      Message 2 of 7 , Apr 24, 2011
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@...> wrote:

        > So for my book, having a indexOf in an if condition without a comparison
        > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
        > not paying attention.

        Please try it now.

        Are there any other standard functions with similar hazards?
      • Merlin
        ... lastIndexOf() has the same issue.
        Message 3 of 7 , Apr 24, 2011
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
          >
          > --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@> wrote:
          >
          > > So for my book, having a indexOf in an if condition without a comparison
          > > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
          > > not paying attention.
          >
          > Please try it now.
          >
          > Are there any other standard functions with similar hazards?
          >

          lastIndexOf() has the same issue.
        • Merlin
          ... search(regexp) may have the same issue.
          Message 4 of 7 , Apr 24, 2011
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
            >
            > --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@> wrote:
            >
            > > So for my book, having a indexOf in an if condition without a comparison
            > > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
            > > not paying attention.
            >
            > Please try it now.
            >
            > Are there any other standard functions with similar hazards?
            >

            search(regexp) may have the same issue.
          • Jordan
            ... `if (!a.indexOf( c ))` and `if (!!a.indexOf( c ))` both do not trigger the weird condition error. Is it worth also checking that Array.length is always
            Message 5 of 7 , Apr 24, 2011
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
              >
              > --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@> wrote:
              >
              > > So for my book, having a indexOf in an if condition without a comparison
              > > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
              > > not paying attention.
              >
              > Please try it now.
              >
              > Are there any other standard functions with similar hazards?
              >

              `if (!a.indexOf('c'))` and `if (!!a.indexOf('c'))` both do not trigger the "weird condition" error. Is it worth also checking that Array.length is always used in an integer context and not a boolean one?
            • Simon Kenyon Shepard
              works great. Thanks. ... -- We are the music makers, And we are the dreamers of dreams [Non-text portions of this message have been removed]
              Message 6 of 7 , Apr 24, 2011
              • 0 Attachment
                works great. Thanks.

                On 24 April 2011 18:52, Douglas Crockford <douglas@...> wrote:

                >
                >
                > --- In jslint_com@yahoogroups.com, Simon Kenyon Shepard <simon.shepard@...>
                > wrote:
                >
                > > So for my book, having a indexOf in an if condition without a comparison
                > > i.e. > 0 or !==-1 is a bad part with the potential to trip you up if your
                > > not paying attention.
                >
                > Please try it now.
                >
                > Are there any other standard functions with similar hazards?
                >
                >
                >



                --
                "We are the music makers, And we are the dreamers of dreams"


                [Non-text portions of this message have been removed]
              • Douglas Crockford
                ... Yes, I think they should. ... No, I don t think so. The confusion with indexOf is that the not-found value is truthy, and only one of the found values is
                Message 7 of 7 , Apr 24, 2011
                • 0 Attachment
                  --- In jslint_com@yahoogroups.com, "Jordan" <ljharb@...> wrote:
                  > `if (!a.indexOf('c'))` and `if (!!a.indexOf('c'))` both do not trigger the "weird condition" error.

                  Yes, I think they should.

                  > Is it worth also checking that Array.length is always used in an integer context and not a boolean one?

                  No, I don't think so. The confusion with indexOf is that the not-found value is truthy, and only one of the found values is falsy. length does not have that confusion.
                Your message has been successfully submitted and would be delivered to recipients shortly.