Are increment / decrement operators considered bad in Javascript?

Possible duplicate:
Why avoid increments ("++") and shorten ("-") operators in JavaScript?

I recently ran all of my javascript through JSLint, and it always gives me Unexpected '++' for my increment operators. I also noticed that there is a JSLint option for ++ / - portability.

Is bad form using i++/i-- ?

+4
source share
4 answers

If I understand correctly, the reason in JSLint is that there is a significant difference between ++i and i++ , and many developers do not necessarily evaluate the consequences of when they are performed with respect to the addition, with respect to other operations around it.

eg:

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

That's why Crockford considers this a bad practice and prefers a more explicit +=1 .

However, in general, I have no such problem with ++ ; I would love to use it, and I would ignore JSLint telling me not to (most of the time!).

This is an important thing in JSLint - it does not mean real errors; all he tells you is an offer. Some of them are great suggestions (for example, you should never use with ); some of them are good suggestions that indicate bad code; and some of them are only a problem at some points in time and should be considered individually. As a developer, it’s more important that you know why he makes an offer, and not actually fix them. Once you understand the reasons why they are indicated, you can decide for yourself whether this instance of it in your code is a problem or not and how to fix it.

Hope this helps.

+5
source

No, I suppose this could be reasoned, but seriously everyone will learn about this operator very soon in their education or books or something else. This sentence annoys me.

+2
source

I do not think that in Javascript it was definitely considered a bad form, for most programmers it is simply difficult to use these operators in general.

+1
source

Instead of using ++/-- he looked at best practices for using +=/-= .

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

It is intended to increase readability and avoid printing errors due to the excessive use of "code tricks."

Personally, I find it more useful to use this method instead of the standard ++ / - operators, since the number will be colored by the IDE, and this is easier to determine.

+1
source

All Articles