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

Suggest Boolean() if !! is used?

Expand Messages
  • Joshua Bell
    In many C-like languages, !! (two applications of the unary negation operator) is used by developers as a to boolean pseudo-operator. This construct is
    Message 1 of 6 , Apr 25, 2011
    • 0 Attachment
      In many C-like languages, !! (two applications of the unary negation
      operator) is used by developers as a "to boolean" pseudo-operator. This
      construct is perfectly valid in JavaScript, but is IMHO less readable than
      using the intrinsic Boolean() function which yields the same result.

      In the interests of improving code readability and maintainability, should
      JSLint suggest Boolean() if !! is seen?

      -- Josh


      [Non-text portions of this message have been removed]
    • John Hawkinson
      Joshua Bell wrote on Mon, 25 Apr 2011 ... This is pointless overstepping into the personal style decisions. JSLint already does too much
      Message 2 of 6 , Apr 25, 2011
      • 0 Attachment
        Joshua Bell <josh@...> wrote on Mon, 25 Apr 2011
        at 09:18:41 -0700 in <BANLkTi=w3hmKhLxDaDTG0CWYObSYDjM-xw@...>:

        > In the interests of improving code readability and maintainability, should
        > JSLint suggest Boolean() if !! is seen?

        This is pointless overstepping into the personal style decisions.
        JSLint already does too much of that sort of thing, at least in several
        people's opinion.

        JSLint's job is to complain about language constructs that are dangerous,
        especially those that are non-obviously dangerous because of defects in
        the JavaScript language. That does not apply to !!.

        (The instant recommendation is also inconsistent with the implicit
        recommendation of "avoid Constructors() where feasible.")

        Indeed, I would love to see JSLint much more tolerant of reasonable
        but succint language constructs, like && and || forms as statements,
        etc., etc. E.g.: condition && action();

        --jhawk@...
        John Hawkinson
      • Luke Page
        I agree that !! Should be replaced with Boolean. 1. So !! Is not confused with ! 2. To be consistant with other warnings such as + should be String() I
        Message 3 of 6 , Apr 25, 2011
        • 0 Attachment
          I agree that !! Should be replaced with Boolean.

          1. So !! Is not confused with !
          2. To be consistant with other warnings such as +"" should be String()

          I thought this was already implemented, but seemingly not.
          On 25 Apr 2011 18:52, "John Hawkinson" <jhawk@...> wrote:
          > Joshua Bell <josh@...> wrote on Mon, 25 Apr 2011
          > at 09:18:41 -0700 in <BANLkTi=w3hmKhLxDaDTG0CWYObSYDjM-xw@...>:
          >
          >> In the interests of improving code readability and maintainability,
          should
          >> JSLint suggest Boolean() if !! is seen?
          >
          > This is pointless overstepping into the personal style decisions.
          > JSLint already does too much of that sort of thing, at least in several
          > people's opinion.
          >
          > JSLint's job is to complain about language constructs that are dangerous,
          > especially those that are non-obviously dangerous because of defects in
          > the JavaScript language. That does not apply to !!.
          >
          > (The instant recommendation is also inconsistent with the implicit
          > recommendation of "avoid Constructors() where feasible.")
          >
          > Indeed, I would love to see JSLint much more tolerant of reasonable
          > but succint language constructs, like && and || forms as statements,
          > etc., etc. E.g.: condition && action();
          >
          > --jhawk@...
          > John Hawkinson


          [Non-text portions of this message have been removed]
        • Erik Eckhardt
          !! vs ! doesn t confuse me, they re different enough in my mind. Consistency with other warnings is a net zero to me, because of warnings about not using named
          Message 4 of 6 , Apr 25, 2011
          • 0 Attachment
            !! vs ! doesn't confuse me, they're different enough in my mind.

            Consistency with other warnings is a net zero to me, because of warnings
            about not using named object constructors such as Array() or Boolean().

            Also, I thought we used string.toString() not String(string).

            Last, why use !! anyway? If the value is truthy then when used in a place
            where a Boolean is expected it is already implicitly converted to Boolean:

            if (!!document.documentElement) {
            }

            // doesn't this work just the same?

            if (document.documentElement) {
            }

            On Mon, Apr 25, 2011 at 2:31 PM, Luke Page <luke.a.page@...> wrote:

            >
            >
            > I agree that !! Should be replaced with Boolean.
            >
            > 1. So !! Is not confused with !
            > 2. To be consistant with other warnings such as +"" should be String()
            >
            > I thought this was already implemented, but seemingly not.
            >
            > On 25 Apr 2011 18:52, "John Hawkinson" <jhawk@...> wrote:
            > > Joshua Bell <josh@...> wrote on Mon, 25 Apr 2011
            > > at 09:18:41 -0700 in <BANLkTi=w3hmKhLxDaDTG0CWYObSYDjM-xw@...
            > >:
            > >
            > >> In the interests of improving code readability and maintainability,
            > should
            > >> JSLint suggest Boolean() if !! is seen?
            > >
            > > This is pointless overstepping into the personal style decisions.
            > > JSLint already does too much of that sort of thing, at least in several
            > > people's opinion.
            > >
            > > JSLint's job is to complain about language constructs that are dangerous,
            > > especially those that are non-obviously dangerous because of defects in
            > > the JavaScript language. That does not apply to !!.
            > >
            > > (The instant recommendation is also inconsistent with the implicit
            > > recommendation of "avoid Constructors() where feasible.")
            > >
            > > Indeed, I would love to see JSLint much more tolerant of reasonable
            > > but succint language constructs, like && and || forms as statements,
            > > etc., etc. E.g.: condition && action();
            > >
            > > --jhawk@...
            > > John Hawkinson
            >
            > [Non-text portions of this message have been removed]
            >
            >
            >


            [Non-text portions of this message have been removed]
          • Erik Eckhardt
            My apologies, I meant number.toString() not String(number). ... [Non-text portions of this message have been removed]
            Message 5 of 6 , Apr 25, 2011
            • 0 Attachment
              My apologies, I meant number.toString() not String(number).

              On Mon, Apr 25, 2011 at 3:20 PM, Erik Eckhardt <erik@...> wrote:

              > !! vs ! doesn't confuse me, they're different enough in my mind.
              >
              > Consistency with other warnings is a net zero to me, because of warnings
              > about not using named object constructors such as Array() or Boolean().
              >
              > Also, I thought we used string.toString() not String(string).
              >
              > Last, why use !! anyway? If the value is truthy then when used in a place
              > where a Boolean is expected it is already implicitly converted to Boolean:
              >
              > if (!!document.documentElement) {
              > }
              >
              > // doesn't this work just the same?
              >
              > if (document.documentElement) {
              >
              > }
              >
              > On Mon, Apr 25, 2011 at 2:31 PM, Luke Page <luke.a.page@...> wrote:
              >
              >>
              >>
              >> I agree that !! Should be replaced with Boolean.
              >>
              >> 1. So !! Is not confused with !
              >> 2. To be consistant with other warnings such as +"" should be String()
              >>
              >> I thought this was already implemented, but seemingly not.
              >>
              >> On 25 Apr 2011 18:52, "John Hawkinson" <jhawk@...> wrote:
              >> > Joshua Bell <josh@...> wrote on Mon, 25 Apr 2011
              >> > at 09:18:41 -0700 in <
              >> BANLkTi=w3hmKhLxDaDTG0CWYObSYDjM-xw@...>:
              >> >
              >> >> In the interests of improving code readability and maintainability,
              >> should
              >> >> JSLint suggest Boolean() if !! is seen?
              >> >
              >> > This is pointless overstepping into the personal style decisions.
              >> > JSLint already does too much of that sort of thing, at least in several
              >> > people's opinion.
              >> >
              >> > JSLint's job is to complain about language constructs that are
              >> dangerous,
              >> > especially those that are non-obviously dangerous because of defects in
              >> > the JavaScript language. That does not apply to !!.
              >> >
              >> > (The instant recommendation is also inconsistent with the implicit
              >> > recommendation of "avoid Constructors() where feasible.")
              >> >
              >> > Indeed, I would love to see JSLint much more tolerant of reasonable
              >> > but succint language constructs, like && and || forms as statements,
              >> > etc., etc. E.g.: condition && action();
              >> >
              >> > --jhawk@...
              >> > John Hawkinson
              >>
              >> [Non-text portions of this message have been removed]
              >>
              >>
              >>
              >
              >


              [Non-text portions of this message have been removed]
            • Joshua Bell
              ... When not in a new context, Boolean(), Number(), String() and Object() perform type conversions rather than constructing objects. (This is different than
              Message 6 of 6 , Apr 25, 2011
              • 0 Attachment
                On Mon, Apr 25, 2011 at 3:20 PM, Erik Eckhardt <erik@...> wrote:

                > !! vs ! doesn't confuse me, they're different enough in my mind.
                >
                > Consistency with other warnings is a net zero to me, because of warnings
                > about not using named object constructors such as Array() or Boolean().
                >
                > Also, I thought we used string.toString() not String(string).
                >

                When not in a "new" context, Boolean(), Number(), String() and Object()
                perform type conversions rather than constructing objects. (This is
                different than Function, Error and Array, which have the same behavior when
                called as a function or called as a constructor) These generally match the
                ECMAScript internal ToXXX function behavior. If you're building libraries
                that you want to function like the ECMAScript intrinsics, these are useful.
                Calling number.toString() is great if you know that you're dealing with a
                number primitive, but if you want the String value (or Number value) of an
                argument that may be of unknown type (including null or undefined) then
                String(arg) matches what ECMAScript functions tend to do internally.

                Within an application you may know that data is of a given type, and thus
                calling e.g. number.toString() is acceptable. On the other extreme, if
                you're handling user-generated data then parseFloat(string) may be what you
                want (modulo locale issues) given how it handles leading whitespace/trailing
                garbage, whereas for a library you may want the Number(arg) conversion
                function for the more straightforward behavior.

                IMHO, of course.


                > Last, why use !! anyway? If the value is truthy then when used in a place
                > where a Boolean is expected it is already implicitly converted to Boolean:
                >
                > if (!!document.documentElement) {
                > }
                >
                > // doesn't this work just the same?
                >
                > if (document.documentElement) {
                > }
                >

                Agreed that using !! or Boolean() in an "if" test is not "good JavaScript"
                since it adds nothing and potentially implies a misunderstanding of the
                language. But if you're storing the data (for later use, transmission to
                another process, etc) you may want to be sure it's a boolean value rather
                than the object itself.

                I'm not expecting that !! gets used very frequently in JavaScript but I ran
                across an old usage in some of my code that I would replace with Boolean()
                now, so thought I'd toss this out.

                -- Josh


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