views:

107

answers:

10

What's a good name for a method that adds something to a container if it's not already there, i.e.

void AddCustomerToList(CustomerList list, Customer customer)

but that name does not properly convey that it won't be added if it's not there already. What is a better name? AddCustomerToListIfNotThereAlready? EnsureCustomerInList?

+4  A: 

AddIfNotPresent

Jason
+2  A: 
bool TryAdd(CustomerList list, Customer customer)
Shay Erlichmen
+4  A: 

Change CustomerList to CustomerSet, then it's obvious.

add(CustomerSet set, Customer customer);
Chase Seibert
can't do that... the type of the container is constrained by other requirements... say it's a DataTable or List
JoelFan
A: 

You could do like the generic list does and create a ContinsCustomer function to check if it exists first, then use AddCustomerToList if it returns false.

SwDevMan81
But I don't want to have to write an "if" statement every time... I just want to call a method with a good name
JoelFan
Slightly better than throwing an ArgumentException if the customer is already in the list. I guess if you just want a name, then TryAdd by Shay would be what I would use
SwDevMan81
+1  A: 

Usually "put" would be used instead of "add" to convey this, but I agree with chase that you should just call this "add" and use "set" instead of "list". Unless of course the container supports both operations (which would be odd).

Draemon
Say the container is of a type that needs to be quite generic, i.e. a DataTable or List
JoelFan
Tables, List and Set have quite specific meanings. They are all specialisations of Collection. The operation the OP is asking about has Set semantics, so it makes no sense in a completely generic Collection. If you really wanted DataTable or List to have this operation (on top of a normal add operation), it would be an odd thing to do, but I would again suggest put.
Draemon
Re-reading your various comments, are you saying that this CustomerList must implement (eg) a List interface? If so, you are defining new semantics that break that interface. If you are forced to do it for whatever reason, you can still call it CustomerSet and have it implement List, restrict the original functionality and document it properly. You could call the operation add or put.
Draemon
A: 

Make it two methods. IsCustomerPresent() AddCustomer(). Then if you want you could make a AddCustomerIfNotAlreadyPresent() method that just calls your loosely coupled logic.

Matthew Vines
I wouldn't do it this way unless I was pretty sure it wouldn't be twice as much work. For many sorted data structures finding the proper place to insert an item also finds if there is already an item with the same key. Splitting it into two wastes a lot of work.
T.E.D.
It all depends really. Is there a need to add customers for any other reason than they are not already in the list? Is there a reason to check to see if a customer already exists outside of adding a new one. I don't want to think about how to add or check if they exist more than once. I would think that would be extra work.
Matthew Vines
+2  A: 

I would go with something like AddIfMissing, though I like the idea of renaming to Set since that's really what it is.

public static class ListExtensions
{
      public static void AddIfMissing<T>( this List<T> list, T item )
      {
            if (!list.Contains(item))
            {
                list.Add( item );
            }
      }
}
tvanfosson
+1, I like that even more than `AddIfNotPresent` as it leaves out the negation.
0xA3
A: 

in my opinion you are asking this question since you design OO is sub-optimal:

void AddCustomerToList(CustomerList list, Customer customer)

the responsability to ensure if a customer must be present must be assigned to CustomerList.

In that case you name your method:

  • Add in the case you specify in the documentation that the customer will be added only if not present
  • AddIfNotPresent or PutIfNotPresent otherwise.

I prefer the latter since it is more autodocumented.

dfa
A: 

Maybe just name it AddCustomerToList, and don't make it check to see if its already there, instead at the end, call another method, RemoveMultipleOccurences(..).

Mk12
hmmmmmmmmmm....
JoelFan
A: 

You didn't say what you do if it isn't there. Assuming the answer is "nothing", I'd go with.

bool InsertIfNew (CustomerList list, Customer customer)

The method returns true if it was "inserted", and false if it was already there. That the caller can perform alternate logic if the entry was already there. You might not want to do that, but someone might and you already have the knowledge inside the routine.

T.E.D.