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

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

Expand Messages
  • Kent Davidson
    ... 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
    Message 1 of 24 , Mar 10, 2011
      >Am I making more sense now?

      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.

      Here's my take.

      If you can imagine a case when doc.eventListeners.add calls your anonymous function immediately, before returning a value, then it's an error. pdfdone is undefined, and pdfdone.remove() is an error. That's why I think the warning is right. A canonical example would be:

      (function () {
      var foo = setInterval(function() { clearInterval(foo); dosomethingelse(); }, 0);
      }());

      For the cases when the anonymous function is called immediately, I believe, the JSLint advice is, in general, good advice. Like I said, it's unreasonable and, more likely, futile, for someone to code a lint filter to determine if the function is called right away, or not.

      Most likely, in your case, "afterExport" is called much much later, in which case, it's really your judgement call. It will work. It will likely work 100% of the time. So you can just ignore the warning. 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

      So, if you want to fix the core of the issue, then be safe as you indicated, like so:

      (function() {
      var pdfdone = null;
      pdfdone = doc.eventListeners.add("afterExport", function(e) {
      if (e.format==="whatever") {
      if (pdfdone) {
      pdfdone.remove();
      }
      }
      // ...
      });
      }());

      And, like I said, you can simply agree with the error, but not in this case. Your call.

      Best,

      -Kent.

      On Mar 9, 2011, at 6:40 PM, John Hawkinson wrote:

      > Kent Davidson <kent@...> wrote on Wed, 9 Mar 2011
      > at 15:40:57 -0500 in <3E0C5AD2-D3BD-4B8F-B8BA-DF57B308A8B9@...>:
      >
      > > Good advice, bad advice. Matter of opinion. If you think it's bad
      > > advice, then ignore that warning. I set up my scripts to run through
      >
      > Well, help me out here. It seemed like bad advice to me, for the
      > reason I articulated (doesn't reduce the liklihood of a problem,
      > just makes JSLint stop warning). I'm curious why you disagree [and Douglas]?
      >
      > I'm happy to be convinced that it is good advice, but I'd like to know
      > why.
      >
      > > a local copy of jslint for testing. Any errors produced are
      > > considered blocking for shipping new versions. If I don't like a
      > > warning, I code the test to ignore the warning. You should do the
      > > same.
      >
      > Well, I definitely don't want to do that without understanding why
      > other people think the warning is right. Sometimes I might decide I
      > am right and Douglas/JSLint are wrong. But I want to make some
      > effort to be sure I'm gith before deciding that.
      >
      > > I agree, Douglas could go and rewrite his parsing engine to discover
      > > and detect when anonymous functions declared and assigned to the
      > > same var that are used within the function itself are OK,
      >
      > No, I agree that's way too much work. What I want is to be able to write:
      >
      > var pdfdone = doc.eventListeners.add("afterExport", function(e) {
      > /*jslint toleratenotdefined: true */
      > if (e.format==="whatever") {
      > pdfdone.remove();
      > }
      > }
      >
      > Though in the general case I'd like to be able to write:
      >
      > var pdfdone = doc.eventListeners.add("afterExport", function(e) {
      > if (e.format==="whatever") {
      > pdfdone.remove(); /*jslinted*/
      > }
      > }
      >
      > > Hence I think the absolute rule that in general, it's safer to
      > > simply declare the variable, then use it.
      >
      > Did you understand my point, though? Declaring the variable
      > does not confirm that it has a value before it is referenced.
      > It could well still be "undefined". And that would be equally bad.
      >
      > > If you disagree, I completely respect that. However, given his
      > > experience, and the fact that he wrote a lint program in JavaScript,
      > > I tend to follow his advice.
      >
      > I don't disagree. Quite the reverse. It's why I'm concerned that
      > your solution doesn't fix the real problem, it just makes the warning
      > go away. And there are plenty of cases where that's not safe.
      >
      > Am I making more sense now?
      >
      > --jhawk@...
      > John Hawkinson
      >



      [Non-text portions of this message have been removed]
    • 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 2 of 24 , Mar 10, 2011
        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 3 of 24 , Mar 10, 2011
          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 4 of 24 , Mar 10, 2011
            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 5 of 24 , Mar 10, 2011
              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 6 of 24 , Mar 10, 2011
                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 7 of 24 , Mar 10, 2011
                  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 8 of 24 , Mar 10, 2011
                    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 9 of 24 , Mar 10, 2011
                      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 10 of 24 , Mar 10, 2011
                        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.