Well, both the post- and pre increment/decrement are useful in making the code a few lines shorter, and sometimes more readable.
Without it,
int i = len;
while (--i >= start)
{
/* do something */
}
// use the value of i for something.
Would become
int i = len - 1;
while (i >= start)
{
/* do something */
i = i - 1;
}
// use the value of i for something.
Assuming the while loop could exit some other way than i becoming exactly equal to start - 1. Though again, it isn't really that much harder to read at all, and you could change the while to a for, to keep all the loop specific code in one place.
If you are reading code, and you encounter either a ++ or a -- that is not:
A) The loop increment at the end of a for statement, or
B) A stand-alone statement incrementing a variable,
Then you need to stop and mentally breakdown the single statement into two statements in order to continue reasoning about the code.
In fairness, I'm talking about readability in terms of being able to scan a piece of code and quickly ascertain what it is doing, and I will concede that pre and post increment might communicate intent, which could also be considered part of readability.
For myself, one of the first things I do when I take over code is to remove all the ++ and -- that are not in loop counters. They all get replaced by += 1. The only reason I leave them in the loop counters is a nod to tradition.
Of course you don't need it. There is basically nothing you need. A language that only supports:
Load from memory
Store to memory
Add
Jump
Checking for 0
Checking for negative
is most certainly Turing complete, and could probably actually be used.
Everything else added on top just makes certain things easier to do, and reduces how much code you have to write and how many clock cycles the CPU needs to execute a certain instruction.
Yes, of course you don't need all the luxuries to just have a Turing complete machine. You could get by on a binary machine with just nor and branch if 0.
But my point was not to make it minumal without going esoteric. Hence my "could probably actually be used"
Like a lot of people have said, you wouldn't do that in Python, it's much easier in my opinion:
int i = len;
while (--i >= start)
{
/* do something */
}
// use the value of i for something.
Would be actually
for i in range(len, start ,-1):
# Do something
# Use the value of i for something
or, if you're using i as an index in a list:
for value in list:
pass
for value in reversed(list):
pass
orrrrr, if you want the index and value:
for i, item in enumerate(list):
pass
None of these can be improved with ++ or -- in my opinion, and is completely unnecessary to add new syntax which holds very little purpose, furthermore you can do this kind of thing:
while (i := i - 1) >= start:
# Do stuff
# Use I for something
And it also has a similar effect if you want to do that.
8
u/Torebbjorn May 27 '22
Well, both the post- and pre increment/decrement are useful in making the code a few lines shorter, and sometimes more readable.
Without it,
int i = len; while (--i >= start) { /* do something */ } // use the value of i for something.Would become
int i = len - 1; while (i >= start) { /* do something */ i = i - 1; } // use the value of i for something.Assuming the while loop could exit some other way than
ibecoming exactly equal tostart - 1. Though again, it isn't really that much harder to read at all, and you could change the while to a for, to keep all the loop specific code in one place.for (; i >= start; i = i - 1;)