0

Why is jslint complainig about this syntax:

var myint;
myint = 0;

myint++;

www.jslint.com response:

Unexpected '++'. myint++;

kstubs
  • 808
  • 4
  • 18
  • 4
    If you look at the options, there is one for "tolerate ++ and --". You have it set not to tolerate it. It's one of the more nitpicky options that Crockford doesn't like. – Matt Burland Oct 07 '13 at 18:20
  • Yes thanks, I just found this option. – kstubs Oct 07 '13 at 19:00

3 Answers3

3

JSLint doesn't like ++, it wants you to use myint += 1. This is highly debatable, see Why avoid increment ("++") and decrement ("--") operators in JavaScript?

There is an option to let it pass, though (on the web interface, it's under "tolerate...").

From JSLint help:

The ++ (increment) and -- (decrement) operators have been known to contribute to bad code by encouraging excessive trickiness. They are second only to faulty architecture in enabling to viruses and other security menaces. Also, preincrement/postincrement confusion can produce off-by-one errors that are extremely difficult to diagnose. There is a plusplus option that allows the use of these operators.

Community
  • 1
  • 1
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
2

Crockford's argument against ++ and -- is that they are "too tight, too tricky, too cryptic" (page 112 of Javascript: the good parts). Your mileage may vary. Use them if you want, or if you think they are too confusing and hamper readability, don't.

You can switch that rule off in JSLint if you don't buy Crockford's argument.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • I guess so. And I never even said I agreed or disagreed with Crockford. – Matt Burland Oct 07 '13 at 18:26
  • I fail to see why it's cryptic at all? it's a common operator with identical behaviour across a vast number of languages... – Emissary Oct 07 '13 at 18:28
  • @Emissary: Not my words, Crockford's. As I said, YMMV. – Matt Burland Oct 07 '13 at 18:30
  • ye I got that - just my two cents ;) – Emissary Oct 07 '13 at 18:31
  • I'd say there are certainly cases where it could be confusing, for example `myarray[++i]` vs `myarray[i++]` could certainly catch somebody out, but I think it's a little silly to replace `for (var i=0; i < j; i++)` with `for (var i=0; i < j; i+=1)` just for the sake of avoiding `++`. – Matt Burland Oct 07 '13 at 18:34
  • Again I think my point still stands, the difference between `++i` and `i++` are Computer Science 101. It's just part of the language, by the same logic he might as well argue that the `+=` operator is confusing too and should be replaced with the fully formed `i=i+1`. Nonsense :) – Emissary Oct 07 '13 at 18:41
  • 1
    @Emissary Did you read Crockford's book and watched his video lectures? The guy is very smart and knows js in depth, but some of his ideas are quite radical. – bfavaretto Oct 07 '13 at 18:44
  • @bfavaretto I'm not saying he isn't - it's just not *"scripture"*. – Emissary Oct 07 '13 at 18:49
  • @Emissary: I don't think anybody is arguing that it is! Just take it or leave it. Some points Crockford makes are pretty good (eval is evil), some are debatable (++/--). – Matt Burland Oct 07 '13 at 19:15
  • 1
    Well, I'm a fan of ++, as in i++. I'm not a fan of ++i, and for no reason other than that that is what I've grown comfortable with. I'm not a fan of i+=1 unless its something like i+=some_var. But these are just things I am comfortable with. I am finding myself wrapping single if then blocks now with proper { } but have always been comfortable with the one liner if(..) return -or same 2 liner- – kstubs Oct 07 '13 at 19:19
1

@bfavaretto's answer is correct. If you want to allow this (assuming you're using a non-web version), set plusplus to true with this directive:

/*jslint plusplus: true */

Reference

Bucket
  • 7,415
  • 9
  • 35
  • 45