4 ms·
Yes. Anointing "+ 1" as a thing that needs special syntax is weird.
by cballard 11y ago
Yes. Anointing "+ 1" as a thing that needs special syntax is weird.
- dragonwriter 11y ago> Yes. Anointing "+ 1" as a thing that needs special syntax is weird. Its not a special syntax for "+1", its a special syntax for the combination of assignment and increment/decrement. The postfix versions (increment/decrement variable and then return the value that was present before the increment/decrement), particularly, are equivalent to: _x = x; x += 1; _x In an environment where that pattern of operations is common, I can see the value of having a concise mechanism for expressing it. OTOH, there's a cost to it, too, and I'm mostly happier without them around.
- cballard 11y agoSorry, you are correct - although I would restate my point with "+= 1" isn't special. 1 is just another number, the fact that it's useful for enumerating arrays in C isn't relevant as Swift provides more native solutions for that, the use of which should be encouraged.
- ars 11y agoHave you never heard the rule that your code should have no numbers in it except 0 and 1? All other numbers should be written as constants instead, and then use the name of the constant instead of the number directly. > Anointing "+ 1" as a thing that needs special syntax is weird. 1 is special.
- cballard 11y agoYes, of course, I use constants in all cases. Why is adding 1 to something special, compared to adding 2 to something? The #1 use case, by far, is C-style for loops, which aren't used in idiomatic Swift code.
- ars 11y agoI said why: The 2 should be a constant as a name, but the 1 can be a bare 1.
- spc476 11y agoNot necessarily so if you come from a strong Assembly language background. Most processors have a special instruction for incrementing a register that's shorter (and faster) than the more generic ADD instruction. For instance, on a 64-bit x86 machine, ADD RAX,1 is nine bytes long, while INC RAX is one byte. Maybe not as much a win these days, but back when memory was smaller and more expensive, it was worth it.
- cballard 11y agoIn a high-level language, this is the job of the compiler, though. If the compiler isn't generating those instructions for cases where the increment can be proven to be 1, I would consider it a bug.
- DiThi 11y agoNowadays, a compiler that doesn't optimize a "+1" is a bad compiler.
- spc476 11y agoThese days yes. But when C was first being developed, when you might have had 64K of memory for everything? Back then it might have been a sane choice.
- JoeAltmaier 11y agoDoesn't x86 machine code have short-immediate forms? So ADD RAX,1 could be 4 or 5 bytes. Still a win to use INC of course.
- throwaway999888 11y agoAny optimising compiler for a mainstream language that can't do this optimisation reliably in this day and age is crap.
- mattst88 11y agoCompiling #include <stdint.h> uint64_t inc(uint64_t x) { return x + 1; } With -O0 gives me: 48 83 c0 01 add $0x1,%rax and with -O2: 48 8d 47 01 lea 0x1(%rdi),%rax That is, both are four bytes.