4

Possible Duplicate:
Why avoid increment ("++") and decrement ("--") operators in JavaScript?

I've recently been running all my javascript through JSLint and it always gives me Unexpected '++' on my increment operators. I've also noticed there is a JSLint option to tolerate ++/--.

Is it considered bad form to use i++/i--?

Community
  • 1
  • 1
Brandon Cordell
  • 1,308
  • 1
  • 9
  • 24
  • 2
    This imo is just Crockfords personal preference. I wont stop using ++ or -- anytime soon. – Loktar Aug 30 '11 at 15:29

4 Answers4

5

If I understand correctly, the reason it's in JSLint is because there's a significant difference between ++i and i++, and many developers don't necessarily appreciate the implications of being specific about when the addition is done, in relation to other operations around it.

for example:

var i = 0;
if(i++) {
    //does this get run or not?
}
if(--i) {
    //and what about this one?
}

This is why Crockford considers it bad practice, and prefers the more explicit +=1.

However, in general, I don't have this kind of issue with ++; I'm perfectly happy to use it, and I would ignore JSLint telling me not to (most of the time!).

This is the important thing about JSLint -- it doesn't tell you about actual errors; everything it tells you is a suggestion. Some of them are excellent suggestions (eg you should never ever use with); some are good suggestions that indicate poor code; and some of them are only a problem some of the time and should be considered individually. As a developer it is more important that you need to know why it's making its suggesting than it is to actually fix them. Once you understand the reasons for them being pointed out, you are in a position to decide for yourself whether a given instance of it in your code is a problem or not and how to fix it.

Hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
2

No, I suppose this could get argumentative, but seriously everything learns about this operator very soon in their educations or books or whatever. This suggestion annoys me.

Joe
  • 80,724
  • 18
  • 127
  • 145
  • 1
    I 100% agree. I think its just Crockfords personal taste being added to JSLint. I also disagree with him on having a separate `var` on every line, but thats a different discussion. – Loktar Aug 30 '11 at 15:31
  • I agree. I'm just trying to better myself as a javascript developer and this stuck out to me as odd. – Brandon Cordell Aug 30 '11 at 15:31
1

I don't think it's specifically considered bad form in Javascript, it's just hard for most programmers to use these operators correctly in general.

Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
  • Can you include an example of using it incorrectly? I've always used it without an issue, but now I'm curious as to if I've ever used it incorrectly. – Brandon Cordell Aug 30 '11 at 15:32
  • You have to remember what order the increment happens with respect to when the value becomes available. If you've only ever used it by itself or in an idiomatic expression it's unlikely you've misused it. But doing things like `var x = i++ * 3;` can lead to surprises that aren't possible if you use `i + 1` or `i += 1`. – Daniel Lyons Aug 30 '11 at 15:48
0

Instead of using ++/-- it's considered a better practice to use +=/-=.

x += 1; instead of x++;.

This is supposed to increase readability and avoid typing mistakes due to excessive use of "code tricks".

Personally I find more useful to use this method instead of the default ++/-- operators, as the number will be colored by the IDE and it's easier to spot.

Jose Faeti
  • 12,126
  • 5
  • 38
  • 52