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

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

Expand Messages
  • 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 1 of 14 , Feb 24, 2010
    • 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.