I’ve always wondered why a lot of languages don’t have an increment/decrement-by-one operator like there is in C++, like
i++
i--
Why? I like using that operator, it’s very natural.
I’ve always wondered why a lot of languages don’t have an increment/decrement-by-one operator like there is in C++, like
i++
i--
Why? I like using that operator, it’s very natural.
Read this:
I have always wondered, why not make it a compiler error to use increment/decrement operator in an expression, so that it can only be used as a statement. So, basically ++x or x++ is just a syntax sugar for x += 1.
I understand that many of the use cases for increment/decrement operator involves using them as part of an expression to make concise code. But many programmers already only use it as a separate statement or an expression, like in a for loop.
Having a separate operator for incrementing/decrementing by 1 is nice, because it a very basic operation on numbers. Successor operation is the basic operation in Peano axioms.
It would be nice to have increment/decrement operator in many modern programming languages that do no include them.
For example in Swift they got removed about a decade ago, and I don’t miss them. At first it felt a bit weird, because in all C-like languages I have used so far (including Java) you would normally reach for the classic for-loop syntax with i++, and I have written variants of that a thousand times.
But with modern constructs like for-loops over ranges (and more expressive iteration patterns in general), those operators don’t really add much value anymore.
The arguments in their proposal are still valid: