After reading Douglas Crockford's "JavaScript: The Good Parts" and Stoyan Stevanov's "JavaScript Patterns," I'm trying to determine exactly what 'excessive trickiness' means. They both say that this occours when using either ++
or --
in your code, but can't find a firm definition for this term, either within SO or via a Google search. Any ideas?
-
6Here is a fairly lengthy discussion with links: http://stackoverflow.com/questions/971312/why-avoid-increment-and-decrement-operators-in-javascript – Jonathan Dec 29 '11 at 11:25
-
There are lot of great answers here and I get it now. Thanks for thge help from everyone! – kaidez Jan 02 '12 at 10:47
4 Answers
You can hear it from Crockford himself here: http://www.youtube.com/watch?feature=player_detailpage&v=47Ceot8yqeI#t=4140s
The simple answer is that ++
and --
act on a variable, and usually do it at the same time as you're doing something else to that variable. For example, can you quickly decide what this does?
y += x++ + ++y; // what is y now?
More StackOverflow discussion: Why avoid increment ("++") and decrement ("--") operators in JavaScript?

- 1
- 1

- 16,984
- 3
- 55
- 63
-
1I don't know about you, but I can tell you instantly that this saves to code at index 1. And I use Python 80% of the time, that language doesn't even *have* post-/pre-increment/-decrement. Are JS developers unable to memorize the perfectly simple meaning of these operators? Not that side effects in *complex expressions* aren't confusing. But your example seems quite contrieved to me. And many developers in C and C++ seem to get along just fine with these operators, despite using them properly being even harder in C and C++ due to potential of undefined behaviour. – Dec 29 '11 at 11:35
-
-
That is indeed tricky, I suggest you use that instead. Note that the aforementioned C and C++ developers would agree it's feindish (only partly because it's undefined behaviour in their languages). – Dec 29 '11 at 11:42
It's about the little tricks that are built-in. For example, avar++
is an alias for avar = avar + 1
.
JavaScript is filled with operators and other things that make programming easier, but they are just tricks. That's probably what he means.
Also, as Jonathan pointed out: Why avoid increment ("++") and decrement ("--") operators in JavaScript?

- 1
- 1

- 29,532
- 7
- 72
- 105
-
-
2`avar++` is **not** an alias for `avar = avar = avar + 1`, in expression contexts. `++avar` is. Preincrement changes the variable's value but evaluates to the previous value. Maybe Crockford *is* right... – Dec 29 '11 at 11:44
Excessive trickiness is subjective. Because of that, there's no firm definition for it.
Some would say the C famous string copy code (while (*p++ = *q++);
) is excessively tricky, some would say it's not.

- 76,472
- 17
- 159
- 346
Here is a fairly lengthy discussion with links:
Why avoid increment ("++") and decrement ("--") operators in JavaScript?
I think part of the idea is that when using those operators anywhere but with a single variable on a single line, they become confusing to anyone not in the developer's head.