3

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?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
kaidez
  • 679
  • 8
  • 27
  • 6
    Here 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 Answers4

5

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?

Community
  • 1
  • 1
Interrobang
  • 16,984
  • 3
  • 55
  • 63
  • 1
    I 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
  • Sorry, it's a contrived example. How about `y += x++ + ++y`? – Interrobang Dec 29 '11 at 11:39
  • 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
1

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?

Community
  • 1
  • 1
Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • ...except that `var` is a reserved word. ;-) – Peter-Paul van Gemerden Dec 29 '11 at 11:29
  • 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
0

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.

GSerg
  • 76,472
  • 17
  • 159
  • 346
0

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.

Community
  • 1
  • 1
Jonathan
  • 5,495
  • 4
  • 38
  • 53