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

2513Re: [jslint] for comma

Expand Messages
  • Emmett Pickerel
    Jul 15 3:40 PM
      My for loops are fairly consistently of this form:

      for (i=0, l=arr.length; i<l; i = i + 1)

      What are the caveats of using the comma in this particular case, where it's really being used as a separator?

      I certainly can't see a case where I'd want to use more than a single comma, or set another property than the loop length. I'd argue it should only be allowed if the value of the second variable is used in the test.


      From: Douglas Crockford <douglas@...>
      To: jslint_com@yahoogroups.com
      Sent: Friday, 15 July 2011, 15:04
      Subject: Re: [jslint] for comma

      > --- In jslint_com@yahoogroups.com, Erik Eckhardt <erik@...> wrote:
      > > > Is anyone still using comma expressions in the control part of for
      > > statements? I think this is a bad practice, but JSLint currently tolerates
      > > it.

      > > Would you be willing to explain (or point us to something explaining) the
      > > pitfalls of this practice?

      > There are two. First, the confusion between with comma operator and the comma separator. Having both in the language tends to mask syntax errors. Currently, JSLint tolerates the comma operator only in for statements.

      > Second, for statements are intended to provide convenience in declaring, testing, and incrementing the induction variable. The comma introduces confusion here as well, over what matter belongs in the body and what matter belongs in the induction specification. Taken to the extreme, you see loops with no bodies at all. 
    • Show all 33 messages in this topic