4 ms·
push_back always sounds clunky and dissonant to me. Gotta wonder what was wrong with ".append" -- guess it wasn't applicable to stack-based containers or somet
by CamperBob2 2y ago
push_back always sounds clunky and dissonant to me. Gotta wonder what was wrong with ".append" -- guess it wasn't applicable to stack-based containers or something.
- wrsh07 2y agoI wonder if they designed the APIs for vector, list, deque, etc together On deque it all feels fairly natural. Ruby's shift and unshift are cool, but I always struggle to remember the words
- kragen 2y agoyes, they did ruby and js got shift and unshift from perl, which got shift from sh, where it's the only way to iterate over an array or other list. (also, sh only has one array) in the old country, instead of shift and unshift, we said cdr and cons. we didn't have push and pop; if we wanted to mess with the backside of a list we had to reverse it first. but our code ran ten times as fast as ruby and had implicit undo at some point don hopkins suggested, i think it was, eat and barf for, respectively, push and pop on the front end. the back end would then necessarily be boof and shit (not shift). betcha wouldn't forget which end those pertained to
- beached_whale 2y agoAppend to which side? Push and pop are terms of are for queues and stacks which containers can be and the back and front tell us which end of the container we mean
- CamperBob2 2y agoAppend implies the back or end of something, while "prepend" would be the analogous term for the front.
- throwaway2037 2y agoFrom a natural language perspective, I agree. In practice, however, I see far more container libraries use the terms: (1) add or append and (2) insert w/ index (instead of prepend). Do you know of any container libraries that use the term prepend? From time to time, I use it in a function for some kind of business logic, e.g., "prependMeetingInvite()".
- mewpmewp2 2y agoJS dom manipulation has prepend and append. These always felt quite natural to me. While push back feels weird. The act of pushing feels out of place and I also think back and front may also be ambiguous.
- timthorn 2y agoPrefix would be the traditional term.
- jkaplowitz 2y agoWouldn’t prefix be the traditional counterpart to suffix, not to append?
- timthorn 2y agoYes, it would. But prefix is also a verb that means to "to add something before another thing" which is the counterpart to append.
- jkaplowitz 2y agoSuffix is also a verb that means “to add something after another thing”. I think the verb prefix is the counterpart to the verb suffix, the noun prefix is the counterpart to the noun suffix, and the verb prepend is the counterpart to the verb append.
- the_mitsuhiko 2y agoYou can look at the earliest sources of the STL from 1994 pre-standardization and the names were already used back then. It's not entirely unsurprising considering even back then there was a vector and a deque.
- layer8 2y agoYou have Alexander Stepanov to thank: http://stepanovpapers.com/STL/DOC.PDF http://stepanovpapers.com/STL/DOC.PDF The thought was to have the symmetry of push and pop, on both ends of a sequence container. See table 11 on page 22 (actually page 24 of the PDF).
- mort96 2y agoI agree that push_back/pop_back is a bit clunky, but I've gotten used to it tbh. What I haven't gotten used to is the terms "shift" and "unshift" from other languages. But I never get confused by C++'s push_front/pop_front. And I think the consistency has some value. Is it enough value to warrant the extra verbosity? Meh, I think it's more or less a wash.
- tzs 2y agoA minor problem with "append" (and "prepend" for the front) is that, as far as I know, there isn't a really satisfying word for the reverse operation. A search for the opposite of "append" suggests "remove", "subtract", "detach", "disconnect", "disjoin", "unfasten", "reduce", "diminish", and more. None of them really have the same "at the back" connotation that "append" has. "Push" and "pop" are well known from stacks so it makes sense to use them for operations on one end of list-like containers. That still leaves the problem of what to call the similar operations on the other end. I suppose we could sidestep that problem by only providing them at one end. So say push() adds to the back and pop() removes from the back. If you want to operate at the front just reverse the list, do your push or pop, and then reverse the list again and let the optimizer deal with eliminating the two reverses. :-)
- dataflow 2y agoappend is used for sequences. push_back is used for individual elements. Maybe not the most intuitive terminology, but there's value to separating the two ideas.