4 ms·
I'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 s
by rocky1138 11y ago
I'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.
- SamReidHughes 11y agoIn Go, where they're a statement, they're fine. But as an expression, but stuff can happen. In non-contrived, real-world, historical examples.
- Gibbon1 11y agoWas just thinking in moldy old c code the two places to use ++ or -- was when either incrementing pointers or indexes. The other is ++ and -- also map directly to old school assembly language instructions. Helpful when writing the back end of old school compilers. Probably doesn't make much sense in a language with foreach() or dictionaries, etc.
- Retra 11y agoAll the examples where they're useful are contrived. And simplicity is an actual goal of computer language design.
- mattst88 11y agoThat'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.
- SamReidHughes 11y agoFor what it's worth, you could always make a function postincr and write array[postincr(&i)] = ...; if you're doing enough of those that it's worth it.
- lomnakkus 11y agoI presume you're using C as the example language? Most modern languages have an append-to-list method/function, so your example isn't really applicable in those langauges. For example, ArrayList#add() for Java or Vec::push[1] for Rust, etc. (Yes, these are technically dynamically sized arrays, but you can always reserve the necessary amount of space up front if you so desire.) [1] https://doc.rust-lang.org/std/vec/struct.Vec.html#method.push https://doc.rust-lang.org/std/vec/struct.Vec.html#method.pus...