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

Re: [jslint] Increment (++) operator

Expand Messages
  • Michael Lorton
    To the extent that this is a democracy, seconded. Kernighan and Ritchie s decision way back when to have prefix and postfix increment and decrement probably
    Message 1 of 2 , Jun 5, 2009
    View Source
    • 0 Attachment
      To the extent that this is a democracy, seconded. Kernighan and Ritchie's decision way back when to have prefix and postfix increment and decrement probably seemed like a good idea at the time, but as a practical matter, it only makes real sense in the loop case (whiles and expecially fors). Stand-alone statements are probably OK too.

      M.





      ________________________________
      From: Martin Barrett <mbrrtt@...>
      To: jslint_com@yahoogroups.com
      Sent: Friday, June 5, 2009 7:24:48 AM
      Subject: [jslint] Increment (++) operator


      I've been using JSLint for several years. As a result, I've developed an habitual JavaScript coding style which conforms to its requirements, and my code generally passes JSLint with minimal modifications and does seem to have fewer bugs as a result.

      There is one minor feature which rubs me the wrong way, however, and repetition does not seem to allay the irritation. It concerns the increment and decrement operators ++ and --. On the whole, their use is frequently pernicious-- some of the most unreadable code I've seen features tricky side effects resulting from embedding "++" in "if" statements or assignment statements. (And then you have to keep straight the difference between i++ and ++i.)

      However, there is one case in which ++ (or --) promotes readability: when used by itself as the increment or third part of a "for" statement. E.g., "for (i = 0; i < myArray.length; i++) { ...."
      This is by far the most common for-loop form, the use of "++" in this context is traditional, and I've found that writing " i += 1" or "i = i + 1" instead seems only fussy and more difficult to scan visually. (Indeed, one virtue of allowing the ++ form here would be that, e.g., "i += 2" could be reserved for cases in which the incrementing operation is *different* from the most common case. This surely would promote readability.)

      Of course, JSLint already has an option to "disallow ++ and -- operators". But when that option is unchecked, all uses of these operators are allowed, including the really bad ones. What I request instead is that JSLint pass the usage of "++" and "--" when affixed to a variable as a lone STATEMENT, not in conditional expressions or assignment statements, and that it do this regardless of the setting of the option. This sole usage seems safe and readable and perhaps even efficient. And of course, In this usage, of course, you don't even have to remember whether to put the operator on the left or the right. The vast majority of uses thus permitted would be in "for" loops; this change would be most useful for programming groups which wish to permit the usage of "++" as the increment in for-loops while prohibiting its use elsewhere.

      --Martin Barrett

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