Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
JS96185344yThe most funny part is the explanation “the probability of making mistakes is lower”. 😂
-
Two possible bug classes come to mind:
1) mixing up *ptr++ and (*ptr)++
2) mistaking var++ as atomic operation. -
@JS96 Post-increment is a very weird operator and I still fail to see why it was a good idea, but it's one of those things you learn to watch out for and it ceases to be a problem after a while.
-
@homo-lorens It's useful for pointer load/store operations with post increment, which is often a CPU instruction, and some CPUs had inc/dec as opcode.
Early C compilers were not able to see that optimisation with the increment moved out to a separate statement. -
There's actually a good reason Rust doesn't have an increment operator
They didn't want *both* post-increment and pre-increment because they easily lead to errors and Rust is all about correctness. So if they only implemented one, pre-increment would have been the obvious choice, but then you run into the problem of choosing between `++x`, which is quite unintuitive when you don't also have `x++`,
or making `x++` be pre-increment but then that's the opposite of what you would expect, again leading to bugs -
Not really. It's a bad operator, for reasons outlined above.
I strongly dislike rust but they got this decision right, and for the right reasons. -
Oh, and there's another one: side effects as arguments in macros. That's a common pitfall in C/C++, in particular because you sometimes don't know whether something is implemented as function or macro, or that might change due to code changes elsewhere.
The solution is "don't use side effects in arguments", but that relies on the dev's discipline or experience. -
@Fast-Nop assert() is a great example of this being an issue. Absolutely good point.
-
@junon Or stuff like isalpha(), tolower() etc. which may be functions, macros, or -hold on- even both (see https://softwareengineering.stackexchange.com/...).
That's fun when you port existing code from a platform where that used to be a function to another target where it happens to evaluate to a macro.
Related Rants
Is anyone else super annoyed that rust doesn’t have an increment operator??? Why did they get rid of it????
rant
rust