> Their expressive advantage is minimal - x++ is not much shorter than x += 1 Personal opinion, but I definitely miss '++' every time I'm doing '+=' in Python.
I've always felt like Python and Ruby chose to drop these to discourage you from writing traditional loops, as they have their own enumerators to accomplish the same thing. And, as the author mentioned, so does Swift. Obviously you still have to do a '+=' occasionally, but I don't find myself doing that often.
Remove the ++ and –- operators
201–210 of 249 posts
Re: Remove the ++ and –- operators
#202Earlier quoted context omitted.
Apple has a long history of readability over terseness. "plus plus" doesn't mean anything. It might even be confusing that it doesn't add 2 .
From Swift documentation: for i in 0.. This ..< operator is so much more logical, of course! Not to mention == and ===. Logically, the first one compares twice and the second compares three times (for luck), just in case first two comparisons didn't go through...
Re: Remove the ++ and –- operators
#203Earlier quoted context omitted.
Apple has a long history of readability over terseness. "plus plus" doesn't mean anything. It might even be confusing that it doesn't add 2 .
From Swift documentation: for i in 0.. This ..< operator is so much more logical, of course! Not to mention == and ===. Logically, the first one compares twice and the second compares three times (for luck), just in case first two comparisons didn't go through...
zip(names, 1...names.count).forEach { name, index in
print("Person \(index) is called \(name)")
}
Or, in an ideal world: zip(names, 1...names.count)
.map({ name, index in "Person \(index) is called \(name)" })
.forEach(print)
Currently, that isn't valid Swift, "print" can't be used like that, though if you define a closure it works: let printIt = { x in print(x) }
zip(names, 1...names.count)
.map({ name, index in "Person \(index) is called \(name)" })
.forEach(printIt)
I think that Swift could use = for equality, since assignments return Void and comparison returns Bool. Maybe it should.But your argument boils down to "if you can't be clear everywhere, don't be clear anywhere". That's not a good goal.
Re: Remove the ++ and –- operators
#204I've never understood the obsession with avoiding ++ and --. Douglas Crockford advocates the same thing. All of the examples of why it's bad are contrived and set up to fail. In the majority of real world instances, it's just us incrementing or decrementing a single variable.
All the examples where they're useful are contrived. And simplicity is an actual goal of computer language design.
array[i++] = this;
array[i++] = that;
array[i++] = the_other;
which also extends nicely when some of those elements are optional.Re: Remove the ++ and –- operators
#205Earlier quoted context omitted.
Yes. Anointing "+ 1" as a thing that needs special syntax is weird .
Not 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.
#include
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.Re: Remove the ++ and –- operators
#206I just taught ++ and -- to 1st semester students last week. There were no problems.
Re: Remove the ++ and –- operators
#207Earlier quoted context omitted.
All the examples where they're useful are contrived. And simplicity is an actual goal of computer language design.
That's quite a claim. I agree that most uses of these operators as an expression are not wise. Some are though. As a counter-claim, I offer: array[i++] = this; array[i++] = that; array[i++] = the_other; which also extends nicely when some of those elements are optional.
array[postincr(&i)] = ...;
if you're doing enough of those that it's worth it.Re: Remove the ++ and –- operators
#208One of the reasons they state is that ++ makes it harder for beginners. I just taught ++ and -- to 1st semester students last week. There were no problems.
Re: Remove the ++ and –- operators
#209Earlier quoted context omitted.
People suppose this, but that's not quite how it happened. C came from B, and B had ++ also. But B came before the PDP-11. The PDP-7 had hardware that supported auto-increment-by-one, and Ritchie suggests that's where ++ came from. The PDP-7 assembler manual is at http://bitsavers.informatik.uni-stuttgart.de/pdf/dec/pdp7/PD... , but I can't see any reference to this auto-increment hardware, or to an assembler directi…
Very interesting. I have supposed this since using PDP-11 assembler and reading Dennis' compiler in the late 70's. There were machines with auto increment modes long before the PDP-11 though, e.g. " http://ed-thelen.org/comp-hist/GE-635.html#Indirect Then Tally (IT) Modification"
Re: Remove the ++ and –- operators
#210I had no idea there was even a debate about this let alone people passionate about each side. This is bike shedding right?