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

Re: [jslint] Re: option wish for single line if

Expand Messages
  • Cheney, Edward A SSG RES USAR USARC
    ... There is no middle ground. Either beautify your code in conformance to a rule set or don t. It may help if you use an automated beautifier that conforms
    Message 1 of 14 , Feb 22 10:33 PM
    • 0 Attachment
      > Thanks! I see that that works. I wish there was a middle ground though. Even
      > though I want to be able to write "if (!someVariable) { return; }", I still
      > care that my code is indented properly and don't want to disable that
      > checking.

      There is no middle ground. Either beautify your code in conformance to a rule set or don't. It may help if you use an automated beautifier that conforms to that rule set.

      Additionally, I have found that testing for a variable name is less reliable than testing for a variable value unless your objective is to determine if that namespace exists within a given inheritable scope. I would recommend setting all your variable names at the top of your function and then test for undefined.

      if (varName === undefined) {return;}

      In that example there is no confusion. A variable can be set else where and inherited into a grandchild function of execution, which then will trigger a decision contrary to intentions of some localized invocation, which is confusing and produces an unexpected decision.

      Austin
    • Jordan
      Austin, since undefined can be overwritten, your test isn t reliable either. What you SHOULD do in your case is: if (typeof(varName) === undefined ) { }
      Message 2 of 14 , Feb 22 11:15 PM
      • 0 Attachment
        Austin, since "undefined" can be overwritten, your test isn't reliable either.

        What you SHOULD do in your case is:
        if (typeof(varName) === "undefined") { }

        Also see my other comment about having a single exit point from a function.

        --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:
        >
        >
        > > Thanks! I see that that works. I wish there was a middle ground though. Even
        > > though I want to be able to write "if (!someVariable) { return; }", I still
        > > care that my code is indented properly and don't want to disable that
        > > checking.
        >
        > There is no middle ground. Either beautify your code in conformance to a rule set or don't. It may help if you use an automated beautifier that conforms to that rule set.
        >
        > Additionally, I have found that testing for a variable name is less reliable than testing for a variable value unless your objective is to determine if that namespace exists within a given inheritable scope. I would recommend setting all your variable names at the top of your function and then test for undefined.
        >
        > if (varName === undefined) {return;}
        >
        > In that example there is no confusion. A variable can be set else where and inherited into a grandchild function of execution, which then will trigger a decision contrary to intentions of some localized invocation, which is confusing and produces an unexpected decision.
        >
        > Austin
        >
      • Cheney, Edward A SSG RES USAR USARC
        Jordan, ... That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions
        Message 3 of 14 , Feb 23 11:22 AM
        • 0 Attachment
          Jordan,

          > Austin, since "undefined" can be overwritten, your test isn't reliable either.

          That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:

          var parent = function () {
          var test = "stringValue",
          child = function () {
          var test;
          if (test === undefined) {
          alert("true");
          }
          };
          };

          That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.

          Austin
        • Jordan
          You misundersand. undefined itself can be redefined. Simply running var undefined = 1; will break your entire code. NEVER rely on the variable undefined .
          Message 4 of 14 , Feb 23 10:20 PM
          • 0 Attachment
            You misundersand.

            "undefined" itself can be redefined.
            Simply running "var undefined = 1;" will break your entire code. NEVER rely on the variable "undefined".

            --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:
            >
            > Jordan,
            >
            > > Austin, since "undefined" can be overwritten, your test isn't reliable either.
            >
            > That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:
            >
            > var parent = function () {
            > var test = "stringValue",
            > child = function () {
            > var test;
            > if (test === undefined) {
            > alert("true");
            > }
            > };
            > };
            >
            > That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.
            >
            > Austin
            >
          • Michael Lorton
            Are you kidding? It s lamentable that undefined and null aren t constants but there s no sense programming around it. Never type in var undefined = 1;
            Message 5 of 14 , Feb 23 10:27 PM
            • 0 Attachment
              Are you kidding? It's lamentable that "undefined" and "null" aren't constants but there's no sense programming around it. Never type in "var undefined = 1;" and never use any library that does.

              M.



              Sent from my iPhone

              On Feb 23, 2010, at 10:20 PM, "Jordan" <ljharb@...> wrote:

              You misundersand.

              "undefined" itself can be redefined.
              Simply running "var undefined = 1;" will break your entire code. NEVER rely on the variable "undefined".

              --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@...> wrote:

              Jordan,

              Austin, since "undefined" can be overwritten, your test isn't reliable either.

              That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:

              var parent = function () {
              var test = "stringValue",
              child = function () {
              var test;
              if (test === undefined) {
              alert("true");
              }
              };
              };

              That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.

              Austin





              ------------------------------------

              Yahoo! Groups Links
            • Jordan
              There s no sense programming around flaws in the language? It is incredibly irresponsible to just decide I don t like that, so I m going to act like it s not
              Message 6 of 14 , Feb 23 10:39 PM
              • 0 Attachment
                There's no sense programming around flaws in the language?

                It is incredibly irresponsible to just decide "I don't like that, so I'm going to act like it's not a problem."

                Obviously you wouldn't type that into your code. Obviously you wouldn't intentionally divide by zero either, but you would still program defensively to prevent DivideByZero errors.

                JSLint is about programming defensively and properly, not about "doing what works".

                --- In jslint_com@yahoogroups.com, Michael Lorton <mlorton@...> wrote:
                >
                > Are you kidding? It's lamentable that "undefined" and "null" aren't constants but there's no sense programming around it. Never type in "var undefined = 1;" and never use any library that does.
                >
                > M.
                >
                >
                >
                > Sent from my iPhone
                >
                > On Feb 23, 2010, at 10:20 PM, "Jordan" <ljharb@...> wrote:
                >
                > You misundersand.
                >
                > "undefined" itself can be redefined.
                > Simply running "var undefined = 1;" will break your entire code. NEVER rely on the variable "undefined".
                >
                > --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@> wrote:
                >
                > Jordan,
                >
                > Austin, since "undefined" can be overwritten, your test isn't reliable either.
                >
                > That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:
                >
                > var parent = function () {
                > var test = "stringValue",
                > child = function () {
                > var test;
                > if (test === undefined) {
                > alert("true");
                > }
                > };
                > };
                >
                > That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.
                >
                > Austin
                >
                >
                >
                >
                >
                > ------------------------------------
                >
                > Yahoo! Groups Links
                >
              • Cheney, Edward A SSG RES USAR USARC
                If you have a variable named undefined your code deserves to break. Austin
                Message 7 of 14 , Feb 23 11:02 PM
                • 0 Attachment
                  If you have a variable named "undefined" your code deserves to break.

                  Austin
                • Michael Lorton
                  That well is bottomless. If a library is deliberately sabotaging your code -- and sabotage is the kindest interpretation of var undefined = 1; -- no amount
                  Message 8 of 14 , Feb 24 9:52 AM
                  • 0 Attachment
                    That well is bottomless.

                    If a library is deliberately sabotaging your code -- and sabotage is the kindest interpretation of "var undefined = 1;" -- no amount of defensive programming is going to save you.

                    And no, I wouldn't program defensively to prevent DivideByZero errors necessarily. Say for example I was writing a program to compute the average of the list passed in as an argument. A naive implementation would throw DivideByZero if given an empty list. What would a "defensive" implementation do?

                    Well, nothing. There is nothing to be done. You can't return 0, that's the wrong answer. You could return NaN, but that won't really help anything: either the calling program will test for it (couldn't it just have tested for a zero-length list?) or some other error will be thrown at some point in the future.

                    My point, and I do have one, is that it only makes sense to program defensively against problems you can actually solve. There's no way to calculate the average of a zero-length list, and there's no way for a Javascript program to survive in the face of a suicidal library.

                    M.





                    ________________________________
                    From: Jordan <ljharb@...>
                    To: jslint_com@yahoogroups.com
                    Sent: Tue, February 23, 2010 10:39:44 PM
                    Subject: [jslint] Re: option wish for single line if

                    There's no sense programming around flaws in the language?

                    It is incredibly irresponsible to just decide "I don't like that, so I'm going to act like it's not a problem."

                    Obviously you wouldn't type that into your code. Obviously you wouldn't intentionally divide by zero either, but you would still program defensively to prevent DivideByZero errors.

                    JSLint is about programming defensively and properly, not about "doing what works".

                    --- In jslint_com@yahoogroups.com, Michael Lorton <mlorton@...> wrote:
                    >
                    > Are you kidding? It's lamentable that "undefined" and "null" aren't constants but there's no sense programming around it. Never type in "var undefined = 1;" and never use any library that does.
                    >
                    > M.
                    >
                    >
                    >
                    > Sent from my iPhone
                    >
                    > On Feb 23, 2010, at 10:20 PM, "Jordan" <ljharb@...> wrote:
                    >
                    > You misundersand.
                    >
                    > "undefined" itself can be redefined.
                    > Simply running "var undefined = 1;" will break your entire code. NEVER rely on the variable "undefined".
                    >
                    > --- In jslint_com@yahoogroups.com, "Cheney, Edward A SSG RES USAR USARC" <austin.cheney@> wrote:
                    >
                    > Jordan,
                    >
                    > Austin, since "undefined" can be overwritten, your test isn't reliable either.
                    >
                    > That is true, but it is much easier to set a variable to undefined than it is to delete it from the scope. This is especially true with functions that are executed more than once. Consider the following test:
                    >
                    > var parent = function () {
                    > var test = "stringValue",
                    > child = function () {
                    > var test;
                    > if (test === undefined) {
                    > alert("true");
                    > }
                    > };
                    > };
                    >
                    > That is an example of reassigning a variable from a value to undefined. I still believe that is a far more accurate test than testing to see if the variable name exists in the accessible scope.
                    >
                    > Austin
                    >
                    >
                    >
                    >
                    >
                    > ------------------------------------
                    >
                    > Yahoo! Groups Links
                    >




                    ------------------------------------

                    Yahoo! Groups Links



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