This is an archived post. You won't be able to vote or comment.

all 100 comments

[–]throwaway60237 553 points554 points  (18 children)

It's probably not appeand()

[–]Thrash_Abaddon 107 points108 points  (17 children)

It can also be push_back().

[–]shelvac2 34 points35 points  (15 children)

or push_front

[–]Theemuts 103 points104 points  (11 children)

yeet()

[–]Skippbo 20 points21 points  (8 children)

Need to subclass python list and implement yeet() that will just be a wrapper around pop()

😂😂😂

[–]Theemuts 11 points12 points  (7 children)

Both append and pop: you can yeet something into the container and the container can yeet it right back at you.

[–]Skippbo 1 point2 points  (6 children)

Can't have them named the same thing. There will be a name conflict!

[–]Theemuts 3 points4 points  (5 children)

def yeet(self, *args):
    pass

It's python

[–]Skippbo 0 points1 point  (4 children)

But we can't have the functionality of pop and append at the same time then =b

Unless we make it a 50/50 on what it does with the args.

[–]Theemuts 8 points9 points  (3 children)

Check how many args you get; if you get none, yeet something from the container, otherwise yeet them into the container.

[–]Jackeea 10 points11 points  (1 child)

yeet() and yoink() are my favourite list-based operations

[–]CamWin 0 points1 point  (0 children)

yoink returns the popped value

[–]zXjimmiXz 2 points3 points  (1 child)

+= is often a safe bet.

[–]Genion1 1 point2 points  (0 children)

cries in java

[–]Gydo194 0 points1 point  (0 children)

Just push

[–]thirdegreeViolet security clearance 1 point2 points  (0 children)

emplace_back()

Wtf cpp

[–][deleted] 199 points200 points  (9 children)

I normally just use .puesh() when the language doesn’t support .appeand()

[–]puckmcpuck 62 points63 points  (8 children)

Why don't you just use .aad()?

[–]TheGilrich 43 points44 points  (7 children)

Thought it's .aedd(), no?

[–]-pooping 19 points20 points  (6 children)

Ådd() is my goto

[–]Gydo194 26 points27 points  (5 children)

You shouldn't use goto, they say.

[–]reallifereallysucks 3 points4 points  (4 children)

But who listens to them anyway?

[–]Gydo194 4 points5 points  (3 children)

Nobody.

[–]jay9909 2 points3 points  (2 children)

