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

Re: [jslint] Re: has not been fully defined yet

Expand Messages
  • John Hawkinson
    Kent Davidson wrote on Thu, 10 Mar 2011 ... But making the proposed change is no safer than the original code! I agree the warning is
    Message 1 of 24 , Mar 10, 2011
    • 0 Attachment
      Kent Davidson <kent@...> wrote on Thu, 10 Mar 2011
      at 09:18:32 -0500 in <A45E4A5D-C2C5-43FA-922F-C23C6DBE1D2B@...>:

      > Hm. Well. I think so, but you say you want to write it a certain
      > way, yet that way is flagged as an error. So, if you agree with
      > JSLint, then you'd change the code.

      But making the proposed change is no safer than the original code!
      I agree the warning is right.
      Your 3rd solution is better, though, thank you!

      To summarize, (in abbreviated form; assume a function() wrapper),
      JSLint warns us about:

      var foo = bar(function() { foo.m(); }); // Case 1

      You suggested the solution is to use:

      var foo;
      foo = bar(function() { foo.m(); }); // Case 2

      I'm concerned that doesn't solve anything other than JSLint's warning.
      If foo() might have been invalid in Case 1, it's just as invalid in Case 2.

      A future version of JSLint would be just as right to warn about Case 2
      as Case 1.

      I agree the better solution is to use:

      var foo;
      foo = bar(function() { foo && foo.m(); }); // Case 3

      Except JSLint doesn't like the guard operator on function calls
      ("Expected an assignment or function call and instead saw an expression.")
      so we must write:

      var foo;
      foo = bar(function() { if (foo) { foo.m(); } }); // Case 4

      I do have to say, it makes me sad that the in the name of avoiding standalone
      expressions, we have to add an extra level of braces that make the code
      harder to read.

      => I wonder if its asking too much for JSLint to permit
      the guard operator (&&) on function calls. Thoughts?




      Actually, you proposed:

      var foo = null;
      foo = bar(function() { if (foo) { foo.m(); } }); // Case 5

      but why bother to initialize foo to null? The truthiness of null and
      and undefined is the same...

      > My point previously was that regardless of warnings or not, if a
      > small tweak in my code will eliminate the warning, I err on the side
      > of just making the cosmetic change, even if I know the warning

      Looks like you didn't finish that sentence...

      --jhawk@...
      John Hawkinson
    • Kent Davidson
      ... True, it s not necessary in this case. ... I err on the side of just making the cosmetic change, even if I know the warning is not really going to affect
      Message 2 of 24 , Mar 10, 2011
      • 0 Attachment
        On Mar 10, 2011, at 9:55 AM, John Hawkinson wrote:

        > but why bother to initialize foo to null? The truthiness of null and
        > and undefined is the same...

        True, it's not necessary in this case.

        > > My point previously was that regardless of warnings or not, if a
        > > small tweak in my code will eliminate the warning, I err on the side
        > > of just making the cosmetic change, even if I know the warning
        >
        > Looks like you didn't finish that sentence...

        I err on the side of just making the cosmetic change, even if I know the warning is not really going to affect the code.

        > I do have to say, it makes me sad that the in the name of avoiding standalone
        > expressions, we have to add an extra level of braces that make the code
        > harder to read.

        Anonymous functions are generally hard to read, esp. when on the same line. I use http://prettydiff.com if presented with something which is hard to read. It's another awesome tool to make code more readable.

        foo && foo.m()

        is a nice construct, but the assumption is that jslint is trying to avoid the issue where you want to assign something, but forgot to. So,

        var x = foo && foo.m();

        validates, but

        foo && foo.m()

        doesn't. It could be an error if you actually did want to use the expression result, which && returns.

        And:

        if (foo) {
        foo.m();
        }

        returns nothing, which is what is wanted in this case.

        As for harder to read, we're really nitting picks at this point.

        Cheers,

        -Kent.
        --
        Kent Davidson
        Market Ruler, LLC
        Marketing Power Tools
        http://marketruler.com/?_cr=efoot
        +1 866-622-8636 x88



        [Non-text portions of this message have been removed]
      • abyssoft@ymail.com
        In response to the last question, ... ********** The Truthiness of null and undefined are not the same. while null == undefined is true, null === undefined is
        Message 3 of 24 , Mar 10, 2011
        • 0 Attachment
          In response to the last question,

          **********
          --- In jslint_com@yahoogroups.com, John Hawkinson <jhawk@...> wrote:
          > Actually, you proposed:
          >
          > var foo = null;
          > foo = bar(function() { if (foo) { foo.m(); } }); // Case 5
          >
          > but why bother to initialize foo to null? The truthiness of null and
          > and undefined is the same...
          **********

          The Truthiness of null and undefined are not the same.
          while null == undefined is true, null === undefined is false; if the Truthiness was the same then null === undefined would be true.
          Also the intent of null and undefined are different in nature. When a variable is declared it is by definition is undefined. Which is to say it is not empty, but rather that it has yet to be defined. Null is a empty state, think of Null in terms of math with help with this concept as null is also known as the empty set. Undefined in math terms and null (empty) in math terms are not equivalent (undefined !== null) but they are similar (undefined == null).


          Declare but leave uninitialized

          var someVariable1; // assignment to undefined is understood
          var someVariable2 = undefined; // does the same as example 1 but will throw lint warning, use example 1

          someVariable1 = 123; // assign value to variable
          someVariable1 = undefined; // destruct variable but leave declared

          Declare and initialize to empty
          var someVariable3 = null;
          someVariable1 = 123; // assign value to variable
          someVariable1 = null; // empty variable


          Hope this short post clears up confusion between undefined and null.
        • Satyam
          I would like to add that while undefined means I don t have a clue what the value might be , null means I know it has no value (a known unknown :-P ).
          Message 4 of 24 , Mar 10, 2011
          • 0 Attachment
            I would like to add that while undefined means "I don't have a clue what
            the value might be", null means "I know it has no value" (a known
            unknown :-P ). Beyond a certain point in the application, undefined
            might mean there is an error, null means I know it has no value.

            If more languages had null and undefined we wouldn't have silly
            conventions like indexOf returning -1 when an item was not found. In
            JavaScript it is an unfortunate legacy of other languages. Ideally, you
            could have:

            var position = 'abc'.indexOf('a') sets position to 0
            var position = 'abc'.indexOf('x') should set position to null, not -1
            var position = null.indexOf('a') should leave position undefined
            (assuming the error was not caught)

            All those (0, null and undefined) are falsy but they are far from
            meaning the same. The last one shows why I said that a variable set to
            undefined might signal an error when found beyond a certain point.

            In the second case, returning false would also be an arbitrary
            convention, just as -1 is. It might be agreed that false means 'it is
            not there' but indexOf asks for the position of the item sought, not if
            it is there (where false would be a logical reply) and if it is not
            there, it has no position, no such thing, thus null, not false. Anyway,
            this is beyond the point.

            Satyam


            El 10/03/2011 17:53, abyssoft@... escribió:
            >
            > In response to the last question,
            >
            > **********
            > --- In jslint_com@yahoogroups.com
            > <mailto:jslint_com%40yahoogroups.com>, John Hawkinson <jhawk@...> wrote:
            > > Actually, you proposed:
            > >
            > > var foo = null;
            > > foo = bar(function() { if (foo) { foo.m(); } }); // Case 5
            > >
            > > but why bother to initialize foo to null? The truthiness of null and
            > > and undefined is the same...
            > **********
            >
            > The Truthiness of null and undefined are not the same.
            > while null == undefined is true, null === undefined is false; if the
            > Truthiness was the same then null === undefined would be true.
            > Also the intent of null and undefined are different in nature. When a
            > variable is declared it is by definition is undefined. Which is to say
            > it is not empty, but rather that it has yet to be defined. Null is a
            > empty state, think of Null in terms of math with help with this
            > concept as null is also known as the empty set. Undefined in math
            > terms and null (empty) in math terms are not equivalent (undefined !==
            > null) but they are similar (undefined == null).
            >
            > Declare but leave uninitialized
            >
            > var someVariable1; // assignment to undefined is understood
            > var someVariable2 = undefined; // does the same as example 1 but will
            > throw lint warning, use example 1
            >
            > someVariable1 = 123; // assign value to variable
            > someVariable1 = undefined; // destruct variable but leave declared
            >
            > Declare and initialize to empty
            > var someVariable3 = null;
            > someVariable1 = 123; // assign value to variable
            > someVariable1 = null; // empty variable
            >
            > Hope this short post clears up confusion between undefined and null.
            >
            >
            > ------------------------------------------------------------------------
            >
            > No virus found in this message.
            > Checked by AVG - www.avg.com <http://www.avg.com>
            > Version: 10.0.1204 / Virus Database: 1497/3495 - Release Date: 03/09/11
            >


            [Non-text portions of this message have been removed]
          • Lindsay John Lawrence
            Hello,       Problem at line 9 character 7: Unexpected continue . I now get an error like the above with loops that have a continue of the form shown
            Message 5 of 24 , Mar 10, 2011
            • 0 Attachment
              Hello,

                    Problem at line 9 character 7: Unexpected 'continue'.


              I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.

              Is this a bug in jslint or is it now enforcing some form I am not aware of?

              // --- start sample code ---
              var nop = function () {
                return;
              };

              var test = function () {
                var i;
                for (i=0; i<10; i+=1) {
                  if (i<5) {
                    continue;
                  }
                  nop();
                }
              };
              // --- end sample code ---

              Thanks in advancet,

              Lindsay





              [Non-text portions of this message have been removed]
            • spence.randall@ymail.com
              It s not a bug, JSLint has a new(ish) Tolerate continue option. You can check that box in options, or add /*jslint continue:true*/ to your code. Or you could
              Message 6 of 24 , Mar 10, 2011
              • 0 Attachment
                It's not a bug, JSLint has a new(ish) "Tolerate continue" option. You can check that box in options, or add /*jslint continue:true*/ to your code.

                Or you could refactor your code to avoid the use of continue. In your example, you could just set i = 5 in the loop and drop the if check, or check if i > 5 then run nop().

                -Randall

                --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@...> wrote:
                >
                > Hello,
                >
                >       Problem at line 9 character 7: Unexpected 'continue'.
                >
                >
                > I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.
                >
                > Is this a bug in jslint or is it now enforcing some form I am not aware of?
                >
                > // --- start sample code ---
                > var nop = function () {
                >   return;
                > };
                >
                > var test = function () {
                >   var i;
                >   for (i=0; i<10; i+=1) {
                >     if (i<5) {
                >       continue;
                >     }
                >     nop();
                >   }
                > };
                > // --- end sample code ---
                >
                > Thanks in advancet,
                >
                > Lindsay
                >
                >
                >
                >
                >
                > [Non-text portions of this message have been removed]
                >
              • Lindsay John Lawrence
                Thanks! Why the change though? Is continue being deprecated in the language? --Lindsay ... From: spence.randall@ymail.com Subject:
                Message 7 of 24 , Mar 10, 2011
                • 0 Attachment
                  Thanks! Why the change though? Is 'continue' being deprecated in the language?

                  --Lindsay


                  --- On Thu, 3/10/11, spence.randall@... <randall@...> wrote:

                  From: spence.randall@... <randall@...>
                  Subject: [jslint] Re: JSLint Bug?
                  To: jslint_com@yahoogroups.com
                  Date: Thursday, March 10, 2011, 11:00 AM







                   













                  It's not a bug, JSLint has a new(ish) "Tolerate continue" option. You can check that box in options, or add /*jslint continue:true*/ to your code.



                  Or you could refactor your code to avoid the use of continue. In your example, you could just set i = 5 in the loop and drop the if check, or check if i > 5 then run nop().



                  -Randall



                  --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@...> wrote:

                  >

                  > Hello,

                  >

                  >       Problem at line 9 character 7: Unexpected 'continue'.

                  >

                  >

                  > I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.

                  >

                  > Is this a bug in jslint or is it now enforcing some form I am not aware of?

                  >

                  > // --- start sample code ---

                  > var nop = function () {

                  >   return;

                  > };

                  >

                  > var test = function () {

                  >   var i;

                  >   for (i=0; i<10; i+=1) {

                  >     if (i<5) {

                  >       continue;

                  >     }

                  >     nop();

                  >   }

                  > };

                  > // --- end sample code ---

                  >

                  > Thanks in advancet,

                  >

                  > Lindsay

                  >

                  >

                  >

                  >

                  >

                  > [Non-text portions of this message have been removed]

                  >

























                  [Non-text portions of this message have been removed]
                • spence.randall@ymail.com
                  Deprecated? No, I don t think so. There are almost always better ways of writing statements that more explicitly define what you are attempting to do without
                  Message 8 of 24 , Mar 10, 2011
                  • 0 Attachment
                    Deprecated? No, I don't think so. There are almost always better ways of writing statements that more explicitly define what you are attempting to do without resorting to continue. JSLint is all about the good parts, and not about the parts that are acceptable. It forces you to use a higher standard than the one defined.

                    Douglas says it best in his book:

                    "The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement."

                    -Randall

                    --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@...> wrote:
                    >
                    > Thanks! Why the change though? Is 'continue' being deprecated in the language?
                    >
                    > --Lindsay
                    >
                    >
                    > --- On Thu, 3/10/11, spence.randall@... <randall@...> wrote:
                    >
                    > From: spence.randall@... <randall@...>
                    > Subject: [jslint] Re: JSLint Bug?
                    > To: jslint_com@yahoogroups.com
                    > Date: Thursday, March 10, 2011, 11:00 AM
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >  
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    > It's not a bug, JSLint has a new(ish) "Tolerate continue" option. You can check that box in options, or add /*jslint continue:true*/ to your code.
                    >
                    >
                    >
                    > Or you could refactor your code to avoid the use of continue. In your example, you could just set i = 5 in the loop and drop the if check, or check if i > 5 then run nop().
                    >
                    >
                    >
                    > -Randall
                    >
                    >
                    >
                    > --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@> wrote:
                    >
                    > >
                    >
                    > > Hello,
                    >
                    > >
                    >
                    > >       Problem at line 9 character 7: Unexpected 'continue'.
                    >
                    > >
                    >
                    > >
                    >
                    > > I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.
                    >
                    > >
                    >
                    > > Is this a bug in jslint or is it now enforcing some form I am not aware of?
                    >
                    > >
                    >
                    > > // --- start sample code ---
                    >
                    > > var nop = function () {
                    >
                    > >   return;
                    >
                    > > };
                    >
                    > >
                    >
                    > > var test = function () {
                    >
                    > >   var i;
                    >
                    > >   for (i=0; i<10; i+=1) {
                    >
                    > >     if (i<5) {
                    >
                    > >       continue;
                    >
                    > >     }
                    >
                    > >     nop();
                    >
                    > >   }
                    >
                    > > };
                    >
                    > > // --- end sample code ---
                    >
                    > >
                    >
                    > > Thanks in advancet,
                    >
                    > >
                    >
                    > > Lindsay
                    >
                    > >
                    >
                    > >
                    >
                    > >
                    >
                    > >
                    >
                    > >
                    >
                    > > [Non-text portions of this message have been removed]
                    >
                    > >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    >
                    > [Non-text portions of this message have been removed]
                    >
                  • Lindsay John Lawrence
                    Thanks! I have read that book cover to cover several times and it continues to be an excellent reference... somehow I missed that statement of his though. ...
                    Message 9 of 24 , Mar 10, 2011
                    • 0 Attachment
                      Thanks! I have read that book cover to cover several times and it continues to be an excellent reference... somehow I missed that statement of his though.

                      --- On Thu, 3/10/11, spence.randall@... <randall@...> wrote:

                      From: spence.randall@... <randall@...>
                      Subject: [jslint] Re: JSLint Bug?
                      To: jslint_com@yahoogroups.com
                      Date: Thursday, March 10, 2011, 11:47 AM







                       













                      Deprecated? No, I don't think so. There are almost always better ways of writing statements that more explicitly define what you are attempting to do without resorting to continue. JSLint is all about the good parts, and not about the parts that are acceptable. It forces you to use a higher standard than the one defined.



                      Douglas says it best in his book:



                      "The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement."



                      -Randall



                      --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@...> wrote:

                      >

                      > Thanks! Why the change though? Is 'continue' being deprecated in the language?

                      >

                      > --Lindsay

                      >

                      >

                      > --- On Thu, 3/10/11, spence.randall@... <randall@...> wrote:

                      >

                      > From: spence.randall@... <randall@...>

                      > Subject: [jslint] Re: JSLint Bug?

                      > To: jslint_com@yahoogroups.com

                      > Date: Thursday, March 10, 2011, 11:00 AM

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >  

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      > It's not a bug, JSLint has a new(ish) "Tolerate continue" option. You can check that box in options, or add /*jslint continue:true*/ to your code.

                      >

                      >

                      >

                      > Or you could refactor your code to avoid the use of continue. In your example, you could just set i = 5 in the loop and drop the if check, or check if i > 5 then run nop().

                      >

                      >

                      >

                      > -Randall

                      >

                      >

                      >

                      > --- In jslint_com@yahoogroups.com, Lindsay John Lawrence <thinknlive@> wrote:

                      >

                      > >

                      >

                      > > Hello,

                      >

                      > >

                      >

                      > >       Problem at line 9 character 7: Unexpected 'continue'.

                      >

                      > >

                      >

                      > >

                      >

                      > > I now get an error like the above with loops that have a 'continue' of the form shown in the sample code below.

                      >

                      > >

                      >

                      > > Is this a bug in jslint or is it now enforcing some form I am not aware of?

                      >

                      > >

                      >

                      > > // --- start sample code ---

                      >

                      > > var nop = function () {

                      >

                      > >   return;

                      >

                      > > };

                      >

                      > >

                      >

                      > > var test = function () {

                      >

                      > >   var i;

                      >

                      > >   for (i=0; i<10; i+=1) {

                      >

                      > >     if (i<5) {

                      >

                      > >       continue;

                      >

                      > >     }

                      >

                      > >     nop();

                      >

                      > >   }

                      >

                      > > };

                      >

                      > > // --- end sample code ---

                      >

                      > >

                      >

                      > > Thanks in advancet,

                      >

                      > >

                      >

                      > > Lindsay

                      >

                      > >

                      >

                      > >

                      >

                      > >

                      >

                      > >

                      >

                      > >

                      >

                      > > [Non-text portions of this message have been removed]

                      >

                      > >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      >

                      > [Non-text portions of this message have been removed]

                      >

























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