Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there are any standards or conventions on naming methods which add something if it not exist into another something.

For example: Store.instance().addItemIfNotExist(item)

IfNotExist is really ugly part. What workaround may be?

share|improve this question

closed as primarily opinion-based by durron597, gnat, MichaelT, Kilian Foth, Snowman Aug 3 '15 at 14:37

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

Why do you think 'addItemIfNotExist' is bad? Would you rather put the 'IfNotExist' part in the documentation and force your users to read the documentation along with the code? They're always going to have to read the code, so the more information you can state in your code (and nowhere else) the better. If you still don't like this, consider instead making your class a "Set", which already has this behavior defined intrinsically. – Nate W. Feb 1 '13 at 22:32
In memory, you would use a set and just call add(). A set can only contain one copy of each value. – kevin cline Feb 2 '13 at 18:20
up vote 3 down vote accepted

Some databases use the term Upsert to mean "Update or Insert as needed", but you may not want the Update aspect.

In general, an addItem() function has an implied, expected failure if the item already exists within the database, so you could use that. Your error handling code after the addItem() call would make it clear that failure because of already existing was okay.

Finally, you could consider addItemExclusive() to better indicate that the routine will check for existence prior to adding the item.

share|improve this answer

an easy one that comes to mind is addIfAbsent()

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.