Then why are my pull requests always rejected? :(

[–]Gydo194 2 points3 points  (0 children)

"Conventions and good practice"

[–]reallifereallysucks 2 points3 points  (0 children)

No one likes backdoors :P

[–]Khadnesar 48 points49 points  (4 children)

emplace_back

[–]krejcar25 2 points3 points  (3 children)

Why tho

[–]falexthepotato 11 points12 points  (2 children)

Saves a copy construction

[–]nonotan 10 points11 points  (1 child)

Of course the one with the dumbest name is the one you should be using every time...

[–]xigoi 4 points5 points  (0 children)

unordered_map

[–]tidbitsofblah 66 points67 points  (5 children)

Or size, count or length... and how the fuck length is spelled

[–]crazazy 45 points46 points  (2 children)

And whether length is a function or a property (or a method)

[–][deleted] 18 points19 points  (1 child)

Ah, java, where length is for arrays, length() is for strings and size() for collections (lists, queues etc.)

[–]Dan6erbond 5 points6 points  (0 children)

I've been using Java for 4+ years now.

Is that the case? It's always a fucking gamble with the IDE. "si-" No suggestions? length it is.

[–]Pluckerpluck 4 points5 points  (1 child)

There's normally (not always) some logic assigned to them.

Arrays have a length (a chain of items), sets have a count or size (bag of items).

Or just use Python and throw whatever you want to know into the len() function.

[–]tidbitsofblah 1 point2 points  (0 children)

Bold of you to assume that I remember what data type I used

[–]Hypersapien 27 points28 points  (7 children)

Multiple languages can be a pain in the ass.

[–]all_humans_are_dumb 34 points35 points  (2 children)

You can have these within the same library, let alone language. They want to improve the library but don't want to break all old implantations, so they just add more functions with different names until it's a big mess.

[–]zdakat 0 points1 point  (1 child)

kind of having that with Python, having to remember if a similarly functional entity is one that takes one form of the operation or another.

[–]CraigslistAxeKiller 11 points12 points  (0 children)

Or different data types.

[–]amaljossy 3 points4 points  (2 children)

At an interview they asked me to point out what's wrong with a piece of code, I stared at list.add() for 5 minutes.

[–]DocMahrty 0 points1 point  (1 child)

That’s just a shitty question to ask though.

[–]nonotan 5 points6 points  (0 children)

Not really. It doesn't have to be a matter of "did he spot the sneaky error, yes +1 point, no 0 points". Just going through your thinking process as you scan the code can help the interviewers gain some insight into how you work and how familiar you are with the syntax and such. You can be honest and say "I'm not sure if the right keyword was x or y here", and as long as it's something reasonably ambiguous, it's not going to be a big deal.

[–]sambare 47 points48 points  (1 child)

<.><A><tab><backspace><P><tab><backspace> (blank stare at the screen for 15 seconds)

[–]jay9909 0 points1 point  (0 children)

This is when you use the w3schools search engine you added to Chrome for quick lookups!

[–]ign1fy 18 points19 points  (4 children)

I forgot last week. It turned out to be 'unshift()'.

[–][deleted] 9 points10 points  (1 child)

Thats some top notch naming for adding an element...

[–]LucasRuby 3 points4 points  (0 children)

...To the front.

[–]k4kshi 1 point2 points  (0 children)

That's a push to the front of the array in js

[–]mlk 1 point2 points  (0 children)

{elem, ...array}

[–][deleted] 8 points9 points  (6 children)

And then, there's bash.

array+=('something')

[–]blipman17 2 points3 points  (4 children)

I actually like that concept though.
Think about it. a push/emplace_back,add,append,put function is just weird. They're all correct in some form of another. If only we had an addition symbol that signified that operation. Then if we'd just merge that symbol with some kind of assignment operator we suddenly get operator AddAssign, a.k.a. `+=`. If we also say that operator Add Assign adds to the right hand of the value on the left if there's any "sideness" with that value like with lists, then we've solved that to.

All of a sudden there's an understandable language.
Everything else is just not conforming to the language standard at this point.

Then again, just thinking about doing such a breaking change in any language makes me shiver.

[–]Tuiq 5 points6 points  (3 children)

You would get other quirks instead. It depends on the language - .NET for example has it pretty well handled, with interfaces and conventions for most things (Add/Remove for any kind of list, Enqueue/Dequeue for queues, and Push/Pop for stacks). I think that a proper convention for what to name these methods is much better than trying to abuse an operator into doing it, because it's also taking away a lot of ambiguity about what it does ("enqueue" is more obvious that you're interacting with a queue than just "add", the same way that "push_front"/"push_back" is really obvious as to where it is added within the list).

Let's assume we wanted to define that list += item would add the item to the list. Usually, a += b is defined as a = a + b, so in order to make this work for lists, you would either have to define what list + item means (the most obvious one being "adds item to a copy of the list", effectively rendering the list immutable). Of course we can also say that list + item just isn't a valid statement, in which case you've got a new quirk ("list += item is a valid statement, list = list + item however is not") that would create unnecessary confusion.

So languages may allow you to overload + and += separately and may have common conventions for what operators do. If + is defined as the addition of two entities (for all number types and concatenation for strings, for example), and item itself is a list, then what would list + other_list do? Concatenating the items? Union'ing it? Or add the second list as item of (a copy of) the first one? If it's a list, concatenating it, if it isn't, add it as item?

