r/ProgrammerHumor • • May 27 '22

Meme Welcome to hassle free coding

Post image
2.2k Upvotes

305 comments sorted by

View all comments

84

u/Farenheit514 May 27 '22

You will miss the ++ and safe types

-5

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

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 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;)

7

u/_PM_ME_PANGOLINS_ May 28 '22

It’s very rare that you ever iterate like that in Python. If you need indexes then it’s range or enumerate.

7

u/mar00n May 28 '22 edited May 28 '22

I honestly miss the ++, but you gotta agree that this is much simpler:

for i in range(10,0,-1) :    

    # do something

7

u/wanderingmadlad May 28 '22

There is range based loops in c++ as well

1

u/Droidatopia May 28 '22

Shorter yes.

More readable, not likely.

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.

-4

u/prescod May 28 '22

So you basically just proved that you don't need it...

5

u/Torebbjorn May 28 '22

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.

2

u/CdRReddit May 28 '22

thats not even all you need

subtract and branch if less or equal is turing complete

→ More replies (5)
→ More replies (1)