4 ms·
FYI These operators are being removed from modern languages now, via style guides, linters, or the language itself (eg: Swift). Personally I actually like the i
by exac 4y ago
FYI These operators are being removed from modern languages now, via style guides, linters, or the language itself (eg: Swift). Personally I actually like the increment and decrement operators, but I understand how they can be confusing (especially ++x vs x++).
- bawolff 4y agoi like x++, but relying on the return value usually tends to be evil.
- Alupis 4y agoThis trope needs to die. If you cannot understand ++x vs x++ then you fundamentally do not understand the language - or programming in the more general sense. Let's stop stupifying our languages for beginners. Beginners are only beginners for a short while, and increasing verbosity with zero gain is annoying at best for everyone else.
- mynameisash 4y ago> If you cannot understand ++x vs x++ then you fundamentally do not understand the language I hardly think this is the issue. The problem is this kind of thing becomes so easy to write but not consciously notice that it causes disproportionately costly mistakes, and I don't think the benefit is worth it. Python and other languages don't have the increment operator (pre or postfix), but never has this been a problem for productivity in any of my teams. On the contrary, I've encountered several instances where a senior engineer with 20+ years in the industry can't for the life of them figure out why some particular value is off-by-one. It wastes hours of time and often takes a fresh set of eyes to figure it out. I know a senior principal engineer who wasted an entire day of his time because a comma at the end of the line turned his Python scalar into a unary tuple and was incredibly difficult to notice. "5," is semantically equivalent to "(5,)" but Python isn't strict enough in this instance. It should be strict, though, not for stupifying but for protecting against errors that we all make.
- Alupis 4y agoPerhaps the problem isn't really the increment operator, but rather folks writing too fancy of code while using it. One should probably not deeply nest things like increments... because they do get difficult to read. That's not an increment operator problem though, that's an engineer problem. The example with Python doesn't apply here. We cannot just go around removing language features because sometimes people write bad code. Further, if you read the code and walk through it in your head, finding a mistaken tuple someplace unexpected should have been easy to find... which means your senior principal engineer isn't very good at reading code, even if they are very good at writing code. Reading code is it's own skillset I've found... that and apparently the debug steppers for python aren't very good either...
- Sohcahtoa82 4y ago> Perhaps the problem isn't really the increment operator, but rather folks writing too fancy of code while using it I find this is a common theme of many complaints of C++ and OOP. For example, C++ lets you do some incredibly ugly things using operator overloading. This can be useful so you could, say, make a matrix class and allow matrix operations to just using + or * rather than .add and .multiply functions. The fact that it can be abused to do very non-sensical, unintuitive things should be a criticism of the programmer who does it, not the feature.
- 5d8767c68926 4y ago>... a comma at the end of the line turned his Python scalar into a unary tuple I have been burned by this mistake a handful of times. Each instance managed to eat up a shocking amount of time. I definitely consider it a misfeature.
- Sohcahtoa82 4y agoI'm trying to figure out how you ended up with a stray comma at the end of a line. A simple typo, bumping the key without noticing?
- 4y ago
- skitter 4y ago++x and x++ just seem so unnecessary. Two extra operators for a very specific case, in which they either safe one character verses x+=1 or mix different mutations into the same line. Iterating a list by incrementing an index doesn't even come up that often if your language supports iterators well. Edit: Just checked, the project I just had open has 7 instances of +=1 in 5k loc and none of them would have been improved by ++
- Alupis 4y agoWell, Lua also does not support += or any of it's counterparts. You have to be extremely literal with x = x + 1, which is just a huge waste for very little gain. I'm all for beginners learning easier constructs... teach them x = x + 1 if you want, just don't remove the shorter constructs for the rest of us. Iterating a list/array is just one of many use cases for the increment/decrement operators anyway...