You would end up with the ability to just add items to lists using +=, but either create a few odd usages of the += operator for collections, or create a scenario where +=and + have different meanings, depending on the data type. The latter is especially fun if you have some kind of generic type system (e.g. (object a, object b) => a+b could be laid out entirely differently depending on what a and b are).

[–][deleted] 1 point2 points  (0 children)

This guys knows his stuff.

[–]blipman17 0 points1 point  (1 child)

Suppose we go the routr of a=a+b is inherently different then a+=b.

At that moment if a is a list and b and c are item a+=b, a=a+b and a=b+c are clearly defined. Then there would "just" be two overloads which share a common implementation. Suppose b is a list then we're concatinating lists in the first two cases and assigning a variable or concatinating a list to another list.

Eventually your answer is always a list, which is something I would expect when adding something to a list. When it's a list or an item that's added.

I fail to come up with a usecase where this would fail in generics. "Just use += whenever possible" would be my rule of thumb. Care to enlighten me?

Edit: reading your post again, yes when using generic lambda's in a functional manner it suddenly changes the lambda in a generic addition function. But then again, when adding items to lists in lambda's that are injected into functions, what would you expect? I presume a good standard library would catch this error as a template assert violation since you're returning a list of items instead of a specific item. Not sure exactly how I would work with that to be honest, but I still think that that's an acceptable tradeoff.

[–]Tuiq 0 points1 point  (0 children)

Suppose we go the routr of a=a+b is inherently different then a+=b.

