why shouldn't we use ++ in javascript? [duplicate]
Possible Duplicate:
Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?
I've heard in a few places that it is not recommended to use ++ & -- in javascript, and we should be using += 1 o开发者_运维知识库r -= 1 instead. Can anyone clarify why for me?
Because Douglas Crockford has his own opinions of how confusing something may or may not be. They are fine to use, JSLint is just projecting his opinions.
Anyone mind if I make the opposite suggestion...?
Please use the increment (++) and decrement (--) operators when you can. Much more straightforward. (And that's what they're there for.)
The reason that Crockford et al. discourages the use of ++
/--
is that they introduce side-effects. There is virtually no reason to use i+=1;
instead of i++;
because both are single statements, but there are lots of reasons to have a statement do only one thing and ++
makes it easy to violate that rule.
Discouraging use of ++
means we don't have to keep the distinction between i++
and ++i
in our heads. It means we're less likely to try to use them in clever but confusing ways. It's good practice not to use them, imo, for those reasons.
The only time I saw these operators discouraged was in Douglas Crockford's JavaScript: The Good Parts book.
Quoting from the "Bad Parts" Appendix:
The increment and decrement operators make it possible to write in an extremely terse style. In languages such as C, they make it possible to write one-liners that could do string copies:
for (p = src, q = dest; !*p; p++, q++) *q = *p;
They also encourage a programming style that, as it turns out, is reckless. Most of the buffer overrun bugs that created terrible security vulnerabilities were due to code like this.
In my own practice, I observed that when I used
++
and--,
my code tended to be too tight, too cryptic. So, as a matter of discipline, I don't use them anymore. I think that as a result, my coding style has become clearer.
In addition, quoting from the JSLint documentation (the code quality tool which he has written):
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. There is a plusplus option that prohibits the use of these operators.
He really doesn't fancy these operators... But come on! I think we can still use the ++
and --
operators without writing cryptic code. I use them, and I like them. Everyone uses them.
Note that although JSLint has the option to disallow these operators, this option is not enabled by default.
The only reason I can think of, is that your Javascript is more likely to be edited by a moron than any other code you write (html??).
So to a noob,
x=x+1 ;
is more readable than
x++;
When dealing with experienced programmers, it really doesn't matter.
Using ++
/--
can restrict js minification options, because their meaning is very sensitive to whitespace. Depending on the minifier, it could even introduce errors into working code. Crockford explains (in the Caution section) here.
The only good reason I can think of is clarity. You don't have to worry about the following situation:
var result = ++i;
Or:
var result = i++;
use the ++, is standard and have a good situation of being there.
精彩评论