13

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 or -= 1 instead. Can anyone clarify why for me?

Community
  • 1
  • 1
Bala Clark
  • 1,524
  • 12
  • 19

8 Answers8

36

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.

tbranyen
  • 8,507
  • 1
  • 21
  • 18
  • OK, that makes sense, and it seems to be the consensus here too so I will continue to use ++. Javascript does have a few weird ways of doing things every now and again and it is hard to tell when a recommendation is a personal thing or something performance based. – Bala Clark Jul 13 '10 at 15:22
  • 1
    I liked The Good Parts quite a lot, but I was very disappointed in his rationale for declaring unary increment and decrement as being a bad part. Sure, they can be used in ways -- like parts of sadistic/masochistic for loops -- but that applies to many language features. It might apply to all of them if you spend enough time creating crazy examples. – Adam Crossland Jul 13 '10 at 15:26
  • 1
    Sigh. Crockford has a *lot* to answer for, I wish people wouldn't take his opinion as gospel so much... – annakata Jul 13 '10 at 15:47
  • Although I disagree with a fair few of Crockford's recommendations (this one and his rejection of `new` spring to mind), as the opinions of well-know JavaScripters go, I'd say his are pretty good. He's done a lot to popularize some powerful techniques. Five years ago his ideas were ahead of pretty much everyone I'd come across. – Tim Down Jul 14 '10 at 19:38
16

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.)

froadie
  • 79,995
  • 75
  • 166
  • 235
  • 1
    Agreed. Why *shouldn't* we use some of the more convenient language elements that make C syntax so cool in the first place? – Robusto Jul 13 '10 at 15:20
  • 1
    not that i mind `++`/`--`, i use them all the time. however. i'm not sure i can *really* call them much more straightforward. consider `var c = a++ + b` and `var c = a + ++b`. *all three* variables will have different values depending on where you place the spaces, here, which opens for the possibility to mis-interpret `var c = a+++b` – David Hedlund Jul 13 '10 at 15:23
  • 2
    @David Hedlund - true. There is potential confusion. However, the increment operator is MEANT to increment. Its purpose is about as straightforward as it gets. Using `+= 1` or `= var + 1` seems to me like doing x + x when you want to double something instead of x * 2. Code what you mean. – froadie Jul 13 '10 at 15:27
  • What's more straightforward about `x++`/`++x` than `x += 1`. The latter looks as straightforward to me as the former. Why add special syntax for incrementing (and decrementing) only by 1 if we already have readable and more flexible syntax to increment by any value? – Robert Kusznier Apr 23 '19 at 16:14
9

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.

Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
  • 1
    I totally agree. Also, I think we should keep things, including the language syntax, as simple as possible. Why add special syntax to increment variables only by 1 (`x++`/`++x`), if we already have syntax to increment variables by any value? For me it doesn't make any sense. – Robert Kusznier Apr 23 '19 at 16:21
7

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.

Community
  • 1
  • 1
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • 6
    Well said. It seems to me that crockford is blaming the gun for the hole in his foot. Or maybe it was that straw man who did it... – annakata Jul 13 '10 at 15:48
  • Most of the buffer overrun bugs that created terrible security vulnerabilities were due to the fact that C strings are null-terminated. – Florian F Jul 19 '16 at 14:39
  • You say "we can still use the ++ and -- operators without writing cryptic code". True. But why keep using tools, which can be overused by others (and by us too, if we are not disciplined enough), if we can stick to tools that are never having these dangers and are as expressive as the former ones? We can increment variables by `+= 1` and not run into dangers of easy to miss errors or confusing code, like when we use `++`. – Robert Kusznier Apr 23 '19 at 16:25
5

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.

Chris Cudmore
  • 29,793
  • 12
  • 57
  • 94
3

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.

Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
2

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++;
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 6
    Never use things you do not understand. Please use things that you *do* understand! (Same thing applies to `break`, `continue`, Delphi's `with` and all other things we have argued about at SO the last few weeks.) – Andreas Rejbrand Jul 13 '10 at 15:19
  • @Andreas, things like that _sound_ good, but in reality, how many professionals can afford to learn to see through every abstraction layer and understand a system from quantum physics to GUI? I do believe that everyone should strive to learn the details of the operation model of the layer of abstraction at which they work. I say "strive" because in general there's no way to know when you truly understand every detail of the operation model of an abstraction layer. – David Gladfelter Jul 13 '10 at 15:31
  • @Andreas there is a stronger principle - never use things that are liable to be misunderstood, by yourself or others. The difference between `++i` and `i++` is a prime example. – Gabe Moothart Jul 13 '10 at 15:42
0

use the ++, is standard and have a good situation of being there.

Gustavo V
  • 152
  • 1
  • 1
  • 4