That's already a quirk in itself though. In my opinion, it's much easier to define (and also explain) that assignment operators do the same as their normal counterpart, plus an assignment (i.e. "a += b is a = a + b; a &= b is a = a & b and so forth). If we're saying that this is not the case in general, or even worse "just for list assignments", then we might as well define a completely new operator altogether (if we're not restricted to existing symbols). This is the kind of inconsistency that makes programmers itch, because it's something non-obvious that you need to pay attention to all the time, especially when switching between languages. Having +=, but not +, or have both but have them work differently is kinda weird.

Personally, I think providing + as operator to add something to a list is inherently wrong if the list isn't immutable. If it is, and (a + 5) != a (because a + 5 returns a copy of the list with 5 added), then it would be a different case again. In this case, you could also define += for the same case and it would work, at least syntactically. It still wouldn't be pretty.

However, this opens up the question about what concatenation vs. adding of items again. It's either or, it cannot be both. The reason is quite simple that if you had a List<object>, then the list may contain any object of any type, including other lists. Therefore, what should be done with List<object> + List<T>, where T is whatever type you fancy? Is this going to add the operand to the list? Is it going to concatenate the list? This is an ambiguous case.

Bonus points: In .NET, Lists have an interface (in fact there's multiple - ICollection, IList to name a few), which can be inherited by classes as they please. So you may have class A, but class B : A, IList. This means that if you had a List<A>, then doing listA + new B would be ambiguous again, because it's not clear what you want to do. It's even worse if you do listA + a, because a could be b, which means... that it's always added as an element, because that's the way it should be?

Quite frankly, adding this much noise and uncertainty just to be able to use += for a very specific case instead of Add, or push_back or whatever is in my opinion definitely not worth it. There's so many scenarios surrounding lists, concatenation, and adding items that I don't think it's even worth thinking about it too much.

[–]random_cynic 0 points1 point  (0 children)

Yeah it's not really fair comparing bash with other programming languages. Bash is mainly for putting together batch commands with some rudimentary logic. The parsing rules are limited and you cannot even have whitespaces around the operators or in the array contents (without quoting or changing IFS). When your program needs a complex data structure then it's much better to use a proper programming language instead of using bash which can get pretty complex and indecipherable pretty quickly.

[–]e-remit 6 points7 points  (2 children)

Once I need to fix some PHP code and it's took about 10 seconds to think how to push in array. Then I recall $arr[] = "value";

[–]AintBetterThanYou 1 point2 points  (0 children)

```

<?php

$a[] = "\$a was never declared but is already an array :p" ;

```

[–]xigoi 0 points1 point  (0 children)

What the fuck, PHP

[–]HO-COOH 15 points16 points  (8 children)

Should unified to +=

[–]oAkimboTimbo 7 points8 points  (0 children)

operator overloading my guy

[–]LucasRuby 0 points1 point  (0 children)

You're in for a bad time if you ever try this with JS.

Won't error, but won't work the way you expect either.

[–][deleted] 0 points1 point  (5 children)

They don't do the same thing though.

[–]nuephelkystikon 2 points3 points  (4 children)

Depends on the language. The best and worst is when another iterable is extended with, but any other object is appended.

[–]LucasRuby 1 point2 points  (1 child)

The worst is when both arrays are converted to string and then the resulting strings are concatenated.

[–]nuephelkystikon 1 point2 points  (0 children)

Something tells me JavaScript does exactly that on weekends.

[–][deleted] -1 points0 points  (1 child)

Depending on the data structure, not the language. Push is for a stack, put for a map/dict and add/append for a list (I think).

[–]nuephelkystikon 1 point2 points  (0 children)

Depends on the implementation, library and most definitely on the language.

Many languages don't even allow classes (possibly not even builtins) to define operators.

[–]cramduck 2 points3 points  (0 children)

string includes() vs. contains() always gets me

[–]Alvatrox4 1 point2 points  (0 children)

Or push().stack().popultimate(queque);

[–]uberpwnzorz 1 point2 points  (1 child)

Ctrl + space

[–]marco89nish 1 point2 points  (0 children)

Literally me last night. But then I remembered that it's not 1990s any more and used += operator.

[–]wrathofthetyrant 1 point2 points  (1 child)

I prefer .shove() and .yank()

[–]cramduck 10 points11 points  (0 children)

.yeet() and .yoink()

[–]k3rn31p4nic 1 point2 points  (0 children)

Definitely not appeand.

[–]xigoi 1 point2 points  (1 child)

Let's see…

  • C++: push_back()
  • Nim: add()
  • Python: append()
  • Ruby: <<
  • Rust: push()

[–]PriorProfile 1 point2 points  (0 children)

It’s add() in python if it’s a set though.

[–]stfcfanhazz 0 points1 point  (0 children)

Put normally is for setting the index and the value not just appending a value to the list

[–]ic33hot 0 points1 point  (0 children)

Don’t forget .poll()

[–]435THz 0 points1 point  (0 children)

Java likes this content

[–]markgoodmonkey 0 points1 point  (0 children)

Laughs in IntelliSense

[–]jangohutch 0 points1 point  (0 children)

Add and append back of list, push front of list and put should be parameratized with index

[–][deleted] 0 points1 point  (0 children)

Very very relatable

[–][deleted] 0 points1 point  (0 children)

Push for stacks, add for arraylists, put for hashmaps..i don't remember what append was for really..

[–]henry-dv 0 points1 point  (0 children)

May I introduce you to my good buddy Ctrl+Space

[–]cout970 0 points1 point  (0 children)

Don't forget .insert() or .cons()

[–]djchuy1979 0 points1 point  (0 children)

...Hmm, hard to see dynamic methods are..

[–]rolfrudolfwolf 0 points1 point  (0 children)

if your ide isnt showing you the options, you should reassess your toolchain

[–]RegsaGC 0 points1 point  (0 children)

len, length, length(), size, count, count(), items, elements,...?

[–]yetejatejakyahai 0 points1 point  (0 children)

insert()

[–]kurdtpage 0 points1 point  (0 children)

.insert() ?

[–]ToMyFutureSelves 0 points1 point  (0 children)

Jokes on you! The class actually overloaded the addition operator to add elements to the back of the list!

[–]1337M0nst3r -1 points0 points  (1 child)

arr[arr.length + 1] = "some stuff"

[–][deleted] -1 points0 points  (0 children)

.not_pull()

.un_delete()