Javascript: Disallowing Unary Increment and Decrement (++ and --)

Created on 7 Oct 2015  路  12Comments  路  Source: airbnb/javascript

In Douglas Crockford's Javascript: The Good Parts, he writes:

++ and --
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 [in JSLint] that prohibits the use of these operators.

In addition, his replacement for ++ and -- is += 1 and -= 1, respectively.

Apparently Crockford's book is pretty popular, but I don't see anyone really talking about this part.

What does everyone think?

EDIT: This is a little off-topic, but Ruby does not support ++ and --.

pull request wanted

All 12 comments

I think i++; and i += 1; are mostly equivalent, but since our style guide also recommends avoiding loops, this doesn't come up often in practice. Where do you find yourself needing increment and decrement operators besides loops?

I'm in. I don't like assignments like foo = ++this.id

I also don't like those, but because it's conflating assignment with expressions, not because of the ++

Similarly to loops, should we, instead of banning them outright, recommend not to use ++ and --?

Python also doesn't allow this.

Also worth noting that i++ is not the same as ++i.

Throughout our style guide we mention that mutation is bad, maybe this should be explicitly mentioned. i += 1; i++; or friends are all mutating.

It has been a while since someone has commented on this. Has a PR already been made or would the community still want one. I would be happy to make one for this.

Thanks

None has been made; I'd be happy to review one!

Thanks for the prompt response @ljharb! Ill have that up as soon as I can, within the next 48 hours Im guessing.

This is my first contribution to the code base so I have a few questions.

  1. I will only need to update eslint-config-airbnb-base correct? I was going to apply the no-plusplus rule from eslint http://eslint.org/docs/rules/no-plusplus.
  2. Where would be a good place to update the guides with this fix/rule? There are a few areas that use the ++ -- in the guides(README) currently that im guessing would need to be updated. But I am not sure where we could declare this rule on its own? I can personally see this going into either, Variables or TypeCasting to name a few.

Thank you and let me know if anything needs to be clarified.

Yes, updating the base config is correct. Please prefix your commit with [eslint config] [base] [breaking] if you can :-)

I'm not sure which section would be more appropriate, but we can bikeshed that on the PR :-)

Perfect, will get on it!

Thanks

Closed with #1012

Was this page helpful?
0 / 5 - 0 ratings

Related issues

progre picture progre  路  3Comments

felixsanz picture felixsanz  路  3Comments

kozhevnikov picture kozhevnikov  路  3Comments

olalonde picture olalonde  路  3Comments

weihongyu12 picture weihongyu12  路  3Comments