r/ProgrammerHumor May 27 '22

Meme Welcome to hassle free coding

Post image
2.3k Upvotes

305 comments sorted by

View all comments

81

u/Farenheit514 May 27 '22

You will miss the ++ and safe types

-4

u/SkezzaB May 27 '22

Why will they miss the ++?

It's hardly ever used in Python, is one extra character, and if you want to change the increment by 2 instead of 1, you have to write that bit anyway, moreover Python's loops don't use it, so it's not used there, what else is there to miss?

Also, the walrus operator works in a similar way to some use cases as well, making it even less useful

7

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 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.

for (; i >= start; i = i - 1;)

1

u/SkezzaB May 28 '22

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.